Compare commits

..

No commits in common. "3fde7d4a207a5e9ff04017b2cfbee8e204fc59f6" and "7e501a3acac535df8f206d0cfe3f7c5b70b85235" have entirely different histories.

View file

@ -49,7 +49,7 @@
import { spawn } from "node:child_process"; import { spawn } from "node:child_process";
import { randomBytes } from "node:crypto"; import { randomBytes } from "node:crypto";
import { access, constants, copyFile, mkdir, readdir, rm, stat } from "node:fs/promises"; import { copyFile, mkdir, readdir, rm, stat } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { UPLOADS_DIR, PUBLIC_PREFIX } from "./media.js"; import { UPLOADS_DIR, PUBLIC_PREFIX } from "./media.js";
@ -251,27 +251,12 @@ function update(db, id, fields) {
async function cookieArgs(jobId) { async function cookieArgs(jobId) {
if (COOKIES_FILE) { if (COOKIES_FILE) {
const copy = path.join(CLIPS_DIR, `.cookies-${jobId}.txt`); const copy = path.join(CLIPS_DIR, `.cookies-${jobId}.txt`);
// Two steps, so a failure names the right one: reading the
// original, or writing the copy into CLIPS_DIR.
try {
await access(COOKIES_FILE, constants.R_OK);
} catch (err) {
const why =
err.code === "ENOENT"
? "it isn't there"
: err.code === "EACCES"
? "the service's user isn't allowed to read it (check its owner and permissions)"
: err.code;
throw new Error(`Couldn't read the cookies file at ${COOKIES_FILE} (YTDLP_COOKIES): ${why}.`);
}
try { try {
await copyFile(COOKIES_FILE, copy); await copyFile(COOKIES_FILE, copy);
} catch (err) { } catch (err) {
const why = throw new Error(
err.code === "EACCES" || err.code === "EPERM" `Couldn't read the cookies file at ${COOKIES_FILE} (YTDLP_COOKIES)${err.code === "ENOENT" ? ": it isn't there" : ""}.`,
? "the service's user isn't allowed to write there" );
: err.code;
throw new Error(`Couldn't copy the cookies file into ${CLIPS_DIR} (CLIPS_DIR): ${why}.`);
} }
return { args: ["--cookies", copy], cleanup: () => rm(copy, { force: true }) }; return { args: ["--cookies", copy], cleanup: () => rm(copy, { force: true }) };
} }
@ -442,13 +427,7 @@ export function kick(db = dbRef) {
(async () => { (async () => {
try { try {
await mkdir(CLIPS_DIR, { recursive: true }).catch((err) => { await mkdir(CLIPS_DIR, { recursive: true });
throw new Error(
`Couldn't use the working folder ${CLIPS_DIR} (CLIPS_DIR): ${
err.code === "EACCES" || err.code === "EPERM" ? "the service's user isn't allowed to write there" : err.code
}.`,
);
});
if (job.kind === "source") await download(db, job); if (job.kind === "source") await download(db, job);
else await cut(db, job); else await cut(db, job);
update(db, job.id, { status: "done", progress: 1, finished_at: new Date().toISOString() }); update(db, job.id, { status: "done", progress: 1, finished_at: new Date().toISOString() });
@ -481,55 +460,25 @@ export async function saveToLibrary(db, job, userId) {
const audio = job.file.endsWith(".mp3"); const audio = job.file.endsWith(".mp3");
const ext = audio ? ".mp3" : ".mp4"; const ext = audio ? ".mp3" : ".mp4";
const filename = `${randomBytes(12).toString("hex")}${ext}`; const filename = `${randomBytes(12).toString("hex")}${ext}`;
const to = path.join(UPLOADS_DIR, filename); await mkdir(UPLOADS_DIR, { recursive: true });
await copyFile(from, path.join(UPLOADS_DIR, filename));
// Each step says what it was doing if it fails, so the page can const info = audio ? { width: null, height: null } : await probe(from);
// tell an admin (this tool is admin-only) what to fix: usually the const { size } = await stat(from);
// uploads folder's permissions, or UPLOADS_DIR not set. const params = JSON.parse(job.params ?? "{}");
const step = async (what, fn) => { const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
try { const label = `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`;
return await fn();
} catch (err) {
const why =
err.code === "EACCES" || err.code === "EPERM"
? "the service's user isn't allowed to write there"
: err.message;
throw new ClipError(500, `Couldn't save it to the library: ${what} (${why}).`);
}
};
await step(`making the uploads folder ${UPLOADS_DIR}`, () => mkdir(UPLOADS_DIR, { recursive: true })); const { lastInsertRowid } = db
await step(`copying the clip into ${UPLOADS_DIR}`, () => copyFile(from, to)); .prepare(
`INSERT INTO media (filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
let id; VALUES (?, ?, ?, ?, ?, ?, 0, ?)`,
try { )
const info = audio .run(filename, label.slice(0, 200), audio ? "audio/mpeg" : "video/mp4", size, info.width, info.height, userId);
? { width: null, height: null }
: await step("reading the clip's size", () => probe(from));
const { size } = await stat(from);
const params = JSON.parse(job.params ?? "{}");
const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
const label = `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`;
id = await step("adding it to the media table", () =>
Number(
db
.prepare(
`INSERT INTO media (filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
VALUES (?, ?, ?, ?, ?, ?, 0, ?)`,
)
.run(filename, label.slice(0, 200), audio ? "audio/mpeg" : "video/mp4", size, info.width, info.height, userId)
.lastInsertRowid,
),
);
} catch (err) {
await rm(to, { force: true }); // no file in uploads without its row
throw err;
}
await rm(from, { force: true }); await rm(from, { force: true });
update(db, job.id, { saved_media_id: id, file: null }); update(db, job.id, { saved_media_id: Number(lastInsertRowid), file: null });
return { id, url: `${PUBLIC_PREFIX}${filename}` }; return { id: Number(lastInsertRowid), url: `${PUBLIC_PREFIX}${filename}` };
} }
/* After a clip has gone out in full: its file isn't needed. */ /* After a clip has gone out in full: its file isn't needed. */