Reaperxxxx commited on
Commit
c178ebc
·
verified ·
1 Parent(s): 704cb8b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +9 -10
server.js CHANGED
@@ -94,7 +94,7 @@ const downloadFile = async (fileUrl, filePath, requestId) => {
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,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
- // **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
 
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