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}` : `${id}_${epStr}_${lang}`; 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(); const settings = (await db.collection("settings").findOne({ _id: "global" })) || {}; console.log; res.render("status", { queued, processing, finished: done, failed, global: settings, }); }; 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 sizes const OUTPUT_DIR = process.env.OUTPUT_DIR || "/mnt/media/WIP"; for (const list of [processing, done]) { for (const job of list) { try { const stat = fs.statSync(path.join(OUTPUT_DIR, `${job.filename}.mp4`)); job.fileSize = stat.size; } catch { job.fileSize = null; } } } // Fetch global settings const settings = await db.collection("settings").findOne({ _id: "global" }) || {}; settings.captcha_blocked = !!settings.captcha_blocked; // Send JSON including global res.json({ queued, processing, done, failed, global: settings, }); };