TUS: fix parallel uploads of the same file

## Issue
If you make 2 claims from the same source file, the second upload thinks it's trying to resume from the first one. They should be unique uploads.

## Approach
Stash the upload url for comparison when looking up existing uploads to resume.

Stash that in `params` to minimize code changes. We'll just need to ensure it is cleared before we generate the SDK payload.
This commit is contained in:
infinite-persistence 2021-11-12 13:59:51 +08:00
parent 861aaf4cde
commit dfe30b6d78
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
3 changed files with 15 additions and 3 deletions

View file

@ -62,6 +62,8 @@ declare type FileUploadSdkParams = {
remote_url?: string, remote_url?: string,
thumbnail_url?: string, thumbnail_url?: string,
title?: string, title?: string,
// Temporary values
uploadUrl?: string,
}; };
declare type FileUploadItem = { declare type FileUploadItem = {

View file

@ -164,6 +164,12 @@ export const publishReducer = handleActions(
if (progress) { if (progress) {
currentUploads[key].progress = progress; currentUploads[key].progress = progress;
delete currentUploads[key].status; delete currentUploads[key].status;
if (currentUploads[key].uploader.url && !currentUploads[key].params.uploadUrl) {
// TUS has finally obtained an upload url from the server. Stash that to check later when resuming.
// Ignoring immutable-update requirement (probably doesn't matter to the GUI).
currentUploads[key].params.uploadUrl = currentUploads[key].uploader.url;
}
} else if (status) { } else if (status) {
currentUploads[key].status = status; currentUploads[key].status = status;
if (status === 'error') { if (status === 'error') {

View file

@ -23,10 +23,13 @@ export function makeResumableUploadRequest(
reject(new Error('Publish: v2 does not support remote_url')); reject(new Error('Publish: v2 does not support remote_url'));
} }
const payloadParams = Object.assign({}, params);
delete payloadParams.uploadUrl; // cleanup
const jsonPayload = JSON.stringify({ const jsonPayload = JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: RESUMABLE_ENDPOINT_METHOD, method: RESUMABLE_ENDPOINT_METHOD,
params, params: payloadParams,
id: new Date().getTime(), id: new Date().getTime(),
}); });
@ -92,8 +95,9 @@ export function makeResumableUploadRequest(
uploader uploader
.findPreviousUploads() .findPreviousUploads()
.then((previousUploads) => { .then((previousUploads) => {
if (previousUploads.length > 0) { const index = previousUploads.findIndex((prev) => prev.uploadUrl === params.uploadUrl);
uploader.resumeFromPreviousUpload(previousUploads[0]); if (index !== -1) {
uploader.resumeFromPreviousUpload(previousUploads[index]);
} }
if (!isPreview) { if (!isPreview) {