diff --git a/server/util/tempFileUtil.ts b/server/util/tempFileUtil.ts index 1965712b..79cd22d8 100644 --- a/server/util/tempFileUtil.ts +++ b/server/util/tempFileUtil.ts @@ -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, 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) => {