22 S3Client ,
33 DeleteObjectCommand ,
44 DeleteObjectsCommand ,
5+ CopyObjectCommand ,
56} from '@aws-sdk/client-s3'
67import { deleteTagPayload } from '../common/payloads'
78import { BikeTagApiResponse } from '../common/types'
@@ -12,8 +13,14 @@ import {
1213 listAllS3Objects ,
1314 loadIndex ,
1415 saveIndex ,
16+ getKeyFromUrl ,
17+ encodeMetadataValue ,
1518} from './helpers'
1619import { Tag } from '../common/schema'
20+ import {
21+ getImgurMysteryTitleFromBikeTagData ,
22+ getImgurMysteryDescriptionFromBikeTagData ,
23+ } from '../common/getters'
1724
1825export async function deleteTag (
1926 client : S3Client ,
@@ -24,9 +31,8 @@ export async function deleteTag(
2431 payload
2532 const bucket = `${ game } -biketag`
2633 const deleted : boolean [ ] = [ ]
27-
2834 let success = true
29- let errors = [ ]
35+ let errors : string [ ] = [ ]
3036
3137 if ( ! tagnumber ) {
3238 success = false
@@ -43,10 +49,7 @@ export async function deleteTag(
4349 const deleteOps = list . map ( async ( obj ) => {
4450 try {
4551 await client . send (
46- new DeleteObjectCommand ( {
47- Bucket : bucket ,
48- Key : obj . Key ,
49- } )
52+ new DeleteObjectCommand ( { Bucket : bucket , Key : obj . Key } )
5053 )
5154 return true
5255 } catch ( err ) {
@@ -71,12 +74,10 @@ export async function deleteTag(
7174 '' ,
7275 folder
7376 )
74-
7577 const mysteryObjects = await listAllS3Objects ( client , {
7678 Bucket : bucket ,
7779 Prefix : mysteryKey ,
7880 } )
79-
8081 keysToDelete . push ( ...mysteryObjects . map ( ( obj ) => ( { Key : obj . Key ! } ) ) )
8182 }
8283
@@ -89,12 +90,10 @@ export async function deleteTag(
8990 '' ,
9091 folder
9192 )
92-
9393 const foundObjects = await listAllS3Objects ( client , {
9494 Bucket : bucket ,
9595 Prefix : foundKey ,
9696 } )
97-
9897 keysToDelete . push ( ...foundObjects . map ( ( obj ) => ( { Key : obj . Key ! } ) ) )
9998 }
10099
@@ -106,7 +105,6 @@ export async function deleteTag(
106105 Delete : { Objects : keysToDelete } ,
107106 } )
108107 )
109-
110108 deleted . push ( ...keysToDelete . map ( ( ) => true ) )
111109
112110 if ( result . Errors && result . Errors . length > 0 ) {
@@ -124,8 +122,60 @@ export async function deleteTag(
124122 let indexUpdateError = ''
125123 if ( success && tagnumber ) {
126124 try {
127- const index = await loadIndex ( client , bucket , folder , region )
125+ let index = await loadIndex ( client , bucket , folder , region )
128126 const newIndex = index . filter ( ( t ) => t . tagnumber !== tagnumber )
127+
128+ if ( folder === 'main' && newIndex . length > 0 ) {
129+ const idx = newIndex . findIndex ( ( t ) => t . tagnumber === tagnumber - 1 )
130+
131+ if ( idx === - 1 ) {
132+ errors . push ( `Previous tag ${ tagnumber - 1 } not found in index` )
133+ success = false
134+ } else {
135+ const latestTag = { ...newIndex [ idx ] }
136+
137+ // Reset fields to mystery state
138+ latestTag . gps = { lat : 0 , long : 0 , alt : 0 }
139+ latestTag . foundPlayer = ''
140+ latestTag . foundImageUrl = ''
141+ latestTag . foundTime = 0
142+ latestTag . foundLocation = ''
143+
144+ // Refresh metadata on mystery image
145+ if ( latestTag . mysteryImageUrl ) {
146+ const mysteryKey = getKeyFromUrl ( latestTag . mysteryImageUrl )
147+ try {
148+ await client . send (
149+ new CopyObjectCommand ( {
150+ Bucket : bucket ,
151+ CopySource : `${ bucket } /${ mysteryKey } ` ,
152+ Key : mysteryKey ,
153+ ACL : 'public-read' ,
154+ MetadataDirective : 'REPLACE' ,
155+ Metadata : {
156+ title : encodeMetadataValue (
157+ getImgurMysteryTitleFromBikeTagData ( latestTag ) . trim ( )
158+ ) ,
159+ description : encodeMetadataValue (
160+ getImgurMysteryDescriptionFromBikeTagData (
161+ latestTag
162+ ) . trim ( )
163+ ) ,
164+ } ,
165+ } )
166+ )
167+ } catch ( err : any ) {
168+ success = false
169+ errors . push (
170+ `Failed to refresh metadata for mystery image: ${ err . message } `
171+ )
172+ }
173+ }
174+
175+ newIndex [ idx ] = latestTag
176+ }
177+ }
178+
129179 await saveIndex ( client , bucket , folder , newIndex )
130180 } catch ( indexErr : any ) {
131181 indexUpdateError = `Index update failed: ${ indexErr . message } `
@@ -141,7 +191,7 @@ export async function deleteTag(
141191 return {
142192 data : deleted ,
143193 success,
144- error : errors . join ( ';' ) ,
194+ error : errors . join ( '; ' ) ,
145195 source : AvailableApis [ AvailableApis . aws ] ,
146196 status : success ? HttpStatusCode . Ok : HttpStatusCode . BadRequest ,
147197 }
0 commit comments