import { stat } from "node:fs/promises"; import { cancelJob, getJob, jobView, runningJob, startJob } from "@/lib/jobs"; import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { clipsOf, readManifest } from "@/lib/projects/report.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { clipRenderFile, clipRenderSteps, readClipRender } from "@/lib/report/clipRender.mjs"; import { rangeResponse } from "@/lib/report/serve.mjs"; import { DEFAULT_VARIANT, VARIANTS } from "umtool-report-to-video/build-video"; export const dynamic = "force-dynamic"; // RENDER THIS CLIP (the clip editor's live preview, plans/live-preview.md): // one clip's segment with its on-screen chrome, pixel-true -- // build-video.mjs --clip-preview, from what is on disk, as ONE job on the // app's runner (lib/jobs.ts: one at a time, a log, Cancel), in the heavy slot. // // GET ?project=&clip=[&variant=] { render, running } // GET ?project=&clip=&file=1[&variant=] the mp4 (ranges) // GET ?job=[&since=&sinceEvent=] that job // POST { project, clip, variant? } render it // ?cancel= stop it // // Names, never paths: a project of the scan, a clip of its timeline, a // variant of the pipeline's list. const headers = { "cache-control": "no-store" }; const isClipRenderJob = (kind: string) => /^clip render /.test(kind); async function reportProject(id: unknown) { const project = await projectRef(String(id ?? "")); if (!project) return { error: Response.json({ error: "no such project" }, { status: 404, headers }) }; if (!kindMakesTakes(project.kind)) return { error: Response.json({ error: "a clip render is made of a report video's clip" }, { status: 400, headers }) }; return { project }; } const variantOf = (v: unknown): string | null => { const s = v === null || v === undefined || v === "" ? DEFAULT_VARIANT : String(v); return (VARIANTS as string[]).includes(s) ? s : null; }; export async function GET(request: Request) { const url = new URL(request.url); const jobId = url.searchParams.get("job"); if (jobId) { const job = getJob(jobId); if (!job) return Response.json({ error: "no such job" }, { status: 404, headers }); return Response.json(jobView(job, Number(url.searchParams.get("since") ?? 0), Number(url.searchParams.get("sinceEvent") ?? 0)), { headers }); } const r = await reportProject(url.searchParams.get("project")); if ("error" in r) return r.error; const variant = variantOf(url.searchParams.get("variant")); if (!variant) return Response.json({ error: `variant must be one of ${VARIANTS.join(", ")}` }, { status: 400, headers }); const clip = url.searchParams.get("clip") ?? ""; if (url.searchParams.get("file")) { const abs = clipRenderFile(r.project.dir, variant, clip); const st = abs ? await stat(abs).catch(() => null) : null; if (!abs || !st?.isFile()) return new Response(`no render of ${clip}`, { status: 404 }); return rangeResponse(request, { abs, size: st.size, headers: { "content-type": "video/mp4", "accept-ranges": "bytes", "cache-control": url.searchParams.get("v") === String(Math.round(st.mtimeMs)) ? "private, max-age=3600, immutable" : "private, no-store", }, }); } const manifest = await readManifest(r.project.dir); const entry = (manifest ? clipsOf(manifest) : []).find((e: { id: string }) => e.id === clip) ?? null; const running = runningJob(); return Response.json( { render: await readClipRender(r.project.dir, variant, clip, entry), running: running && running.project === r.project.id && isClipRenderJob(running.kind) ? jobView(running) : null, }, { headers }, ); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/clip-render"); if (refused) return refused; const url = new URL(request.url); const cancel = url.searchParams.get("cancel"); if (cancel) return Response.json({ cancelled: cancelJob(cancel) }, { headers }); const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const r = await reportProject(body.project); if ("error" in r) return r.error; const variant = variantOf(body.variant); if (!variant) return Response.json({ error: `variant must be one of ${VARIANTS.join(", ")}` }, { status: 400, headers }); const manifest = await readManifest(r.project.dir); if (!manifest) return Response.json({ error: "no manifest" }, { status: 400, headers }); const clip = String(body.clip ?? ""); if (!clipsOf(manifest).some((e: { id: string }) => e.id === clip) || !clipRenderFile(r.project.dir, variant, clip)) { return Response.json({ error: `no clip with id ${clip}` }, { status: 400, headers }); } const running = runningJob(); if (running) return Response.json({ error: `a job is already running (${running.kind})`, running: jobView(running) }, { status: 409, headers }); try { const steps = clipRenderSteps(r.project, clip, { variant: variant === DEFAULT_VARIANT ? null : variant }); const job = startJob(`clip render ${r.project.id} ${clip}`, steps, { project: r.project.id }); return Response.json({ ok: true, clip, job: jobView(job) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409, headers }); } }