diff --git a/src/media/server.ts b/src/media/server.ts index ad830da32..d56af9ea9 100644 --- a/src/media/server.ts +++ b/src/media/server.ts @@ -42,9 +42,11 @@ export function attachMediaRoutes( if (mime) res.type(mime); res.send(data); // best-effort single-use cleanup after response ends - setTimeout(() => { - fs.rm(realPath).catch(() => {}); - }, 500); + res.on("finish", () => { + setTimeout(() => { + fs.rm(realPath).catch(() => {}); + }, 50); + }); } catch { res.status(404).send("not found"); }