Update server.js
Browse files
server.js
CHANGED
@@ -1,7 +1,15 @@
|
|
|
|
|
|
1 |
const express = require("express");
|
|
|
2 |
const fs = require("fs");
|
3 |
const path = require("path");
|
4 |
-
const
|
|
|
|
|
|
|
|
|
|
|
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 |
-
//
|
16 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
});
|
63 |
};
|
64 |
|
@@ -72,38 +78,54 @@ app.get("/download", async (req, res) => {
|
|
72 |
try {
|
73 |
console.log(`β¬οΈ Downloading: ${fileUrl}`);
|
74 |
|
75 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
95 |
}
|
96 |
-
}
|
97 |
|
98 |
-
|
99 |
-
|
100 |
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
} catch (error) {
|
105 |
-
console.error("β
|
106 |
-
|
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 |
|