Update server.js
Browse files
server.js
CHANGED
@@ -5,8 +5,9 @@ const path = require("path");
|
|
5 |
const https = require("https");
|
6 |
const ffmpeg = require("fluent-ffmpeg");
|
7 |
const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg");
|
|
|
8 |
|
9 |
-
// Set FFmpeg path
|
10 |
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
|
11 |
|
12 |
const app = express();
|
@@ -18,26 +19,17 @@ if (!fs.existsSync(DOWNLOAD_DIR)) {
|
|
18 |
fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
|
19 |
}
|
20 |
|
21 |
-
// Axios instance to ignore SSL
|
22 |
const axiosInstance = axios.create({
|
23 |
httpsAgent: new https.Agent({ rejectUnauthorized: false })
|
24 |
});
|
25 |
|
26 |
-
// Function to
|
27 |
-
const
|
28 |
-
|
29 |
-
|
30 |
-
if (headers["content-disposition"]) {
|
31 |
-
const match = headers["content-disposition"].match(/filename="(.+)"/);
|
32 |
-
if (match) {
|
33 |
-
filename = match[1];
|
34 |
-
}
|
35 |
-
}
|
36 |
-
|
37 |
-
return filename.replace(/[<>:"/\\|?*]+/g, ""); // Remove invalid characters
|
38 |
};
|
39 |
|
40 |
-
// Function to delete file after a delay (default: 10 minutes)
|
41 |
const scheduleFileDeletion = (filePath, delay = 600000) => {
|
42 |
setTimeout(() => {
|
43 |
fs.unlink(filePath, (err) => {
|
@@ -48,7 +40,7 @@ const scheduleFileDeletion = (filePath, delay = 600000) => {
|
|
48 |
}, delay);
|
49 |
};
|
50 |
|
51 |
-
// Function to convert MKV to MP4
|
52 |
const convertToMp4 = (inputPath, outputPath) => {
|
53 |
return new Promise((resolve, reject) => {
|
54 |
ffmpeg(inputPath)
|
@@ -86,11 +78,12 @@ app.get("/download", async (req, res) => {
|
|
86 |
}
|
87 |
});
|
88 |
|
89 |
-
|
|
|
|
|
90 |
const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename);
|
91 |
|
92 |
-
const
|
93 |
-
const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename;
|
94 |
const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
|
95 |
|
96 |
const writer = fs.createWriteStream(originalFilePath);
|
@@ -127,7 +120,7 @@ app.get("/download", async (req, res) => {
|
|
127 |
}
|
128 |
});
|
129 |
|
130 |
-
// Serve files from downloads directory
|
131 |
app.use("/files", express.static(DOWNLOAD_DIR));
|
132 |
|
133 |
// Start the server
|
|
|
5 |
const https = require("https");
|
6 |
const ffmpeg = require("fluent-ffmpeg");
|
7 |
const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg");
|
8 |
+
const crypto = require("crypto"); // For generating random filenames
|
9 |
|
10 |
+
// Set FFmpeg path
|
11 |
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
|
12 |
|
13 |
const app = express();
|
|
|
19 |
fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
|
20 |
}
|
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 filename
|
28 |
+
const generateUniqueFilename = (extension = "") => {
|
29 |
+
return crypto.randomBytes(10).toString("hex") + extension;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
};
|
31 |
|
32 |
+
// Function to delete a file after a delay (default: 10 minutes)
|
33 |
const scheduleFileDeletion = (filePath, delay = 600000) => {
|
34 |
setTimeout(() => {
|
35 |
fs.unlink(filePath, (err) => {
|
|
|
40 |
}, delay);
|
41 |
};
|
42 |
|
43 |
+
// Function to convert MKV to MP4
|
44 |
const convertToMp4 = (inputPath, outputPath) => {
|
45 |
return new Promise((resolve, reject) => {
|
46 |
ffmpeg(inputPath)
|
|
|
78 |
}
|
79 |
});
|
80 |
|
81 |
+
// Generate a unique filename
|
82 |
+
const isMkv = fileUrl.toLowerCase().endsWith(".mkv");
|
83 |
+
const originalFilename = generateUniqueFilename(isMkv ? ".mkv" : ".mp4");
|
84 |
const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename);
|
85 |
|
86 |
+
const mp4Filename = originalFilename.replace(".mkv", ".mp4");
|
|
|
87 |
const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
|
88 |
|
89 |
const writer = fs.createWriteStream(originalFilePath);
|
|
|
120 |
}
|
121 |
});
|
122 |
|
123 |
+
// Serve files from downloads directory
|
124 |
app.use("/files", express.static(DOWNLOAD_DIR));
|
125 |
|
126 |
// Start the server
|