From a0b3bb21177d21d91974b40aedd6de9ba53615bd Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 25 Jan 2023 14:45:39 +0000 Subject: [PATCH] good cleanup --- .../backend/src/server/FileServerService.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/server/FileServerService.ts b/packages/backend/src/server/FileServerService.ts index e99a633fcd..15722b1e31 100644 --- a/packages/backend/src/server/FileServerService.ts +++ b/packages/backend/src/server/FileServerService.ts @@ -167,6 +167,12 @@ export class FileServerService { }; const image = await convertFile(); + + if (typeof image.data === 'object' && 'pipe' in image.data && typeof image.data.pipe === 'function') { + image.data.on('end', file.cleanup); + image.data.on('close', file.cleanup); + } + reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream'); reply.header('Cache-Control', 'max-age=31536000, immutable'); return image.data; @@ -190,8 +196,9 @@ export class FileServerService { reply.header('Content-Disposition', contentDisposition('inline', file.file.name)); return stream; } - } finally { + } catch (e) { if ('cleanup' in file) file.cleanup(); + throw e; } } @@ -297,11 +304,17 @@ export class FileServerService { }; } + if (typeof image.data === 'object' && 'pipe' in image.data && typeof image.data.pipe === 'function' && 'cleanup' in file) { + image.data.on('end', file.cleanup); + image.data.on('close', file.cleanup); + } + reply.header('Content-Type', image.type); reply.header('Cache-Control', 'max-age=31536000, immutable'); return image.data; - } finally { + } catch (e) { if ('cleanup' in file) file.cleanup(); + throw e; } }