server: tempFileUtil: ignore temp file deletion errors

This commit is contained in:
Jesse Chan
2020-10-20 23:51:07 +08:00
parent 6d4a546690
commit 1168925f2e
+15 -2
View File
@@ -13,6 +13,19 @@ const getTempFilePath = (extension = 'tmp'): string => {
return getTempPath(`${Date.now()}-${crypto.randomBytes(8).toString('hex')}.${extension}`);
};
/**
* Deletes the file after 5 minutes
*/
const delayedDelete = (tempPath: string): void => {
setTimeout(() => {
try {
fs.unlinkSync(tempPath);
} catch (_e) {
// do nothing.
}
}, 1000 * 60 * 5);
};
/**
* Saves buffer to temporary storage as a file.
*
@@ -25,7 +38,7 @@ export const saveBufferToTempFile = async (buffer: Buffer, extension?: string):
fs.writeFileSync(tempPath, buffer);
setTimeout(() => fs.unlinkSync(tempPath), 1000 * 60 * 5);
delayedDelete(tempPath);
return tempPath;
};
@@ -52,7 +65,7 @@ export const fetchURLToTempFile = async (url: string, cookies?: Array<string>, e
: undefined,
}).then(
(res: AxiosResponse) => {
setTimeout(() => fs.unlinkSync(tempPath), 1000 * 60 * 5);
delayedDelete(tempPath);
res.data.pipe(fs.createWriteStream(tempPath)).on('finish', () => resolve(tempPath));
},
(e: AxiosError) => {