Skip to content

Commit

Permalink
fix(bigFiles): missing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Jul 26, 2024
1 parent c646914 commit fb9e763
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ async function uploadChunkWithXHR(file: Blob, info: Record<string, any>, 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);
Expand Down Expand Up @@ -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<any>[] = [];
for (const chunk of response.missingChunks) {
const {promise} = await uploadChunk(chunk - 1);
promises.push(promise);
}
await Promise.all(promises);
}

if (!response?.ok) {
Expand All @@ -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);
Expand Down Expand Up @@ -155,7 +161,7 @@ export async function uploadAllFiles(els: NodeListOf<HTMLInputElement>, options:
}

const fileId = uuid();
if(failed){
if (failed) {
onUploadFinished(el, file, fileId, true);
continue;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
await fsExtra.ensureDir(uploadDir);

const sendError = async (errorMessage: string, emptyDir = true, extraInfo?: any) => {
if(emptyDir){
if (emptyDir) {
await fsExtra.emptyDir(uploadDir);
}
const errorPath = path.join(uploadDir, 'error.txt');
Expand Down Expand Up @@ -116,11 +116,15 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
}

const files = await fs.readdir(uploadDir);
const missingChunks = [];
for (let i = 1; i <= total; i++) {
if (!files.includes(`${i}-${total}`)) {
return await sendError(`Missing chunk ${i}, upload failed`, false, { missingChunk: i });
missingChunks.push(i);
}
}
if (missingChunks.length > 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++) {
Expand Down

0 comments on commit fb9e763

Please sign in to comment.