Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const axios = require("axios");
|
| 3 |
+
const fs = require("fs");
|
| 4 |
+
const path = require("path");
|
| 5 |
+
const progress = require("progress-stream");
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const PORT = 7860;
|
| 9 |
+
const DOWNLOAD_DIR = path.join(__dirname, "downloads");
|
| 10 |
+
|
| 11 |
+
// Ensure the downloads directory exists
|
| 12 |
+
if (!fs.existsSync(DOWNLOAD_DIR)) {
|
| 13 |
+
fs.mkdirSync(DOWNLOAD_DIR);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// Function to format bytes into human-readable size
|
| 17 |
+
function formatBytes(bytes, decimals = 2) {
|
| 18 |
+
if (bytes === 0) return "0 Bytes";
|
| 19 |
+
const k = 1024;
|
| 20 |
+
const dm = decimals < 0 ? 0 : decimals;
|
| 21 |
+
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
| 22 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 23 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// API Route to download and serve .mkv files
|
| 27 |
+
app.get("/download", async (req, res) => {
|
| 28 |
+
const fileUrl = req.query.url;
|
| 29 |
+
if (!fileUrl) {
|
| 30 |
+
return res.status(400).json({ error: "Missing URL parameter" });
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
try {
|
| 34 |
+
// Get filename from URL
|
| 35 |
+
const filename = path.basename(new URL(fileUrl).pathname);
|
| 36 |
+
const filePath = path.join(DOWNLOAD_DIR, filename);
|
| 37 |
+
|
| 38 |
+
// Start the download request
|
| 39 |
+
const response = await axios({
|
| 40 |
+
url: fileUrl,
|
| 41 |
+
method: "GET",
|
| 42 |
+
responseType: "stream"
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
// Get file size from headers
|
| 46 |
+
const totalSize = parseInt(response.headers["content-length"], 10);
|
| 47 |
+
console.log(`Downloading: ${filename} (${formatBytes(totalSize)})`);
|
| 48 |
+
|
| 49 |
+
// Progress tracking
|
| 50 |
+
const progressStream = progress({
|
| 51 |
+
length: totalSize,
|
| 52 |
+
time: 1000 // Update every second
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
progressStream.on("progress", (progress) => {
|
| 56 |
+
const speed = formatBytes(progress.speed) + "/s";
|
| 57 |
+
const downloaded = formatBytes(progress.transferred);
|
| 58 |
+
const percent = progress.percentage.toFixed(2);
|
| 59 |
+
const timeLeft = progress.eta ? progress.eta.toFixed(2) + "s" : "Calculating...";
|
| 60 |
+
console.log(`Progress: ${percent}% | Speed: ${speed} | Downloaded: ${downloaded} | Time Left: ${timeLeft}`);
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
// Pipe the download stream through progress tracking and into a file
|
| 64 |
+
const writer = fs.createWriteStream(filePath);
|
| 65 |
+
response.data.pipe(progressStream).pipe(writer);
|
| 66 |
+
|
| 67 |
+
writer.on("finish", () => {
|
| 68 |
+
const fileUrl = `http://localhost:${PORT}/files/${filename}`;
|
| 69 |
+
console.log(`Download complete: ${fileUrl}`);
|
| 70 |
+
|
| 71 |
+
// Schedule file deletion after 10 minutes (600,000 ms)
|
| 72 |
+
setTimeout(() => {
|
| 73 |
+
fs.unlink(filePath, (err) => {
|
| 74 |
+
if (err) {
|
| 75 |
+
console.error(`Error deleting file ${filename}:`, err);
|
| 76 |
+
} else {
|
| 77 |
+
console.log(`File ${filename} deleted after 10 minutes.`);
|
| 78 |
+
}
|
| 79 |
+
});
|
| 80 |
+
}, 600000); // 10 minutes
|
| 81 |
+
|
| 82 |
+
return res.json({ message: "Download complete", fileUrl });
|
| 83 |
+
});
|
| 84 |
+
|
| 85 |
+
writer.on("error", (err) => {
|
| 86 |
+
console.error("Error writing file:", err);
|
| 87 |
+
return res.status(500).json({ error: "Error saving file" });
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
} catch (error) {
|
| 91 |
+
console.error("Download error:", error);
|
| 92 |
+
return res.status(500).json({ error: "Failed to download file" });
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
// Serve files from downloads directory
|
| 97 |
+
app.use("/files", express.static(DOWNLOAD_DIR));
|
| 98 |
+
|
| 99 |
+
// Start the server
|
| 100 |
+
app.listen(PORT, () => {
|
| 101 |
+
console.log(`Server running on http://localhost:${PORT}`);
|
| 102 |
+
});
|