Reaperxxxx commited on
Commit
ffe37d1
Β·
verified Β·
1 Parent(s): 9ff1078

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -11
server.js CHANGED
@@ -6,6 +6,7 @@ const https = require("https");
6
 
7
  const app = express();
8
  const PORT = 7860;
 
9
  const DOWNLOAD_DIR = path.join(__dirname, "downloads");
10
 
11
  // Ensure the downloads directory exists
@@ -13,6 +14,9 @@ if (!fs.existsSync(DOWNLOAD_DIR)) {
13
  fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
14
  }
15
 
 
 
 
16
  // Axios instance to ignore SSL certificate verification
17
  const axiosInstance = axios.create({
18
  httpsAgent: new https.Agent({ rejectUnauthorized: false })
@@ -28,16 +32,16 @@ const getFileName = (fileUrl, headers) => {
28
  filename = match[1];
29
  }
30
  }
31
-
32
  return filename.replace(/[<>:"/\\|?*]+/g, ""); // Remove invalid characters
33
  };
34
 
35
- // Function to delete file after a delay
36
- const scheduleFileDeletion = (filePath, delay = 600000) => { // 10 minutes
37
  setTimeout(() => {
38
  fs.unlink(filePath, (err) => {
39
  if (!err) {
40
- console.log(`Deleted file: ${filePath}`);
41
  }
42
  });
43
  }, delay);
@@ -47,11 +51,11 @@ const scheduleFileDeletion = (filePath, delay = 600000) => { // 10 minutes
47
  app.get("/download", async (req, res) => {
48
  const fileUrl = req.query.url;
49
  if (!fileUrl) {
50
- return res.status(400).json({ error: "Missing URL parameter" });
51
  }
52
 
53
  try {
54
- console.log(`Downloading: ${fileUrl}`);
55
 
56
  // Request file with forced download handling
57
  const response = await axiosInstance({
@@ -73,26 +77,27 @@ app.get("/download", async (req, res) => {
73
  response.data.pipe(writer);
74
 
75
  writer.on("finish", () => {
76
- const servedUrl = `http://localhost:${PORT}/files/${filename}`;
77
  scheduleFileDeletion(filePath); // Auto-delete after 10 minutes
 
78
  return res.json({ message: "Download complete", fileUrl: servedUrl });
79
  });
80
 
81
  writer.on("error", (err) => {
82
- console.error("Error writing file:", err);
83
  return res.status(500).json({ error: "Error saving file" });
84
  });
85
 
86
  } catch (error) {
87
- console.error("Download error:", error.message);
88
  return res.status(500).json({ error: "Failed to download file" });
89
  }
90
  });
91
 
92
- // Serve files from downloads directory
93
  app.use("/files", express.static(DOWNLOAD_DIR));
94
 
95
  // Start the server
96
  app.listen(PORT, () => {
97
- console.log(`Server running on http://localhost:${PORT}`);
98
  });
 
6
 
7
  const app = express();
8
  const PORT = 7860;
9
+ const HOST_URL = "https://your-domain.com"; // πŸ”Ή Change this to your actual domain
10
  const DOWNLOAD_DIR = path.join(__dirname, "downloads");
11
 
12
  // Ensure the downloads directory exists
 
14
  fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
15
  }
16
 
17
+ // Set correct permissions for the downloads directory
18
+ fs.chmodSync(DOWNLOAD_DIR, 0o777); // πŸ”Ή Allows read/write access
19
+
20
  // Axios instance to ignore SSL certificate verification
21
  const axiosInstance = axios.create({
22
  httpsAgent: new https.Agent({ rejectUnauthorized: false })
 
32
  filename = match[1];
33
  }
34
  }
35
+
36
  return filename.replace(/[<>:"/\\|?*]+/g, ""); // Remove invalid characters
37
  };
38
 
39
+ // Function to delete file after a delay (default: 10 minutes)
40
+ const scheduleFileDeletion = (filePath, delay = 600000) => {
41
  setTimeout(() => {
42
  fs.unlink(filePath, (err) => {
43
  if (!err) {
44
+ console.log(`βœ… Deleted file: ${filePath}`);
45
  }
46
  });
47
  }, delay);
 
51
  app.get("/download", async (req, res) => {
52
  const fileUrl = req.query.url;
53
  if (!fileUrl) {
54
+ return res.status(400).json({ error: "❌ Missing URL parameter" });
55
  }
56
 
57
  try {
58
+ console.log(`⬇️ Downloading: ${fileUrl}`);
59
 
60
  // Request file with forced download handling
61
  const response = await axiosInstance({
 
77
  response.data.pipe(writer);
78
 
79
  writer.on("finish", () => {
80
+ const servedUrl = `${HOST_URL}/files/${filename}`;
81
  scheduleFileDeletion(filePath); // Auto-delete after 10 minutes
82
+ console.log(`βœ… Download complete: ${servedUrl}`);
83
  return res.json({ message: "Download complete", fileUrl: servedUrl });
84
  });
85
 
86
  writer.on("error", (err) => {
87
+ console.error("❌ Error writing file:", err);
88
  return res.status(500).json({ error: "Error saving file" });
89
  });
90
 
91
  } catch (error) {
92
+ console.error("❌ Download error:", error.message);
93
  return res.status(500).json({ error: "Failed to download file" });
94
  }
95
  });
96
 
97
+ // Serve files from downloads directory (publicly accessible)
98
  app.use("/files", express.static(DOWNLOAD_DIR));
99
 
100
  // Start the server
101
  app.listen(PORT, () => {
102
+ console.log(`πŸš€ Server running on ${HOST_URL}:${PORT}`);
103
  });