|
const express = require("express"); |
|
const axios = require("axios"); |
|
const fs = require("fs"); |
|
const path = require("path"); |
|
const https = require("https"); |
|
const { exec } = require("child_process"); |
|
|
|
const app = express(); |
|
const PORT = 7860; |
|
const DOWNLOAD_DIR = path.join(__dirname, "downloads"); |
|
|
|
|
|
if (!fs.existsSync(DOWNLOAD_DIR)) { |
|
fs.mkdirSync(DOWNLOAD_DIR, { recursive: true }); |
|
} |
|
|
|
|
|
const axiosInstance = axios.create({ |
|
httpsAgent: new https.Agent({ rejectUnauthorized: false }) |
|
}); |
|
|
|
|
|
const getFileName = (fileUrl, headers) => { |
|
let filename = path.basename(new URL(fileUrl).pathname); |
|
|
|
if (headers["content-disposition"]) { |
|
const match = headers["content-disposition"].match(/filename="(.+)"/); |
|
if (match) { |
|
filename = match[1]; |
|
} |
|
} |
|
|
|
return filename.replace(/[<>:"/\\|?*]+/g, ""); |
|
}; |
|
|
|
|
|
const scheduleFileDeletion = (filePath, delay = 600000) => { |
|
setTimeout(() => { |
|
fs.unlink(filePath, (err) => { |
|
if (!err) { |
|
console.log(`β
Deleted file: ${filePath}`); |
|
} |
|
}); |
|
}, delay); |
|
}; |
|
|
|
|
|
const convertToMp4 = (inputPath, outputPath) => { |
|
return new Promise((resolve, reject) => { |
|
const command = `ffmpeg -i "${inputPath}" -c:v copy -c:a aac -strict experimental "${outputPath}"`; |
|
exec(command, (error, stdout, stderr) => { |
|
if (error) { |
|
console.error(`β FFmpeg error: ${error.message}`); |
|
reject(error); |
|
} else { |
|
console.log(`β
Conversion complete: ${outputPath}`); |
|
resolve(outputPath); |
|
} |
|
}); |
|
}); |
|
}; |
|
|
|
|
|
app.get("/download", async (req, res) => { |
|
const fileUrl = req.query.url; |
|
if (!fileUrl) { |
|
return res.status(400).json({ error: "β Missing URL parameter" }); |
|
} |
|
|
|
try { |
|
console.log(`β¬οΈ Downloading: ${fileUrl}`); |
|
|
|
|
|
const response = await axiosInstance({ |
|
url: fileUrl, |
|
method: "GET", |
|
responseType: "stream", |
|
headers: { |
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
|
"Accept": "*/*" |
|
} |
|
}); |
|
|
|
|
|
const originalFilename = getFileName(fileUrl, response.headers); |
|
const originalFilePath = path.join(DOWNLOAD_DIR, originalFilename); |
|
|
|
|
|
const isMkv = originalFilename.toLowerCase().endsWith(".mkv"); |
|
const mp4Filename = isMkv ? originalFilename.replace(/\.mkv$/, ".mp4") : originalFilename; |
|
const mp4FilePath = path.join(DOWNLOAD_DIR, mp4Filename); |
|
|
|
|
|
const writer = fs.createWriteStream(originalFilePath); |
|
response.data.pipe(writer); |
|
|
|
writer.on("finish", async () => { |
|
|
|
const hostUrl = `${req.protocol}://${req.get("host")}`; |
|
|
|
if (isMkv) { |
|
|
|
try { |
|
await convertToMp4(originalFilePath, mp4FilePath); |
|
fs.unlinkSync(originalFilePath); |
|
} catch (error) { |
|
return res.status(500).json({ error: "β Conversion failed" }); |
|
} |
|
} |
|
|
|
const servedUrl = `${hostUrl}/files/${mp4Filename}`; |
|
scheduleFileDeletion(mp4FilePath); |
|
|
|
console.log(`β
Download & conversion complete: ${servedUrl}`); |
|
return res.json({ message: "Download & conversion complete", fileUrl: servedUrl }); |
|
}); |
|
|
|
writer.on("error", (err) => { |
|
console.error("β Error writing file:", err); |
|
return res.status(500).json({ error: "Error saving file" }); |
|
}); |
|
|
|
} catch (error) { |
|
console.error("β Download error:", error.message); |
|
return res.status(500).json({ error: "Failed to download file" }); |
|
} |
|
}); |
|
|
|
|
|
app.use("/files", express.static(DOWNLOAD_DIR)); |
|
|
|
|
|
app.listen(PORT, () => { |
|
console.log(`π Server running on port ${PORT}`); |
|
}); |