Skip to content

Commit

Permalink
fix(bigFile): calc directory size
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Jul 26, 2024
1 parent 80ebb4a commit 4ab5311
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/simple-form/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const onRequest = sequence(astroForms({
bigFilesUpload: {
bigFileServerOptions: {
maxUploadSize: 1024 * 1024 * 1024, // 1GB
maxDirectorySize: 1024 * 1024 * 1024 * 10, // 10GB
}
}
}
Expand Down
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(new Error('Upload failed'));
reject({ok: false, error: xhr.responseText});
}
};
xhr.onerror = () => {
reject(new Error('Upload failed'));
reject({ok: false, error: xhr.responseText});
};

xhr.open('POST', location.href, true);
Expand Down Expand Up @@ -97,7 +97,7 @@ async function uploadBigFile(fileId: string, file: File, progressCallback: Progr
if (retry === 0) {
throw error;
}
return uploadPromise(retry - 1);
return await uploadPromise(retry - 1);
}
})().then(() => {
activeLoads.delete(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU

const sendError = async (errorMessage: string) => {
await fsExtra.emptyDir(uploadDir);
await fs.writeFile(path.join(uploadDir, 'error.txt'), errorMessage);
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 });
};

Expand All @@ -89,7 +92,7 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
return await sendError("Directory size exceeded");
}

const newTotalSize = (await totalDirectorySize(tempDirectory)) + uploadSize;
const newTotalSize = (await totalDirectorySize(uploadDir)) + uploadSize;
if (newTotalSize > maxUploadSize) {
return await sendError("Upload size exceeded");
}
Expand Down

0 comments on commit 4ab5311

Please sign in to comment.