Update server.js
Browse files
server.js
CHANGED
@@ -3,6 +3,7 @@ const axios = require("axios");
|
|
3 |
const fs = require("fs");
|
4 |
const path = require("path");
|
5 |
const https = require("https");
|
|
|
6 |
|
7 |
const app = express();
|
8 |
const PORT = 7860;
|
@@ -43,7 +44,23 @@ const scheduleFileDeletion = (filePath, delay = 600000) => {
|
|
43 |
}, delay);
|
44 |
};
|
45 |
|
46 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
app.get("/download", async (req, res) => {
|
48 |
const fileUrl = req.query.url;
|
49 |
if (!fileUrl) {
|
@@ -65,21 +82,37 @@ app.get("/download", async (req, res) => {
|
|
65 |
});
|
66 |
|
67 |
// Extract correct filename
|
68 |
-
const
|
69 |
-
const
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
// Pipe response stream to file
|
72 |
-
const writer = fs.createWriteStream(
|
73 |
response.data.pipe(writer);
|
74 |
|
75 |
-
writer.on("finish", () => {
|
76 |
// Dynamically get the host URL from request headers
|
77 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
78 |
-
const servedUrl = `${hostUrl}/files/${filename}`;
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
});
|
84 |
|
85 |
writer.on("error", (err) => {
|
|
|
3 |
const fs = require("fs");
|
4 |
const path = require("path");
|
5 |
const https = require("https");
|
6 |
+
const { exec } = require("child_process"); // For FFmpeg execution
|
7 |
|
8 |
const app = express();
|
9 |
const PORT = 7860;
|
|
|
44 |
}, delay);
|
45 |
};
|
46 |
|
47 |
+
// Function to convert MKV to MP4 using FFmpeg
|
48 |
+
const convertToMp4 = (inputPath, outputPath) => {
|
49 |
+
return new Promise((resolve, reject) => {
|
50 |
+
const command = `ffmpeg -i "${inputPath}" -c:v copy -c:a aac -strict experimental "${outputPath}"`;
|
51 |
+
exec(command, (error, stdout, stderr) => {
|
52 |
+
if (error) {
|
53 |
+
console.error(`β FFmpeg error: ${error.message}`);
|
54 |
+
reject(error);
|
55 |
+
} else {
|
56 |
+
console.log(`β
Conversion complete: ${outputPath}`);
|
57 |
+
resolve(outputPath);
|
58 |
+
}
|
59 |
+
});
|
60 |
+
});
|
61 |
+
};
|
62 |
+
|
63 |
+
// API Route to download, convert, and serve .mp4 files
|
64 |
app.get("/download", async (req, res) => {
|
65 |
const fileUrl = req.query.url;
|
66 |
if (!fileUrl) {
|
|
|
82 |
});
|
83 |
|
84 |
// Extract correct filename
|
85 |
+
const originalFilename = getFileName(fileUrl, response.headers);
|
86 |
+
const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename);
|
87 |
+
|
88 |
+
// Determine new filename if conversion is needed
|
89 |
+
const isMkv = originalFilename.toLowerCase().endsWith(".mkv");
|
90 |
+
const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename;
|
91 |
+
const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
|
92 |
|
93 |
// Pipe response stream to file
|
94 |
+
const writer = fs.createWriteStream(originalFilePath);
|
95 |
response.data.pipe(writer);
|
96 |
|
97 |
+
writer.on("finish", async () => {
|
98 |
// Dynamically get the host URL from request headers
|
99 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
|
|
100 |
|
101 |
+
if (isMkv) {
|
102 |
+
// Convert MKV to MP4
|
103 |
+
try {
|
104 |
+
await convertToMp4(originalFilePath, mp4FilePath);
|
105 |
+
fs.unlinkSync(originalFilePath); // Remove original MKV after conversion
|
106 |
+
} catch (error) {
|
107 |
+
return res.status(500).json({ error: "β Conversion failed" });
|
108 |
+
}
|
109 |
+
}
|
110 |
+
|
111 |
+
const servedUrl = `${hostUrl}/files/${mp4Filename}`;
|
112 |
+
scheduleFileDeletion(mp4FilePath); // Auto-delete after 10 minutes
|
113 |
+
|
114 |
+
console.log(`β
Download & conversion complete: ${servedUrl}`);
|
115 |
+
return res.json({ message: "Download & conversion complete", fileUrl: servedUrl });
|
116 |
});
|
117 |
|
118 |
writer.on("error", (err) => {
|