Skip to content

Commit

Permalink
fix(bigFile): auto fix missing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Jul 26, 2024
1 parent 7d7ed32 commit 6fb76d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function uploadBigFile(fileId: string, file: File, progressCallback: Progr
const activeLoads = new Map<number, number>();
let finishedSize = 0;

for (let i = 0; i < totalChunks; i++) {
const uploadChunk = async (i: number) => {
while (activeChunks.size >= options.parallelChunks) {
await Promise.race(activeChunks);
}
Expand Down Expand Up @@ -90,6 +90,10 @@ 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?.ok) {
throw new Error(response.error);
}
Expand All @@ -108,6 +112,10 @@ async function uploadBigFile(fileId: string, file: File, progressCallback: Progr
activeChunks.add(uploadPromiseWithRetry);
}

for (let i = 0; i < totalChunks; i++) {
await uploadChunk(i);
}

await Promise.all(activeChunks);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
const uploadDir = path.join(tempDirectory, 'chunks_' + uploadId);
await fsExtra.ensureDir(uploadDir);

const sendError = async (errorMessage: string) => {
await fsExtra.emptyDir(uploadDir);
const sendError = async (errorMessage: string, emptyDir = true, extraInfo?: any) => {
if(emptyDir){
await fsExtra.emptyDir(uploadDir);
}
const errorPath = path.join(uploadDir, 'error.txt');
if (!await checkIfFileExists(errorPath)) {
await fs.writeFile(path.join(uploadDir, 'error.txt'), errorMessage);
}
return Response.json({ ok: false, error: errorMessage });
return Response.json({ ok: false, error: errorMessage, ...extraInfo });
};

if (typeof allowUpload === "function") {
Expand Down Expand Up @@ -105,7 +107,8 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU

const chunkSavePath = path.join(uploadDir, `${part}-${total}`);
if (!await checkIfFileExists(chunkSavePath)) {
await fs.writeFile(chunkSavePath, uploadFile.stream() as any);
const buffer = await uploadFile.arrayBuffer();
await fs.writeFile(chunkSavePath, Buffer.from(buffer));
}

if (part !== total) {
Expand All @@ -115,7 +118,7 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
const files = await fs.readdir(uploadDir);
for (let i = 1; i <= total; i++) {
if (!files.includes(`${i}-${total}`)) {
return await sendError(`Missing chunk ${i}, upload failed`);
return await sendError(`Missing chunk ${i}, upload failed`, false, { missingChunk: i });
}
}

Expand Down

0 comments on commit 6fb76d5

Please sign in to comment.