Reaperxxxx commited on
Commit
fb8af9a
Β·
verified Β·
1 Parent(s): 78d43ad

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +77 -55
server.js CHANGED
@@ -1,7 +1,15 @@
 
 
1
  const express = require("express");
 
2
  const fs = require("fs");
3
  const path = require("path");
4
- const { exec } = require("child_process");
 
 
 
 
 
5
 
6
  const app = express();
7
  const PORT = 7860;
@@ -12,8 +20,24 @@ if (!fs.existsSync(DOWNLOAD_DIR)) {
12
  fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
13
  }
14
 
15
- // Function to extract filename from URL
16
- const getFileName = (fileUrl) => path.basename(new URL(fileUrl).pathname);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  // Function to delete file after a delay (default: 10 minutes)
19
  const scheduleFileDeletion = (filePath, delay = 600000) => {
@@ -26,39 +50,21 @@ const scheduleFileDeletion = (filePath, delay = 600000) => {
26
  }, delay);
27
  };
28
 
29
- // Function to download file using aria2c (super fast)
30
-
31
- const downloadFile = (fileUrl, filePath) => {
32
- return new Promise((resolve, reject) => {
33
- const aria2Command = `aria2c -x 16 -s 16 -k 1M --check-certificate=false -o "${filePath}" "${fileUrl}"`;
34
- console.log(`πŸš€ Starting fast download: ${aria2Command}`);
35
-
36
- exec(aria2Command, (error, stdout, stderr) => {
37
- if (error) {
38
- console.error(`❌ Aria2 Error: ${error.message}`);
39
- console.error(`πŸ”΄ STDERR: ${stderr}`);
40
- return reject(error);
41
- }
42
- console.log(`βœ… Aria2 Output: ${stdout || stderr}`);
43
- resolve(filePath);
44
- });
45
- });
46
- };
47
-
48
- // Function to convert MKV to MP4 using FFmpeg (Fast Conversion)
49
  const convertToMp4 = (inputPath, outputPath) => {
50
  return new Promise((resolve, reject) => {
51
- const ffmpegCmd = `ffmpeg -i "${inputPath}" -c:v copy -c:a aac -b:a 192k -y "${outputPath}"`;
52
- console.log(`πŸš€ Running FFmpeg: ${ffmpegCmd}`);
53
-
54
- exec(ffmpegCmd, (error, stdout, stderr) => {
55
- if (error) {
56
- console.error(`❌ FFmpeg Error: ${error.message}`);
57
- return reject(error);
58
- }
59
- console.log(`βœ… FFmpeg Output: ${stdout || stderr}`);
60
- resolve(outputPath);
61
- });
 
62
  });
63
  };
64
 
@@ -72,38 +78,54 @@ app.get("/download", async (req, res) => {
72
  try {
73
  console.log(`⬇️ Downloading: ${fileUrl}`);
74
 
75
- const originalFilename = getFileName(fileUrl);
 
 
 
 
 
 
 
 
 
 
76
  const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename);
77
 
78
  const isMkv = originalFilename.toLowerCase().endsWith(".mkv");
79
  const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename;
80
  const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
81
 
82
- // Start fast download with aria2c
83
- await downloadFile(fileUrl, originalFilePath);
84
-
85
- // Dynamically get the host URL from request headers
86
- const hostUrl = `${req.protocol}://${req.get("host")}`;
87
-
88
- if (isMkv) {
89
- // Convert MKV to MP4
90
- try {
91
- await convertToMp4(originalFilePath, mp4FilePath);
92
- fs.unlinkSync(originalFilePath); // Remove original MKV after conversion
93
- } catch (error) {
94
- return res.status(500).json({ error: "❌ Conversion failed" });
 
95
  }
96
- }
97
 
98
- const servedUrl = `${hostUrl}/files/${mp4Filename}`;
99
- scheduleFileDeletion(mp4FilePath); // Auto-delete after 10 minutes
100
 
101
- console.log(`βœ… Download & conversion complete: ${servedUrl}`);
102
- return res.json({ message: "Download & conversion complete", fileUrl: servedUrl });
 
 
 
 
 
 
103
 
104
  } catch (error) {
105
- console.error("❌ Error:", error.message);
106
- return res.status(500).json({ error: "Download or conversion failed" });
107
  }
108
  });
