Update server.js
Browse files
server.js
CHANGED
|
@@ -94,7 +94,7 @@ const downloadFile = async (fileUrl, filePath, requestId) => {
|
|
| 94 |
return false;
|
| 95 |
};
|
| 96 |
|
| 97 |
-
// **API Route -
|
| 98 |
app.get("/download", async (req, res) => {
|
| 99 |
const fileUrl = req.query.url;
|
| 100 |
|
|
@@ -115,16 +115,15 @@ app.get("/download", async (req, res) => {
|
|
| 115 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
| 116 |
const servedUrl = `${hostUrl}/files/${filename}`;
|
| 117 |
|
| 118 |
-
// **
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
})();
|
| 128 |
});
|
| 129 |
|
| 130 |
// Serve files from downloads directory
|
|
|
|
| 94 |
return false;
|
| 95 |
};
|
| 96 |
|
| 97 |
+
// **API Route - Only Respond When Download is Done**
|
| 98 |
app.get("/download", async (req, res) => {
|
| 99 |
const fileUrl = req.query.url;
|
| 100 |
|
|
|
|
| 115 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
| 116 |
const servedUrl = `${hostUrl}/files/${filename}`;
|
| 117 |
|
| 118 |
+
// **Wait for download to complete before responding**
|
| 119 |
+
const success = await downloadFile(fileUrl, filePath, requestId);
|
| 120 |
|
| 121 |
+
if (success) {
|
| 122 |
+
scheduleFileDeletion(filePath);
|
| 123 |
+
res.json({ message: "✅ Download complete", fileUrl: servedUrl });
|
| 124 |
+
} else {
|
| 125 |
+
res.status(500).json({ error: "❌ Download failed" });
|
| 126 |
+
}
|
|
|
|
| 127 |
});
|
| 128 |
|
| 129 |
// Serve files from downloads directory
|