109 lines
3.9 KiB
JavaScript
Executable File
109 lines
3.9 KiB
JavaScript
Executable File
const fs = require("node:fs/promises");
|
|
const { getDb } = require("../lib/mongo");
|
|
const { config } = require("../lib/config");
|
|
const {
|
|
ValidationError, STATUS_READ_PROJECTION, buildQueueJobs, toPublicJob, validateOutputTarget,
|
|
} = require("../lib/jobValidation");
|
|
|
|
function createQueueController(dependencies = {}) {
|
|
const connect = dependencies.getDb || getDb;
|
|
const settings = dependencies.config || config;
|
|
const now = dependencies.now || (() => new Date());
|
|
|
|
async function enqueue(req, res) {
|
|
try {
|
|
const jobs = await buildQueueJobs(req.body, settings, now);
|
|
const db = await connect();
|
|
if (jobs.length === 1 && req.body.mode === "video") {
|
|
await db.collection("queue").insertOne(jobs[0]);
|
|
} else {
|
|
await db.collection("queue").bulkWrite(jobs.map(document => ({ insertOne: { document } })));
|
|
}
|
|
return res.redirect("/status");
|
|
} catch (error) {
|
|
const invalid = error instanceof ValidationError;
|
|
return res.status(invalid ? 400 : 500).render("error", {
|
|
title: "Enqueue failed",
|
|
message: invalid ? error.message : "The download could not be queued. Check the service configuration and try again.",
|
|
details: null,
|
|
});
|
|
}
|
|
}
|
|
|
|
async function readStatus() {
|
|
const db = await connect();
|
|
const queries = [
|
|
["queue", { processing: { $ne: true } }, { created_at: 1 }],
|
|
["queue", { processing: true }, { created_at: 1 }],
|
|
["done", {}, { finished_at: -1 }],
|
|
["failed", {}, { failed_at: -1 }],
|
|
];
|
|
const lists = await Promise.all(queries.map(async ([collection, filter, sort]) => {
|
|
const records = await db.collection(collection).find(filter, { projection: STATUS_READ_PROJECTION }).sort(sort).toArray();
|
|
return records.map(toPublicJob);
|
|
}));
|
|
const [queued, processing, done, failed] = lists;
|
|
// Asynchronous file checks avoid blocking the web process. Pagination follows in Phase 2.
|
|
for (const job of [...processing, ...done]) {
|
|
try {
|
|
const target = await validateOutputTarget(settings.outputDir, job.filename);
|
|
const stat = await fs.lstat(target);
|
|
job.fileSize = stat.isFile() && !stat.isSymbolicLink() ? stat.size : null;
|
|
} catch {
|
|
job.fileSize = null;
|
|
}
|
|
}
|
|
const global = await db.collection("settings").findOne({ _id: "global" }, { projection: { captcha_blocked: 1, pause_reason: 1 } });
|
|
return {
|
|
queued, processing, done, failed,
|
|
global: {
|
|
captcha_blocked: global?.captcha_blocked === true,
|
|
pause_reason: ["authentication", "resolution"].includes(global?.pause_reason) ? global.pause_reason : null,
|
|
},
|
|
};
|
|
}
|
|
|
|
async function status(req, res, next) {
|
|
try {
|
|
const snapshot = await readStatus();
|
|
return res.render("status", { ...snapshot, finished: snapshot.done });
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async function statusData(req, res, next) {
|
|
try {
|
|
res.set("Cache-Control", "no-store");
|
|
return res.json(await readStatus());
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
function clearProcessing(req, res) {
|
|
return res.status(409).render("error", {
|
|
title: "Active downloads are protected",
|
|
message: "Active processing flags cannot be cleared safely while downloads may still be running. Stop the affected worker and verify that its download has ended before performing recovery.",
|
|
details: null,
|
|
});
|
|
}
|
|
|
|
async function clearCaptcha(req, res, next) {
|
|
try {
|
|
const db = await connect();
|
|
await db.collection("settings").updateOne({ _id: "global" }, {
|
|
$set: { captcha_blocked: false },
|
|
$unset: { pause_reason: "" },
|
|
});
|
|
return res.redirect("/status");
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
return { enqueue, status, statusData, clearProcessing, clearCaptcha };
|
|
}
|
|
|
|
module.exports = { ...createQueueController(), createQueueController };
|