Unverified Commit 2fc79b4b authored by mvdbeek's avatar mvdbeek
Browse files

Improve robustness of tus upload and calculation of progress

We now don't let the upload fail at all and instead retry indefinitely.
The progress report is also more accurate as we only report the progress
of individual chunks that have been confirmed by the server.
parent 582a7b95
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);
        onError: function (err) {
            const status = err.originalResponse?.getStatus();
            if (status == 403) {
                console.error(`Failed because of missing authorization: ${err}`);
                cnf.error(error);
            } 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.