109
 
 
1
+ It’s way way way way toooo slowwwwwww
2
+
3
  const express = require("express");
4
+ const axios = require("axios");
5
  const fs = require("fs");
6
  const path = require("path");
7
+ const https = require("https");
8
+ const ffmpeg = require("fluent-ffmpeg");
9
+ const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg");
10
+
11
+ // Set FFmpeg path from npm package
12
+ ffmpeg.setFfmpegPath(ffmpegInstaller.path);
13
 
14
  const app = express();
15
  const PORT = 7860;
 
20
  fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
21
  }
22
 
23
+ // Axios instance to ignore SSL certificate verification
24
+ const axiosInstance = axios.create({
25
+ httpsAgent: new https.Agent({ rejectUnauthorized: false })
26
+ });
27
+
28
+ // Function to extract filename from URL or headers
29
+ const getFileName = (fileUrl, headers) => {
30
+ let filename = path.basename(new URL(fileUrl).pathname);
31
+
32
+ if (headers["content-disposition"]) {
33
+ const match = headers["content-disposition"].match(/filename="(.+)"/);
34
+ if (match) {
35
+ filename = match[1];
36
+ }
37
+ }
38
+
39
+ return filename.replace(/[<>:"/\\|?*]+/g, ""); // Remove invalid characters
40
+ };
41
 
42
  // Function to delete file after a delay (default: 10 minutes)
43
  const scheduleFileDeletion = (filePath, delay = 600000) => {
 
50
  }, delay);
51
  };
52
 
53
+ // Function to convert MKV to MP4 using fluent-ffmpeg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  const convertToMp4 = (inputPath, outputPath) => {
55
  return new Promise((resolve, reject) => {
56
+ ffmpeg(inputPath)
57
+ .output(outputPath)
58
+ .outputOptions(["-c:v copy", "-c:a aac", "-strict experimental"]) // Fast conversion
59
+ .on("end", () => {
60
+ console.log(`βœ… Conversion complete: ${outputPath}`);
61
+ resolve(outputPath);
62
+ })
63
+ .on("error", (err) => {
64
+ console.error(`❌ FFmpeg conversion error: ${err.message}`);
65
+ reject(err);
66
+ })
67
+ .run();
68
  });
69
  };
70
 
 
78
  try {
79
  console.log(`⬇️ Downloading: ${fileUrl}`);
80
 
81
+ const response = await axiosInstance({
82
+ url: fileUrl,
83
+ method: "GET",
84
+ responseType: "stream",
85
+ headers: {
86
+ "User-Agent": "Mozilla/5.0",
87
+ "Accept": "*/*"
88
+ }
89
+ });
90
+
91
+ const originalFilename = getFileName(fileUrl, response.headers);
92
  const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename);
93
 
94
  const isMkv = originalFilename.toLowerCase().endsWith(".mkv");
95
  const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename;
96
  const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
97
 
98
+ const writer = fs.createWriteStream(originalFilePath);
99
+ response.data.pipe(writer);
100
+
101
+ writer.on("close", async () => {
102
+ const hostUrl = `${req.protocol}://${req.get("host")}`;
103
+
104
+ if (isMkv) {
105
+ try {
106
+ await convertToMp4(originalFilePath, mp4FilePath);
107
+ fs.unlinkSync(originalFilePath); // Remove MKV file
108
+ } catch (error) {
109
+ console.error("❌ Conversion error:", error.message);
110
+ return res.status(500).json({ error: "❌ Conversion failed" });
111
+ }
112
  }
 
113
 
114
+ const servedUrl = `${hostUrl}/files/${mp4Filename}`;
115
+ scheduleFileDeletion(mp4FilePath);
116
 
117
+ console.log(`βœ… Download & conversion complete: ${servedUrl}`);
118
+ res.json({ message: "Download & conversion complete", fileUrl: servedUrl });
119
+ });
120
+
121
+ writer.on("error", (err) => {
122
+ console.error("❌ Error writing file:", err);
123
+ res.status(500).json({ error: "Error saving file" });
124
+ });
125
 
126
  } catch (error) {
127
+ console.error("❌ Download error:", error.message);
128
+ res.status(500).json({ error: "Failed to download file" });
129
  }
130
  });
131