From 10a7121a756dee79ff3d57d8b95fe981f37e7a46 Mon Sep 17 00:00:00 2001 From: ido Date: Sun, 28 Jul 2024 13:07:42 +0300 Subject: [PATCH] fix(bigFile): race condition --- .../form/UploadBigFile/uploadBigFileServer.ts | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts b/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts index 777f5b1..f0b40c1 100644 --- a/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts +++ b/packages/forms/src/components/form/UploadBigFile/uploadBigFileServer.ts @@ -149,9 +149,16 @@ async function deleteOldUploads(tempDirectory: string, maxUploadTime: number) { const files = await fs.readdir(tempDirectory); for (const file of files) { const fullPath = path.join(tempDirectory, file); - const stat = await fs.stat(fullPath); - if (Date.now() - stat.mtime.getTime() > maxUploadTime) { - await fsExtra.remove(fullPath); + + try { + const stat = await fs.stat(fullPath); + if (Date.now() - stat.mtime.getTime() > maxUploadTime) { + await fsExtra.remove(fullPath); + } + } catch (error) { + if (error.code !== "ENOENT") { + throw error; + } } } } @@ -163,12 +170,18 @@ async function totalDirectorySize(directory: string) { const promises = []; for (const file of files) { const fullPath = path.join(directory, file); - const stat = await fs.stat(fullPath); - - if (stat.isDirectory()) { - promises.push(totalDirectorySize(fullPath)); - } else { - totalSize += stat.size; + try { + const stat = await fs.stat(fullPath); + + if (stat.isDirectory()) { + promises.push(totalDirectorySize(fullPath)); + } else { + totalSize += stat.size; + } + } catch (error) { + if (error.code !== "ENOENT") { + throw error; + } } }