const { getDb } = require("../lib/mongo"); const fs = require("fs"); const path = require("path"); exports.enqueue = async (req, res) => { try { const db = await getDb(); const body = req.body; // Token always required if (!body.token) throw new Error("token required"); const token_prefix = process.env.TOKEN_PREFIX; const token = token_prefix + body.token; // ========================================= // VIDEO MODE // ========================================= if (body.mode === "video") { if (!body.url) throw new Error("url required"); var filename; if (!body.filename) { let split_slash = body.url.split("/"); let split_hash = `${split_slash[4]} E${split_slash[5]} ${split_slash[6]}`.split("#"); filename = split_hash[0]; console.log(filename); } else { filename = body.filename; } await db.collection("queue").insertOne({ url: body.url, token, filename, created_at: new Date(), }); return res.redirect("/status"); } // ========================================= // SERIES MODE // ========================================= if (body.mode === "series") { if (!body.id) throw new Error("id required"); if (!body.language) throw new Error("language required"); if (!body.episode_from) throw new Error("episode_from required"); if (!body.episode_to) throw new Error("episode_to required"); const id = body.id; const lang = body.language; const name = body.name?.trim() || null; const from = parseInt(body.episode_from); const to = parseInt(body.episode_to); if (isNaN(from) || from <= 0) throw new Error("episode_from must be a valid number"); if (isNaN(to) || to < from) throw new Error("episode_to must be >= episode_from"); // Insert one queue item per episode const ops = []; // Determine padding width based on the highest episode number const pad = String(to).length; for (let ep = from; ep <= to; ep++) { const epStr = String(ep).padStart(pad, "0"); const url = `https://proxer.me/watch/${id}/${ep}/${lang}`; const filename = name ? `${name}_E${epStr}.mp4` : `${id}_${epStr}_${lang}.mp4`; ops.push({ insertOne: { document: { url, token, filename, created_at: new Date(), }, }, }); } await db.collection("queue").bulkWrite(ops); return res.redirect("/status"); } throw new Error("Invalid mode"); } catch (err) { res.render("error", { title: "Enqueue Failed", message: err.message, details: err.stack, }); } }; exports.status = async (req, res) => { const db = await getDb(); const queued = await db .collection("queue") .find({ $or: [{ processing: { $exists: false } }, { processing: false }] }) .sort({ created_at: 1 }) .toArray(); const processing = await db .collection("queue") .find({ processing: true }) .sort({ created_at: 1 }) .toArray(); const done = await db .collection("done") .find({}) .sort({ finished_at: -1 }) .toArray(); const failed = await db .collection("failed") .find({}) .sort({ failed_at: -1 }) .toArray(); res.render("status", { title: "Queue Status", queued, processing, finished: done, failed, }); }; exports.statusData = async (req, res) => { const db = await getDb(); const queued = await db .collection("queue") .find({ processing: { $ne: true } }) .sort({ created_at: 1 }) .toArray(); const processing = await db .collection("queue") .find({ processing: true }) .sort({ created_at: 1 }) .toArray(); const done = await db .collection("done") .find({}) .sort({ finished_at: -1 }) .toArray(); const failed = await db .collection("failed") .find({}) .sort({ failed_at: -1 }) .toArray(); // =========================== // FILE SIZE FOR DONE // =========================== for (let job of done) { try { const filePath = path.join(process.env.OUTPUT_DIR, `${job.filename}.mp4`); const stats = fs.statSync(filePath); job.fileSize = stats.size; } catch { job.fileSize = null; } } // =========================== // FILE SIZE FOR PROCESSING // =========================== for (let job of processing) { try { const filePath = path.join(process.env.OUTPUT_DIR, `${job.filename}.mp4`); const stats = fs.statSync(filePath); job.fileSize = stats.size; } catch { job.fileSize = null; } } res.json({ queued, processing, done, failed }); };