Files
Proxer-Downloader/controllers/queueController.js
T
2025-12-06 13:48:44 +01:00

120 lines
3.0 KiB
JavaScript

const { getDb } = require("../lib/mongo");
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 = [];
for (let ep = from; ep <= to; ep++) {
const url = `https://proxer.me/watch/${id}/${ep}/${lang}`;
const filename =
name
? `${name}_E${ep}.mp4`
: `${id}_${ep}_${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({}).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,
finished: done,
failed
});
};