From fc8d36295dc75ef3577abff96333abfcca23557f Mon Sep 17 00:00:00 2001 From: Norris Ng <103449568+norrisng-bc@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:41:17 -0800 Subject: [PATCH] syncObject: delete 'coms-id' tag on new file if id already in use --- app/src/services/sync.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/app/src/services/sync.js b/app/src/services/sync.js index 1477e3d2..8cb6db1e 100644 --- a/app/src/services/sync.js +++ b/app/src/services/sync.js @@ -175,7 +175,43 @@ const service = { // Case: not in COMS - insert new COMS object else { - const objId = await service._deriveObjectId(s3Object, path, bucketId); + let objId = await service._deriveObjectId(s3Object, path, bucketId); + let existingComsId; + try { + existingComsId = await objectService.read(objId); + } catch { + // Derived object id not currently in use by COMS + } + + // Object id in 'coms-id' S3 tag already in use in COMS: + // Delete the 'coms-id' tag on the new object, + // so that we don't attempt to re-use an existing COMS object id. + if (existingComsId) { + const sourceObject = await storageService.getObjectTagging({ + filePath: path, + bucketId: bucketId + }); + + // Workaround for deleting just the 'coms-id' tag (S3 can only delete ALL tags on an object): + // Retrieve all tags, delete all tags on object, then re-add all tags except 'coms-id'. + const objectTags = sourceObject.TagSet.filter( + x => !sourceObject.TagSet.includes(x.Key) && x.Key != 'coms-id' + ); + await storageService.deleteObjectTagging({ + filePath: path, + bucketId: bucketId + }); + const data = { + bucketId, + filePath: path, + tags: objectTags + }; + await storageService.putObjectTagging(data); + + // _deriveObjectId() doesn't care if it conflicts with existing coms-id's, + // so generate a new one after deleting the S3 tag + objId = uuidv4(); + } response = await objectService.create({ id: objId,