Browse Source

Improve file download handling with streaming and error management

master
Atanner 8 months ago
parent
commit
01a0ecb642
  1. 15
      index.js

15
index.js

@ -66,10 +66,23 @@ app.post("/convert", upload.fields([{ name: "audio" }, { name: "image" }]), asyn
} }
console.log("✅ Conversion complete. Sending file..."); 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(audioPath);
fs.unlinkSync(imagePath); fs.unlinkSync(imagePath);
fs.unlinkSync(output); 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.");
}); });
}); });
}); });

Loading…
Cancel
Save