From 9ccc10c4c80ca290debee5c2a3b33db6bdeeef41 Mon Sep 17 00:00:00 2001 From: Jeremy Ho Date: Thu, 31 Aug 2023 14:46:54 -0700 Subject: [PATCH] Ensure TagSet defaults to an empty array instead of undefined There are various places where the TagSet is assumed to always be an array. However, Dell ECS may return situations where the TagSet is outright undefined instead of yielding an empty array. We compensate for this to prevent any undesired undefined parsing issues. Signed-off-by: Jeremy Ho --- app/src/controllers/object.js | 14 ++++++++++---- app/src/services/sync.js | 15 +++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/src/controllers/object.js b/app/src/controllers/object.js index 0cb6a9c7..22c0f38c 100644 --- a/app/src/controllers/object.js +++ b/app/src/controllers/object.js @@ -117,7 +117,9 @@ const controller = { } // get existing tags on source object, eg: { 'animal': 'bear', colour': 'black' } const sourceObject = await storageService.getObjectTagging({ filePath: objPath, s3VersionId: sourceS3VersionId, bucketId: bucketId }); - const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value })))); + const sourceTags = Object.assign({}, { + ...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value }))) + }); const metadataToAppend = getMetadata(req.headers); const data = { @@ -179,7 +181,7 @@ const controller = { const newSet = newTags // Join new tags and existing tags - .concat(existingTags) + .concat(existingTags ?? []) // remove existing 'coms-id' tag if it exists .filter(x => x.Key !== 'coms-id') // filter duplicates @@ -528,7 +530,9 @@ const controller = { s3VersionId: sourceS3VersionId, bucketId: bucketId }); - const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value })))); + const sourceTags = Object.assign({}, { + ...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value }))) + }); const data = { bucketId: bucketId, @@ -892,7 +896,9 @@ const controller = { s3VersionId: sourceS3VersionId, bucketId: bucketId }); - const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value })))); + const sourceTags = Object.assign({}, { + ...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value }))) + }); const newMetadata = getMetadata(req.headers); diff --git a/app/src/services/sync.js b/app/src/services/sync.js index caea4e76..5c46d3e7 100644 --- a/app/src/services/sync.js +++ b/app/src/services/sync.js @@ -30,7 +30,7 @@ const service = { let objId = uuidv4(); if (typeof s3Object === 'object') { // If regular S3 Object - const { TagSet } = await storageService.getObjectTagging({ filePath: path, bucketId: bucketId }); + const TagSet = await storageService.getObjectTagging({ filePath: path, bucketId: bucketId }).then(result => result.TagSet ?? []); const s3ObjectComsId = TagSet.find(obj => (obj.Key === 'coms-id'))?.Value; if (s3ObjectComsId && uuidValidate(s3ObjectComsId)) { @@ -48,12 +48,12 @@ const service = { const { Versions } = await storageService.listAllObjectVersions({ filePath: path, bucketId: bucketId }); for (const versionId of Versions.map(version => version.VersionId)) { - const result = await storageService.getObjectTagging({ + const TagSet = await storageService.getObjectTagging({ filePath: path, s3VersionId: versionId, bucketId: bucketId - }); - const oldObjId = result?.TagSet.find(obj => obj.Key === 'coms-id')?.Value; + }).then(result => result.TagSet ?? []); + const oldObjId = TagSet.find(obj => obj.Key === 'coms-id')?.Value; if (oldObjId && uuidValidate(oldObjId)) { objId = oldObjId; @@ -331,13 +331,16 @@ const service = { // COMS Tags const comsTags = comsTagsForVersion[0]?.tagset ?? []; // S3 Tags - const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet); + const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet ?? []); // Ensure `coms-id` tag exists on this version in S3 if (s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) { await storageService.putObjectTagging({ filePath: path, - tags: s3TagsForVersion?.TagSet.concat([{ Key: 'coms-id', Value: comsVersion.objectId }]), + tags: (s3TagsForVersion?.TagSet ?? []).concat([{ + Key: 'coms-id', + Value: comsVersion.objectId + }]), s3VersionId: comsVersion.s3VersionId, bucketId: bucketId, });