Skip to content

Commit e9dc8d5

Browse files
mzoellnerMurderlon
andauthored
Fix for HEAD request not returning Upload-Offset when S3 multipart upload is finished (#207)
Co-authored-by: Murderlon <[email protected]>
1 parent 2153b17 commit e9dc8d5

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"complexity": ["error", 14],
2121
"prefer-rest-params": 0,
2222
"prefer-spread": 0,
23-
"strict": 0
23+
"strict": 0,
24+
"no-warning-comments": 0
2425
}
2526
}

lib/stores/S3Store.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,17 +505,39 @@ class S3Store extends DataStore {
505505
this._getMetadata(file_id)
506506
.then((metadata) => {
507507
return this._retrieveParts(file_id)
508-
.then((parts) => {
509-
return {
510-
parts,
511-
metadata,
512-
};
513-
})
508+
.then((parts) => ({ parts, metadata }))
514509
.catch((err) => {
515-
throw err;
510+
// getOffset is called from both the HEAD request and the PATCH request
511+
// (via the _processUpload method). The HEAD request calls it (implicitly)
512+
// with with_parts: false, the _processUpload method calls it (explicitly)
513+
// with with_parts: true. The _processUpload method expects upload parts to be returned,
514+
// which would not work when the upload is completed, since there are no parts anymore.
515+
// TODO: refactor to separate these concerns.
516+
// TODO: write a test for this case.
517+
if (with_parts === true || err.code !== 'NoSuchUpload') {
518+
throw err;
519+
}
520+
return this.client.headObject({ Bucket: this.bucket_name, Key: file_id })
521+
.promise()
522+
.then(() => {
523+
return {
524+
data: {
525+
...this.cache[file_id].file,
526+
size: metadata.file.upload_length,
527+
upload_length: metadata.file.upload_length,
528+
},
529+
isFinished: true,
530+
};
531+
})
532+
.catch((headError) => {
533+
throw headError;
534+
});
516535
});
517536
})
518537
.then((data) => {
538+
if (data.isFinished) {
539+
return resolve(data.data);
540+
}
519541
// if no parts are found, offset is 0
520542
if (data.parts.length === 0) {
521543
return resolve({

0 commit comments

Comments
 (0)