diff --git a/packages/forms/src/components/form/UploadBigFile/uploadBigFileClient.ts b/packages/forms/src/components/form/UploadBigFile/uploadBigFileClient.ts index e277d19..5410edc 100644 --- a/packages/forms/src/components/form/UploadBigFile/uploadBigFileClient.ts +++ b/packages/forms/src/components/form/UploadBigFile/uploadBigFileClient.ts @@ -40,11 +40,11 @@ async function uploadChunkWithXHR(file: Blob, info: Record, progres if (xhr.status >= 200 && xhr.status < 300) { resolve(JSON.parse(xhr.responseText)); } else { - reject({ok: false, error: xhr.responseText}); + reject({ ok: false, error: xhr.responseText }); } }; xhr.onerror = () => { - reject({ok: false, error: xhr.responseText}); + reject({ ok: false, error: xhr.responseText }); }; xhr.open('POST', location.href, true); @@ -90,8 +90,13 @@ async function uploadBigFile(fileId: string, file: File, progressCallback: Progr try { const response: any = await upload; - if(response?.missingChunk){ - await uploadChunk(response?.missingChunk - 1); + if (response?.missingChunks && activeChunks.size < options.parallelChunks) { + const promises: Promise[] = []; + for (const chunk of response.missingChunks) { + const {promise} = await uploadChunk(chunk - 1); + promises.push(promise); + } + await Promise.all(promises); } if (!response?.ok) { @@ -110,7 +115,8 @@ async function uploadBigFile(fileId: string, file: File, progressCallback: Progr }); activeChunks.add(uploadPromiseWithRetry); - } + return { promise: uploadPromiseWithRetry }; + }; for (let i = 0; i < totalChunks; i++) { await uploadChunk(i); @@ -155,7 +161,7 @@ export async function uploadAllFiles(els: NodeListOf, options: } const fileId = uuid(); - if(failed){ + if (failed) { onUploadFinished(el, file, fileId, true); continue; } @@ -181,7 +187,7 @@ function onUploadFinished(el: HTMLInputElement, file: File, id: string, failed?: const inputElement = document.createElement('input'); inputElement.type = 'hidden'; inputElement.name = el.name; - inputElement.value = `big-file:${JSON.stringify({ id, name: file.name, failed})}`; + inputElement.value = `big-file:${JSON.stringify({ id, name: file.name, failed })}`; el.required = false; el.removeAttribute('name'); el.after(inputElement); diff --git a/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts b/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts index a9e4cbd..777f5b1 100644 --- a/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts +++ b/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts @@ -69,7 +69,7 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial { - if(emptyDir){ + if (emptyDir) { await fsExtra.emptyDir(uploadDir); } const errorPath = path.join(uploadDir, 'error.txt'); @@ -116,11 +116,15 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial 0) { + return await sendError(`Missing chunks ${missingChunks}, upload failed`, false, { missingChunks }); + } const outputStream = oldFs.createWriteStream(uploadFilePath, { flags: 'a' }); for (let i = 1; i <= total; i++) {