Skip to content

Commit

Permalink
fix(bigFile): race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Jul 28, 2024
1 parent 4abb742 commit 10a7121
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand All @@ -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;
}
}
}

Expand Down

0 comments on commit 10a7121

Please sign in to comment.