Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/aws-s3-multipart: fix Golden Retriever integration #4526

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/@uppy/aws-s3-multipart/src/MultipartUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ class MultipartUploader {
/** @type {() => void} */
#onSuccess

/** @type {typeof import('../types/index').AwsS3MultipartOptions["shouldUseMultipart"]} */
/** @type {import('../types/index').AwsS3MultipartOptions["shouldUseMultipart"]} */
#shouldUseMultipart

/** @type {boolean} */
#isRestoring

#onReject = (err) => (err?.cause === pausingUploadReason ? null : this.#onError(err))

#maxMultipartParts = 10_000
Expand All @@ -83,6 +86,11 @@ class MultipartUploader {
this.#onError = this.options.onError
this.#shouldUseMultipart = this.options.shouldUseMultipart

// When we are restoring an upload, we already have an uploadId. Otherwise
// we need to call `createMultipartUpload` to get an `uploadId`.
// Non-multipart uploads are not restorable.
this.#isRestoring = 'uploadId' in options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options is passed by the user, why are we uploadId from there to know if we are restoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not passed by the user, it's passed by index.js

const upload = new MultipartUploader(file.data, {
// .bind to pass the file object to each handler.
companionComm: this.#companionCommunicationQueue,
log: (...args) => this.uppy.log(...args),
getChunkSize: this.opts.getChunkSize ? this.opts.getChunkSize.bind(this) : null,
onProgress,
onError,
onSuccess,
onPartComplete,
file,
shouldUseMultipart: this.opts.shouldUseMultipart,
...file.s3Multipart,
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it the relationship between uploadId determining whether we are restoring more clear somehow?


this.#initChunks()
}

Expand All @@ -108,12 +116,12 @@ class MultipartUploader {
}
this.#chunks = Array(arraySize)

for (let i = 0, j = 0; i < fileSize; i += chunkSize, j++) {
const end = Math.min(fileSize, i + chunkSize)
for (let offset = 0, j = 0; offset < fileSize; offset += chunkSize, j++) {
const end = Math.min(fileSize, offset + chunkSize)

// Defer data fetching/slicing until we actually need the data, because it's slow if we have a lot of files
const getData = () => {
const i2 = i
const i2 = offset
return this.#data.slice(i2, end)
}

Expand All @@ -123,6 +131,14 @@ class MultipartUploader {
onComplete: this.#onPartComplete(j),
shouldUseMultipart,
}
if (this.#isRestoring) {
const size = offset + chunkSize > fileSize ? fileSize - offset : chunkSize
// setAsUploaded is called by listPart, to keep up-to-date the
// quantity of data that is left to actually upload.
this.#chunks[j].setAsUploaded = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a function and not setting uploaded directly, since we use size from this scope anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know yet if it was already uploaded

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use a comment how this is used later

this.#chunkState[j].uploaded = size
}
}
}
} else {
this.#chunks = [{
Expand Down Expand Up @@ -181,6 +197,9 @@ class MultipartUploader {
if (!this.#abortController.signal.aborted) this.#abortController.abort(pausingUploadReason)
this.#abortController = new AbortController()
this.#resumeUpload()
} else if (this.#isRestoring) {
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
this.options.companionComm.restoreUploadFile(this.#file, { uploadId: this.options.uploadId, key: this.options.key })
this.#resumeUpload()
} else {
this.#createUpload()
}
Expand Down
12 changes: 9 additions & 3 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class HTTPCommunicationQueue {
}
}

restoreUploadFile (file, uploadIdAndKey) {
this.#cache.set(file.data, uploadIdAndKey)
}

async resumeUploadFile (file, chunks, signal) {
throwIfAborted(signal)
if (chunks.length === 1 && !chunks[0].shouldUseMultipart) {
Expand All @@ -278,9 +282,11 @@ class HTTPCommunicationQueue {
.map((chunk, i) => {
const partNumber = i + 1
const alreadyUploadedInfo = alreadyUploadedParts.find(({ PartNumber }) => PartNumber === partNumber)
return alreadyUploadedInfo == null
? this.uploadChunk(file, partNumber, chunk, signal)
: { PartNumber: partNumber, ETag: alreadyUploadedInfo.ETag }
if (alreadyUploadedInfo == null) {
return this.uploadChunk(file, partNumber, chunk, signal)
}
chunk.setAsUploaded?.()
return { PartNumber: partNumber, ETag: alreadyUploadedInfo.ETag }
}),
)
throwIfAborted(signal)
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/aws-s3-multipart/types/chunk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Chunk {
onProgress: (ev: ProgressEvent) => void
onComplete: (etag: string) => void
shouldUseMultipart: boolean
setAsUploaded?: () => void
}