Unverified Commit b6629d7f authored by Marius van den Beek's avatar Marius van den Beek Committed by GitHub
Browse files

Merge pull request #14246 from mvdbeek/tus_robustness_22_01

[22.01] Improve robustness of tus upload and calculation of progress
parents 7e70fa3a 4793bbc8
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -39,16 +39,25 @@ function tusUpload(data, index, tusEndpoint, cnf) {
    console.debug(`Starting chunked upload for ${file.name} [chunkSize=${chunkSize}].`);
    const upload = new tus.Upload(file, {
        endpoint: tusEndpoint,
        retryDelays: [0, 3000, 10000],
        fingerprint: buildFingerprint(cnf),
        chunkSize: chunkSize,
        metadata: data.payload,
        onError: function (error) {
            console.log("Failed because: " + error);
            cnf.error(error);
        onError: function (err) {
            const status = err.originalResponse?.getStatus();
            if (status == 403) {
                console.error(`Failed because of missing authorization: ${err}`);
                cnf.error(err);
            } else {
                // 🎵 Never gonna give you up 🎵
                console.log(`Failed because: ${err}\n, will retry in 10 seconds`);

                setTimeout(() => startTusUpload(upload), 10000);
            }
        },
        onProgress: function (bytesUploaded, bytesTotal) {
            var percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
            console.log(bytesUploaded, bytesTotal, percentage + "%");
        onChunkComplete: function (chunkSize, bytesAccepted, bytesTotal) {
            const percentage = ((bytesAccepted / bytesTotal) * 100).toFixed(2);
            console.log(bytesAccepted, bytesTotal, percentage + "%");
            cnf.progress(percentage);
        },
        onSuccess: function () {
@@ -62,6 +71,10 @@ function tusUpload(data, index, tusEndpoint, cnf) {
            tusUpload(data, index + 1, tusEndpoint, cnf);
        },
    });
    startTusUpload(upload);
}

function startTusUpload(upload) {
    // Check if there are any previous uploads to continue.
    upload.findPreviousUploads().then(function (previousUploads) {
        // Found previous uploads so we select the first one.