Skip to content

Commit

Permalink
feat(append): add support for beforeComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
olalonde committed Sep 27, 2016
1 parent 2eb853f commit 00cddaa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"node": ">=6"
},
"devDependencies": {
"abstract-tus-store": "^1.1.0",
"babel-cli": "^6.14.0",
"babel-preset-blockai": "^1.0.0",
"babel-tape-runner": "^2.0.1",
Expand All @@ -63,7 +62,7 @@
}
},
"dependencies": {
"abstract-tus-store": "^1.4.1",
"abstract-tus-store": "^1.5.0",
"aws-sdk": "^2.6.3",
"common-streams": "^1.1.0",
"debug": "^2.2.0",
Expand Down
31 changes: 25 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ export default ({
const upload = await getUpload(uploadId)
debug(upload)
const offset = await getUploadOffset(uploadId, upload.key)
.then(uploadOffset => {
if (uploadOffset === upload.uploadLength) {
// upload is completed but completeMultipartUpload has not been
// called for some reason...
// force a last call to append :/
return uploadOffset - 1
}
return uploadOffset
})
.catch(err => {
// we got the upload file but upload part does not exist
// that means the upload is actually completed.
Expand All @@ -204,7 +213,12 @@ export default ({
return meterStream
}

const afterWrite = async (uploadId, upload, parts) => {
const afterWrite = async (
uploadId,
upload,
beforeComplete,
parts,
) => {
const offset = await getUploadOffset(parts)
// Upload complete!
debug(`offset = ${offset}`)
Expand All @@ -214,7 +228,7 @@ export default ({
// returning a response
if (offset === upload.uploadLength) {
debug('Completing upload!')
// TODO: completeMultipartUpload
await beforeComplete(upload, uploadId)
const MultipartUpload = {
Parts: parts.map(({ ETag, PartNumber }) => ({ ETag, PartNumber })),
}
Expand Down Expand Up @@ -264,6 +278,7 @@ export default ({
}
return { expectedOffset: arg3, opts: arg4 }
})()
const { beforeComplete = async () => {} } = opts
// need to do this asap to make sure we don't miss reads
const through = rs.pipe(new PassThrough())

Expand All @@ -273,6 +288,13 @@ export default ({
const parts = await getParts(uploadId, upload.key)
const offset = await getUploadOffset(parts)

// For some reason, upload is finished but not completed yet
if (offset === upload.uploadLength) {
if (!Number.isInteger(expectedOffset) || expectedOffset === upload.uploadLength - 1) {
return afterWrite(uploadId, upload, beforeComplete, parts)
}
}

if (Number.isInteger(expectedOffset)) {
// check if offset is right
if (offset !== expectedOffset) {
Expand Down Expand Up @@ -302,10 +324,7 @@ export default ({
body: through.pipe(limitStream), // .pipe(sizeStream),
})

debug('new parts:')
debug(newParts)

return afterWrite(uploadId, upload, [
return afterWrite(uploadId, upload, beforeComplete, [
...parts,
...newParts,
])
Expand Down

0 comments on commit 00cddaa

Please sign in to comment.