Skip to content

Commit 2af2fab

Browse files
committed
Sync versions object during object create
1 parent 30833d7 commit 2af2fab

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

app/src/controllers/object.js

+51-54
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const {
3434
metadataService,
3535
objectService,
3636
storageService,
37+
syncService,
3738
tagService,
3839
userService,
3940
versionService,
@@ -297,20 +298,31 @@ const controller = {
297298
bucketId: bucketId
298299
});
299300

301+
// sync existing file versions and get COMS objectId
302+
const objectId =
303+
await syncService.syncJob(joinPath(bucketKey, req.currentUpload.filename), bucketId, true, userId);
304+
300305
// Hard short circuit skip file as the object already exists on bucket
301-
throw new Problem(409, {
306+
throw new Problem(409, 'Bucket already contains object', req.originalUrl, {
302307
detail: 'Bucket already contains object',
303-
instance: req.originalUrl
308+
existingObjectId: objectId,
304309
});
310+
305311
} catch (err) {
306312
if (err instanceof Problem) throw err; // Rethrow Problem type errors
307313

308314
// Object is soft deleted from the bucket
309315
if (err.$response?.headers['x-amz-delete-marker']) {
310-
throw new Problem(409, {
316+
317+
// sync existing file versions and get COMS objectId
318+
const objectId =
319+
await syncService.syncJob(joinPath(bucketKey, req.currentUpload.filename), bucketId, true, userId);
320+
321+
throw new Problem(409, 'Bucket already contains object', req.originalUrl, {
311322
detail: 'Bucket already contains object',
312-
instance: req.originalUrl
323+
existingObjectId: objectId,
313324
});
325+
314326
}
315327

316328
// Skip upload in the unlikely event we get an unexpected error from headObject
@@ -1032,67 +1044,52 @@ const controller = {
10321044

10331045
let s3Response;
10341046
try {
1035-
// Preflight S3 Object check
1036-
const headResponse = await storageService.headObject({
1047+
// Preflight check for object in bucket
1048+
const vs = await storageService.listObjectVersion({
10371049
filePath: joinPath(bucketKey, filename),
10381050
bucketId: bucketId
10391051
});
1040-
1041-
// Skip upload in the unlikely event we get an unexpected response from headObject
1042-
if (headResponse.$metadata?.httpStatusCode !== 200) {
1043-
throw new Problem(502, {
1044-
detail: 'Bucket communication error',
1045-
instance: req.originalUrl
1046-
});
1047-
}
1048-
1049-
// Object exists on bucket
1050-
if (req.currentUpload.contentLength < MAXCOPYOBJECTLENGTH) {
1051-
log.debug('Uploading with putObject', {
1052-
contentLength: req.currentUpload.contentLength,
1053-
function: 'createObject',
1054-
uploadMethod: 'putObject'
1055-
});
1056-
s3Response = await storageService.putObject({ ...data, stream: req });
1057-
} else if (req.currentUpload.contentLength < MAXFILEOBJECTLENGTH) {
1058-
log.debug('Uploading with lib-storage', {
1059-
contentLength: req.currentUpload.contentLength,
1060-
function: 'createObject',
1061-
uploadMethod: 'lib-storage'
1062-
});
1063-
s3Response = await storageService.upload({ ...data, stream: req });
1064-
} else {
1065-
throw new Problem(413, {
1066-
detail: 'File exceeds maximum 50GB limit',
1067-
instance: req.originalUrl
1068-
});
1069-
}
1070-
} catch (err) {
1071-
if (err instanceof Problem) throw err; // Rethrow Problem type errors
1072-
else if (err.$metadata?.httpStatusCode !== 404) {
1073-
// An unexpected response from headObject
1074-
throw new Problem(502, {
1075-
detail: 'Bucket communication error',
1076-
instance: req.originalUrl
1077-
});
1078-
} else {
1079-
if (err.$response?.headers['x-amz-delete-marker']) {
1080-
// Object is soft deleted from the bucket
1081-
throw new Problem(409, {
1082-
detail: 'Unable to update soft deleted object',
1083-
instance: req.originalUrl
1052+
// if object exists
1053+
if (Array.isArray(vs.Versions)) {
1054+
// if smaller file use putObject
1055+
if (req.currentUpload.contentLength < MAXCOPYOBJECTLENGTH) {
1056+
log.debug('Uploading with putObject', {
1057+
contentLength: req.currentUpload.contentLength,
1058+
function: 'createObject',
1059+
uploadMethod: 'putObject'
10841060
});
1061+
s3Response = await storageService.putObject({ ...data, stream: req });
1062+
}
1063+
// else use upload command
1064+
else if (req.currentUpload.contentLength < MAXFILEOBJECTLENGTH) {
1065+
log.debug('Uploading with lib-storage', {
1066+
contentLength: req.currentUpload.contentLength,
1067+
function: 'createObject',
1068+
uploadMethod: 'lib-storage'
1069+
});
1070+
s3Response = await storageService.upload({ ...data, stream: req });
10851071
} else {
1086-
// Bucket is missing the existing object
1087-
throw new Problem(409, {
1088-
detail: 'Bucket does not contain existing object',
1072+
throw new Problem(413, {
1073+
detail: 'File exceeds maximum 50GB limit',
10891074
instance: req.originalUrl
10901075
});
10911076
}
1092-
// TODO: Add in sync operation to update object record in COMS DB?
10931077
}
1078+
// else no file in bucket
1079+
else {
1080+
throw new Problem(409, {
1081+
detail: 'Bucket does not contain existing object',
1082+
instance: req.originalUrl
1083+
});
1084+
}
1085+
} catch (error) {
1086+
throw new Problem(502, {
1087+
detail: 'Bucket communication error',
1088+
instance: req.originalUrl
1089+
});
10941090
}
10951091

1092+
// do COMS database updates:
10961093
const s3Head = await storageService.headObject({
10971094
filePath: joinPath(bucketKey, req.currentUpload.filename),
10981095
bucketId: bucketId

app/src/docs/v1.api-spec.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,15 @@ paths:
374374
content:
375375
application/json:
376376
schema:
377-
$ref: "#/components/schemas/Response-Conflict"
377+
allOf:
378+
- $ref: "#/components/schemas/Response-Conflict"
379+
- type: object
380+
properties:
381+
existingObjectId:
382+
type: string
383+
format: uuid
384+
description: The ID of the existing object
385+
example: ac246e31-c807-496c-bc93-cd8bc2f1b2b4
378386
"411":
379387
$ref: "#/components/responses/LengthRequired"
380388
"413":

0 commit comments

Comments
 (0)