Update server.js
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879const express = require('express');const cors = require('cors');const { exec } = require('child_process');const path = require('path');const fs = require('fs');const app = express();app.use(cors({ origin: '*' }));app.use(express.json());const PORT = process.env.PORT || 7860;const DOWNLOAD_DIR = path.join(__dirname, 'downloads');// Buat folder download temporer jika belum adaif (!fs.existsSync(DOWNLOAD_DIR)) {fs.mkdirSync(DOWNLOAD_DIR);}console.log("Sistem Inisialisasi... Server Storage Bridge Siap.");app.post('/api/download', (req, res) => {console.log("=== MENERIMA PERMINTAAN UNDUHAN ===");const { url } = req.body;if (!url) {return res.status(400).json({ success: false, message: "URL wajib diisi!" });}// 1. Dapatkan judul video terlebih dahulu untuk penamaan log internalconst getTitleCmd = `yt-dlp --get-title --no-warnings "${url.trim()}"`;exec(getTitleCmd, (titleErr, titleStdout) => {const videoTitle = titleStdout ? titleStdout.trim().replace(/[/\\?%*:|"<>]/g, '-') : `video_${Date.now()}`;console.log(`Mulai mengunduh file fisik video: ${videoTitle}`);// 🔥 2. JALANKAN COMMAND ANDA SECARA LANGSUNG// Kita arahkan output (-o) ke folder internal downloads dengan format penamaan Andaconst command = `yt-dlp -f "best[ext=mp4]/best" --no-warnings --no-check-certificates -o "${DOWNLOAD_DIR}/%(title)s.%(ext)s" "${url.trim()}"`;exec(command, { timeout: 45000 }, (err, stdout, stderr) => {if (err) {console.error("Eror yt-dlp CLI:", err.message);return res.status(500).json({ success: false, message: "YouTube memblokir server cloud. Coba lagi nanti." });}// 3. Cari file yang baru saja diunduh di dalam folder downloadsfs.readdir(DOWNLOAD_DIR, (readErr, files) => {if (readErr || files.length === 0) {return res.status(500).json({ success: false, message: "File hasil unduhan tidak ditemukan di server." });}// Ambil file terbaru yang cocok dengan proses iniconst downloadedFile = files.find(file => file.startsWith(videoTitle) || files[0]);const filePath = path.join(DOWNLOAD_DIR, downloadedFile);console.log(`File fisik siap dikirim ke Vercel: ${downloadedFile}`);// 4. Kirimkan file langsung sebagai unduhan attachment murni (Binary Stream)res.download(filePath, downloadedFile, (downloadErr) => {if (downloadErr) {console.error("Gagal mentransfer file:", downloadErr.message);}// Hapus file dari server pasca-unduh agar penyimpanan Hugging Face tidak penuhtry {fs.unlinkSync(filePath);console.log("Pembersihan cache storage sukses.");} catch (clearErr) {console.error("Gagal menghapus cache:", clearErr.map);}});});});});});app.listen(PORT, () => console.log(`Server berjalan mandiri pada port ${PORT}`));1234567891011121314151617181920212223242526272829const express = require('express');const cors = require('cors');const { exec } = require('child_process');const app = express();app.use(cors({ origin: '*' }));app.use(express.json());app.post('/api/download', (req, res) => {const { url } = req.body;if (!url) return res.status(400).json({ success: false, message: "URL kosong" });// Menggunakan biner yt-dlp hasil unduhan release-files untuk mengambil format gabungan terbaikconst command = `yt-dlp -g -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --no-warnings --no-check-certificates "${url.trim()}"`;exec(command, { timeout: 30000 }, (error, stdout) => {if (error) {return res.status(500).json({ success: false, message: "Banned IP oleh YouTube." });}const lines = stdout.trim().split('\n');res.json({success: true,videoUrl: lines[0],audioUrl: lines[1] || lines[0]});});});app.listen(7860, () => console.log("Server Biner Release Siap."));
server.js+15-65