From 1c08c603046d7eb938462ceff59e90b9f414f7e4 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 29 May 2020 15:09:29 +0200 Subject: [PATCH] retry on 429 --- src/components/HomeUpload/HomeUpload.js | 34 ++++++++++++++++--------- src/components/UploadFile/UploadFile.js | 10 +++++++- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/components/HomeUpload/HomeUpload.js b/src/components/HomeUpload/HomeUpload.js index 7a3e7699..ad066c21 100644 --- a/src/components/HomeUpload/HomeUpload.js +++ b/src/components/HomeUpload/HomeUpload.js @@ -105,7 +105,7 @@ export default function HomeUpload() { }); }; - acceptedFiles.forEach(async (file) => { + acceptedFiles.forEach((file) => { const onUploadProgress = ({ loaded, total }) => { const progress = loaded / total; const status = progress === 1 ? "processing" : "uploading"; @@ -120,21 +120,31 @@ export default function HomeUpload() { return; } - try { - let response; + const upload = async () => { + try { + let response; - if (file.directory) { - const directory = file.files.reduce((acc, file) => ({ ...acc, [getRelativeFilePath(file)]: file }), {}); + if (file.directory) { + const directory = file.files.reduce((acc, file) => ({ ...acc, [getRelativeFilePath(file)]: file }), {}); - response = await client.uploadDirectory(directory, encodeURIComponent(file.name), { onUploadProgress }); - } else { - response = await client.upload(file, { onUploadProgress }); + response = await client.uploadDirectory(directory, encodeURIComponent(file.name), { onUploadProgress }); + } else { + response = await client.upload(file, { onUploadProgress }); + } + + onFileStateChange(file, { status: "complete", url: client.getUrl(response.skylink) }); + } catch (error) { + if (error.response && error.response.status === HttpStatus.TOO_MANY_REQUESTS) { + onFileStateChange(file, { progress: -1 }); + + return new Promise((resolve) => setTimeout(() => resolve(upload()), 3000)); + } + + onFileStateChange(file, { status: "error", error: createUploadErrorMessage(error) }); } + }; - onFileStateChange(file, { status: "complete", url: client.getUrl(response.skylink) }); - } catch (error) { - onFileStateChange(file, { status: "error", error: createUploadErrorMessage(error) }); - } + upload(); }); }; diff --git a/src/components/UploadFile/UploadFile.js b/src/components/UploadFile/UploadFile.js index 6d134299..8cecbb8c 100644 --- a/src/components/UploadFile/UploadFile.js +++ b/src/components/UploadFile/UploadFile.js @@ -36,6 +36,14 @@ export default function UploadFile({ file, url, status, progress, error }) { }; const copyText = copied ? "Copied!" : "Copy to clipboard"; + const getProgressText = (progress) => { + if (progress === -1) { + return "Waiting..."; + } else if (progress > 0) { + return `Uploading ${Math.round(progress * 100)}%`; + } + return "Uploading..."; + }; return (
@@ -43,7 +51,7 @@ export default function UploadFile({ file, url, status, progress, error }) {

{file.name}

- {status === "uploading" && (progress ? `Uploading ${Math.round(progress * 100)}%` : "Uploading...")} + {status === "uploading" && getProgressText(progress)} {status === "processing" && "Processing..."} {status === "error" && {error || "Upload failed."}} {status === "complete" && (