dear god what did I do to deserve this

This commit is contained in:
Essem 2022-07-19 23:03:51 -05:00
parent 809f659f3d
commit ffa22852bc
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -34,6 +34,7 @@ export async function upload(client, result, context, interaction = false) {
}));
}
if (process.env.THRESHOLD) {
process.env.DIRSIZECACHE = parseInt(process.env.DIRSIZECACHE) + result.file.length;
await removeOldImages();
}
}
@ -52,12 +53,13 @@ export async function removeOldImages() {
}).filter(Boolean);
const resolvedFiles = await Promise.all(files);
process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{
return a.size + b.size;
return a + b.size;
}, 0);
const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
while (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
if (!oldestFiles[0]) break;
await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
process.env.DIRSIZECACHE -= oldestFiles[0].size;
process.env.DIRSIZECACHE = parseInt(process.env.DIRSIZECACHE) - oldestFiles[0].size;
logger.log(`Removed oldest image file: ${oldestFiles[0].name}`);
oldestFiles.shift();
}