/* ═══════════════════════════════════════════════════════════════ VIDEO CLIP ROUTES: mounted under /api/admin, admins and up GET /clips tools present, and recent jobs POST /clips/sources { url }: queue a download POST /clips { source_id, start, end, shape, focus, format }: queue a clip GET /clips/:id one job, for polling progress GET /clips/:id/video a source's video, streamed with Range support, for the editor GET /clips/:id/download a finished clip, as an attachment; its file is deleted once it has gone out in full, unless saved POST /clips/:id/save a finished clip into the media library DELETE /clips/:id a job and its files (a source takes its clips) The work itself is in clips.js. Admins only: this downloads and encodes video on the VPS, which is heavy, and pulls from sites outside ours. ═══════════════════════════════════════════════════════════════ */ import { createReadStream } from "node:fs"; import { stat } from "node:fs/promises"; import { Readable } from "node:stream"; import { Hono } from "hono"; import { requireAuth, requireRole } from "../auth.js"; import { ClipError, checkClip, checkSourceUrl, fileOf, forgetDownloaded, kick, removeJob, saveToLibrary, toolVersions, } from "../clips.js"; import { NO_STORE } from "../shape.js"; const clips = new Hono(); clips.use("/clips", requireAuth, requireRole("admin")); clips.use("/clips/*", requireAuth, requireRole("admin")); const RECENT = 50; function shape(job) { let params = null; try { params = job.params ? JSON.parse(job.params) : null; } catch { params = null; } return { id: job.id, kind: job.kind, status: job.status, progress: job.progress, source_url: job.source_url, source_id: job.source_id, params, title: job.title, duration: job.duration, bytes: job.bytes, error: job.error, has_file: Boolean(job.file), saved_media_id: job.saved_media_id, saved_url: job.saved_filename ? `/uploads/${job.saved_filename}` : null, downloaded_at: job.downloaded_at, created_at: job.created_at, finished_at: job.finished_at, }; } const SELECT = `SELECT j.*, m.filename AS saved_filename FROM clip_jobs j LEFT JOIN media m ON m.id = j.saved_media_id`; function findOr404(c) { const id = Number(c.req.param("id")); const job = Number.isInteger(id) ? c.get("db").prepare(`${SELECT} WHERE j.id = ?`).get(id) : null; if (!job) throw new ClipError(404, "No such job."); return job; } async function body(c) { try { return await c.req.json(); } catch { throw new ClipError(400, "Expected a JSON body."); } } clips.get("/clips", async (c) => { const db = c.get("db"); const rows = db.prepare(`${SELECT} ORDER BY j.id DESC LIMIT ${RECENT * 2}`).all(); return c.json( { tools: await toolVersions(), sources: rows.filter((r) => r.kind === "source").slice(0, RECENT).map(shape), clips: rows.filter((r) => r.kind === "clip").slice(0, RECENT).map(shape), }, 200, NO_STORE, ); }); clips.post("/clips/sources", async (c) => { const db = c.get("db"); const url = checkSourceUrl((await body(c)).url); const { lastInsertRowid } = db .prepare(`INSERT INTO clip_jobs (kind, source_url, created_by) VALUES ('source', ?, ?)`) .run(url, c.get("user").id); console.log(`clips: download of ${url} queued by ${c.get("user").email}`); kick(db); return c.json({ job: shape(db.prepare(`${SELECT} WHERE j.id = ?`).get(lastInsertRowid)) }, 201, NO_STORE); }); clips.post("/clips", async (c) => { const db = c.get("db"); const input = await body(c); const source = db .prepare(`SELECT * FROM clip_jobs WHERE id = ? AND kind = 'source'`) .get(Number(input.source_id)); if (!source) throw new ClipError(404, "No such source."); if (source.status !== "done" || !source.file) throw new ClipError(409, "That video hasn't finished downloading."); const params = checkClip(input, source); const { lastInsertRowid } = db .prepare(`INSERT INTO clip_jobs (kind, source_id, params, title, created_by) VALUES ('clip', ?, ?, ?, ?)`) .run(source.id, JSON.stringify(params), source.title, c.get("user").id); kick(db); return c.json({ job: shape(db.prepare(`${SELECT} WHERE j.id = ?`).get(lastInsertRowid)) }, 201, NO_STORE); }); clips.get("/clips/:id", (c) => c.json({ job: shape(findOr404(c)) }, 200, NO_STORE)); /* A byte range of a file, or all of it: what