@@ -34,6 +34,7 @@ const {
34
34
metadataService,
35
35
objectService,
36
36
storageService,
37
+ syncService,
37
38
tagService,
38
39
userService,
39
40
versionService,
@@ -297,20 +298,31 @@ const controller = {
297
298
bucketId : bucketId
298
299
} ) ;
299
300
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
+
300
305
// 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 , {
302
307
detail : 'Bucket already contains object' ,
303
- instance : req . originalUrl
308
+ existingObjectId : objectId ,
304
309
} ) ;
310
+
305
311
} catch ( err ) {
306
312
if ( err instanceof Problem ) throw err ; // Rethrow Problem type errors
307
313
308
314
// Object is soft deleted from the bucket
309
315
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 , {
311
322
detail : 'Bucket already contains object' ,
312
- instance : req . originalUrl
323
+ existingObjectId : objectId ,
313
324
} ) ;
325
+
314
326
}
315
327
316
328
// Skip upload in the unlikely event we get an unexpected error from headObject
@@ -1032,67 +1044,52 @@ const controller = {
1032
1044
1033
1045
let s3Response ;
1034
1046
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 ( {
1037
1049
filePath : joinPath ( bucketKey , filename ) ,
1038
1050
bucketId : bucketId
1039
1051
} ) ;
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'
1084
1060
} ) ;
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 } ) ;
1085
1071
} 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' ,
1089
1074
instance : req . originalUrl
1090
1075
} ) ;
1091
1076
}
1092
- // TODO: Add in sync operation to update object record in COMS DB?
1093
1077
}
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
+ } ) ;
1094
1090
}
1095
1091
1092
+ // do COMS database updates:
1096
1093
const s3Head = await storageService . headObject ( {
1097
1094
filePath : joinPath ( bucketKey , req . currentUpload . filename ) ,
1098
1095
bucketId : bucketId
0 commit comments