Reaperxxxx commited on
Commit
de32a43
Β·
verified Β·
1 Parent(s): 5b152aa

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +27 -5
server.js CHANGED
@@ -21,7 +21,8 @@ if (!fs.existsSync(DOWNLOAD_DIR)) {
21
 
22
  // Axios instance to ignore SSL verification
23
  const axiosInstance = axios.create({
24
- httpsAgent: new https.Agent({ rejectUnauthorized: false })
 
25
  });
26
 
27
  // Function to generate a unique request ID
@@ -40,18 +41,27 @@ const isValidUrl = (url) => {
40
  // Function to convert MKV to MP4
41
  const convertToMp4 = (inputPath, outputPath) => {
42
  return new Promise((resolve, reject) => {
43
- ffmpeg(inputPath)
44
  .output(outputPath)
45
  .outputOptions(["-c:v copy", "-c:a aac", "-strict experimental"])
46
  .on("end", () => {
47
  console.log(`βœ… Conversion complete: ${outputPath}`);
 
48
  resolve(outputPath);
49
  })
50
  .on("error", (err) => {
51
  console.error(`❌ FFmpeg conversion error: ${err.message}`);
 
52
  reject(err);
53
  })
54
  .run();
 
 
 
 
 
 
 
55
  });
56
  };
57
 
@@ -97,9 +107,18 @@ app.get("/download", async (req, res) => {
97
  const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
98
 
99
  const writer = fs.createWriteStream(originalFilePath);
 
 
 
 
 
 
 
 
100
  response.data.pipe(writer);
101
 
102
  writer.on("close", async () => {
 
103
  const hostUrl = `${req.protocol}://${req.get("host")}`;
104
 
105
  if (isMkv) {
@@ -133,7 +152,10 @@ app.get("/download", async (req, res) => {
133
  // Serve files from downloads directory
134
  app.use("/files", express.static(DOWNLOAD_DIR));
135
 
136
- // Start the server
137
- app.listen(PORT, () => {
138
  console.log(`πŸš€ Server running on port ${PORT}`);
139
- });
 
 
 
 
21
 
22
  // Axios instance to ignore SSL verification
23
  const axiosInstance = axios.create({
24
+ httpsAgent: new https.Agent({ rejectUnauthorized: false }),
25
+ timeout: 300000 // 5 minutes timeout
26
  });
27
 
28
  // Function to generate a unique request ID
 
41
  // Function to convert MKV to MP4
42
  const convertToMp4 = (inputPath, outputPath) => {
43
  return new Promise((resolve, reject) => {
44
+ const ffmpegProcess = ffmpeg(inputPath)
45
  .output(outputPath)
46
  .outputOptions(["-c:v copy", "-c:a aac", "-strict experimental"])
47
  .on("end", () => {
48
  console.log(`βœ… Conversion complete: ${outputPath}`);
49
+ clearTimeout(timeout); // Clear timeout when done
50
  resolve(outputPath);
51
  })
52
  .on("error", (err) => {
53
  console.error(`❌ FFmpeg conversion error: ${err.message}`);
54
+ clearTimeout(timeout);
55
  reject(err);
56
  })
57
  .run();
58
+
59
+ // Set a timeout to kill FFmpeg if it hangs
60
+ const timeout = setTimeout(() => {
61
+ console.error(`❌ FFmpeg conversion timed out! Killing process.`);
62
+ ffmpegProcess.kill("SIGKILL");
63
+ reject(new Error("❌ Conversion timeout"));
64
+ }, 600000); // 10 minutes timeout
65
  });
66
  };
67
 
 
107
  const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
108
 
109
  const writer = fs.createWriteStream(originalFilePath);
110
+
111
+ // Handle stream timeout manually
112
+ const streamTimeout = setTimeout(() => {
113
+ console.error(`❌ [${requestId}] Writing file took too long!`);
114
+ writer.destroy();
115
+ return res.status(500).json({ error: "❌ File write timeout" });
116
+ }, 300000); // 5 minutes timeout
117
+
118
  response.data.pipe(writer);
119
 
120
  writer.on("close", async () => {
121
+ clearTimeout(streamTimeout); // Clear timeout when done
122
  const hostUrl = `${req.protocol}://${req.get("host")}`;
123
 
124
  if (isMkv) {
 
152
  // Serve files from downloads directory
153
  app.use("/files", express.static(DOWNLOAD_DIR));
154
 
155
+ // Start the server with an extended timeout
156
+ const server = app.listen(PORT, () => {
157
  console.log(`πŸš€ Server running on port ${PORT}`);
158
+ });
159
+
160
+ // Increase Express server timeout to 10 minutes
161
+ server.timeout = 600000;