Change removeOldImages to be a bit cleaner (#302)

This commit is contained in:
bjcscat 2022-07-24 23:00:31 -07:00 committed by GitHub
parent 398ce07bd2
commit 271600c44f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,6 +42,7 @@ export async function upload(client, ipc, result, context, interaction = false)
export async function removeOldImages(ipc, size) {
if (size > process.env.THRESHOLD) {
const files = (await readdir(process.env.TEMPDIR)).map((file) => {
return lstat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
if (stats.isSymbolicLink()) return;
@ -51,19 +52,23 @@ export async function removeOldImages(ipc, size) {
ctime: stats.ctime
};
});
}).filter(Boolean);
});
const resolvedFiles = await Promise.all(files);
let newSize = resolvedFiles.filter(Boolean).reduce((a, b) => {
return a + b.size;
}, 0);
const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
while (newSize > process.env.THRESHOLD) {
const oldestFiles = resolvedFiles.filter(Boolean).sort((a, b) => a.ctime - b.ctime);
do {
if (!oldestFiles[0]) break;
await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
newSize -= oldestFiles[0].size;
await ipc.centralStore.set("dirSizeCache", newSize);
logger.log(`Removed oldest image file: ${oldestFiles[0].name}`);
size -= oldestFiles[0].csize;
oldestFiles.shift();
}
} while ( size > process.env.THRESHOLD )
let newSize = oldestFiles.reduce((a, b) => {
return a + b.size;
}, 0);
await ipc.centralStore.set("dirSizeCache", newSize);
}
}
}