Update server.js
Browse files
server.js
CHANGED
|
@@ -3,13 +3,8 @@ const axios = require("axios");
|
|
| 3 |
const fs = require("fs");
|
| 4 |
const path = require("path");
|
| 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();
|
| 14 |
const PORT = 7860;
|
| 15 |
const DOWNLOAD_DIR = path.join(__dirname, "downloads");
|
|
@@ -38,41 +33,6 @@ const isValidUrl = (url) => {
|
|
| 38 |
}
|
| 39 |
};
|
| 40 |
|
| 41 |
-
// Function to convert MKV to MP4 without re-encoding video/audio
|
| 42 |
-
const convertToMp4 = (inputPath, outputPath) => {
|
| 43 |
-
return new Promise((resolve, reject) => {
|
| 44 |
-
const ffmpegProcess = ffmpeg(inputPath)
|
| 45 |
-
.output(outputPath)
|
| 46 |
-
.outputOptions([
|
| 47 |
-
"-c:v copy", // Copy video without re-encoding
|
| 48 |
-
"-c:a aac", // Ensure audio is converted to AAC
|
| 49 |
-
"-b:a 192k", // Set audio bitrate
|
| 50 |
-
"-strict experimental"
|
| 51 |
-
])
|
| 52 |
-
.on("start", () => {
|
| 53 |
-
console.log(`π Starting conversion: ${inputPath} β ${outputPath}`);
|
| 54 |
-
})
|
| 55 |
-
.on("end", () => {
|
| 56 |
-
console.log(`β
Conversion complete: ${outputPath}`);
|
| 57 |
-
clearTimeout(timeout);
|
| 58 |
-
resolve(outputPath);
|
| 59 |
-
})
|
| 60 |
-
.on("error", (err) => {
|
| 61 |
-
console.error(`β FFmpeg error: ${err.message}`);
|
| 62 |
-
clearTimeout(timeout);
|
| 63 |
-
reject(err);
|
| 64 |
-
})
|
| 65 |
-
.run();
|
| 66 |
-
|
| 67 |
-
// Set a timeout to kill FFmpeg if it hangs
|
| 68 |
-
const timeout = setTimeout(() => {
|
| 69 |
-
console.error(`β FFmpeg conversion timed out! Killing process.`);
|
| 70 |
-
ffmpegProcess.kill("SIGKILL");
|
| 71 |
-
reject(new Error("β Conversion timeout"));
|
| 72 |
-
}, 600000); // 10 minutes timeout
|
| 73 |
-
});
|
| 74 |
-
};
|
| 75 |
-
|
| 76 |
// Function to delete files after a delay (default: 10 minutes)
|
| 77 |
const scheduleFileDeletion = (filePath, delay = 600000) => {
|
| 78 |
setTimeout(() => {
|
|
@@ -87,7 +47,7 @@ const scheduleFileDeletion = (filePath, delay = 600000) => {
|
|
| 87 |
// **API Route - Responds Immediately, Processes in Background**
|
| 88 |
app.get("/download", async (req, res) => {
|
| 89 |
const fileUrl = req.query.url;
|
| 90 |
-
|
| 91 |
// Validate URL before processing
|
| 92 |
if (!fileUrl || !isValidUrl(fileUrl)) {
|
| 93 |
return res.status(400).json({ error: "β Invalid or missing URL parameter" });
|
|
@@ -97,19 +57,18 @@ app.get("/download", async (req, res) => {
|
|
| 97 |
const requestId = generateRequestId();
|
| 98 |
console.log(`π₯ [${requestId}] Received request for: ${fileUrl}`);
|
| 99 |
|
| 100 |
-
|
| 101 |
-
const
|
| 102 |
-
const
|
| 103 |
-
const
|
| 104 |
-
const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename);
|
| 105 |
|
| 106 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
| 107 |
-
const servedUrl = `${hostUrl}/files/${
|
| 108 |
|
| 109 |
// **Respond to the client immediately**
|
| 110 |
res.json({ message: "Processing in background", fileUrl: servedUrl });
|
| 111 |
|
| 112 |
-
// **Background Processing (Download
|
| 113 |
(async () => {
|
| 114 |
try {
|
| 115 |
console.log(`β¬οΈ [${requestId}] Downloading: ${fileUrl}`);
|
|
@@ -121,7 +80,7 @@ app.get("/download", async (req, res) => {
|
|
| 121 |
headers: { "User-Agent": "Mozilla/5.0", "Accept": "*/*" }
|
| 122 |
});
|
| 123 |
|
| 124 |
-
const writer = fs.createWriteStream(
|
| 125 |
response.data.pipe(writer);
|
| 126 |
|
| 127 |
await new Promise((resolve, reject) => {
|
|
@@ -129,16 +88,10 @@ app.get("/download", async (req, res) => {
|
|
| 129 |
writer.on("error", reject);
|
| 130 |
});
|
| 131 |
|
| 132 |
-
console.log(`β
[${requestId}] Download complete: ${
|
| 133 |
-
|
| 134 |
-
if (isMkv) {
|
| 135 |
-
console.log(`π [${requestId}] Converting MKV to MP4...`);
|
| 136 |
-
await convertToMp4(originalFilePath, mp4FilePath);
|
| 137 |
-
fs.unlinkSync(originalFilePath); // Remove MKV file
|
| 138 |
-
console.log(`β
[${requestId}] Conversion complete: ${mp4FilePath}`);
|
| 139 |
-
}
|
| 140 |
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
} catch (error) {
|
| 144 |
console.error(`β [${requestId}] Processing error:`, error.message);
|
|
@@ -154,5 +107,5 @@ const server = app.listen(PORT, () => {
|
|
| 154 |
console.log(`π Server running on port ${PORT}`);
|
| 155 |
});
|
| 156 |
|
| 157 |
-
// Increase Express server timeout to
|
| 158 |
-
server.timeout =
|
|
|
|
| 3 |
const fs = require("fs");
|
| 4 |
const path = require("path");
|
| 5 |
const https = require("https");
|
|
|
|
|
|
|
| 6 |
const crypto = require("crypto"); // For generating random filenames
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
const app = express();
|
| 9 |
const PORT = 7860;
|
| 10 |
const DOWNLOAD_DIR = path.join(__dirname, "downloads");
|
|
|
|
| 33 |
}
|
| 34 |
};
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
// Function to delete files after a delay (default: 10 minutes)
|
| 37 |
const scheduleFileDeletion = (filePath, delay = 600000) => {
|
| 38 |
setTimeout(() => {
|
|
|
|
| 47 |
// **API Route - Responds Immediately, Processes in Background**
|
| 48 |
app.get("/download", async (req, res) => {
|
| 49 |
const fileUrl = req.query.url;
|
| 50 |
+
|
| 51 |
// Validate URL before processing
|
| 52 |
if (!fileUrl || !isValidUrl(fileUrl)) {
|
| 53 |
return res.status(400).json({ error: "β Invalid or missing URL parameter" });
|
|
|
|
| 57 |
const requestId = generateRequestId();
|
| 58 |
console.log(`π₯ [${requestId}] Received request for: ${fileUrl}`);
|
| 59 |
|
| 60 |
+
// Extract file extension from URL
|
| 61 |
+
const fileExt = path.extname(new URL(fileUrl).pathname) || ".dat"; // Default to ".dat" if no extension
|
| 62 |
+
const filename = `${requestId}${fileExt}`;
|
| 63 |
+
const filePath = path.join(DOWNLOAD_DIR, filename);
|
|
|
|
| 64 |
|
| 65 |
const hostUrl = `${req.protocol}://${req.get("host")}`;
|
| 66 |
+
const servedUrl = `${hostUrl}/files/${filename}`;
|
| 67 |
|
| 68 |
// **Respond to the client immediately**
|
| 69 |
res.json({ message: "Processing in background", fileUrl: servedUrl });
|
| 70 |
|
| 71 |
+
// **Background Processing (Download)**
|
| 72 |
(async () => {
|
| 73 |
try {
|
| 74 |
console.log(`β¬οΈ [${requestId}] Downloading: ${fileUrl}`);
|
|
|
|
| 80 |
headers: { "User-Agent": "Mozilla/5.0", "Accept": "*/*" }
|
| 81 |
});
|
| 82 |
|
| 83 |
+
const writer = fs.createWriteStream(filePath);
|
| 84 |
response.data.pipe(writer);
|
| 85 |
|
| 86 |
await new Promise((resolve, reject) => {
|
|
|
|
| 88 |
writer.on("error", reject);
|
| 89 |
});
|
| 90 |
|
| 91 |
+
console.log(`β
[${requestId}] Download complete: ${filePath}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
// Schedule deletion
|
| 94 |
+
scheduleFileDeletion(filePath);
|
| 95 |
|
| 96 |
} catch (error) {
|
| 97 |
console.error(`β [${requestId}] Processing error:`, error.message);
|
|
|
|
| 107 |
console.log(`π Server running on port ${PORT}`);
|
| 108 |
});
|
| 109 |
|
| 110 |
+
// Increase Express server timeout to 10 minutes
|
| 111 |
+
server.timeout = 600000;
|