From 01a0ecb642e38197b3151ba02e1c7869f8701539 Mon Sep 17 00:00:00 2001 From: Atanner Date: Tue, 15 Apr 2025 02:27:59 -0600 Subject: [PATCH] Improve file download handling with streaming and error management --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8694e4c..7e522b6 100644 --- a/index.js +++ b/index.js @@ -66,10 +66,23 @@ app.post("/convert", upload.fields([{ name: "audio" }, { name: "image" }]), asyn } console.log("โœ… Conversion complete. Sending file..."); - res.download(output, () => { + res.setHeader('Content-Type', 'video/mp4'); + res.setHeader('Content-Disposition', `attachment; filename="${output}"`); + + const stream = fs.createReadStream(output); + + stream.pipe(res); + + stream.on('close', () => { fs.unlinkSync(audioPath); fs.unlinkSync(imagePath); fs.unlinkSync(output); + console.log("๐Ÿงน Cleaned up temp files"); + }); + + stream.on('error', (err) => { + console.error("โŒ Stream error:", err); + res.status(500).send("Error streaming file."); }); }); });