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,15 +115,16 @@ 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 |
// Serve files from downloads directory
|
|
|
94 |
return false;
|
95 |
};
|
96 |
|
97 |
+
// **API Route - Responds Immediately, Processes in Background**
|
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 |
+
// **Respond to the client immediately**
|
119 |
+
res.json({ message: "Processing in background", fileUrl: servedUrl });
|
120 |
|
121 |
+
// **Background Processing (Download)**
|
122 |
+
(async () => {
|
123 |
+
const success = await downloadFile(fileUrl, filePath, requestId);
|
124 |
+
if (success) {
|
125 |
+
scheduleFileDeletion(filePath);
|
126 |
+
}
|
127 |
+
})();
|
128 |
});
|
129 |
|
130 |
// Serve files from downloads directory
|