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 { rangeResponse } from "@/lib/report/serve.mjs"; import { joinPreviewFile, joinPreviewSteps, readJoinPreview } from "@/lib/report/joinPreview.mjs"; export const dynamic = "force-dynamic"; // THE JOIN PREVIEW (release 38 lane E): `/out/join-preview.mp4`, the // first few joins of the cut in its current look (build-video.mjs // --join-preview, in the heavy slot), as ONE job on the app's runner // (lib/jobs.ts) -- the Look panel's Preview join. // // GET ?project= { preview, running } // GET ?project=&file=1 the mp4 (ranges) // GET ?job=[&since=&sinceEvent=] that job // POST { project, from? } make it (from that clip, else the first) // ?cancel= stop it const headers = { "cache-control": "no-store" }; const isPreviewJob = (kind: string) => /^join preview /.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 join preview is a report video's" }, { status: 400, headers }) }; return { project }; } 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; if (url.searchParams.get("file")) { const abs = joinPreviewFile(r.project.dir); const st = await stat(abs).catch(() => null); if (!st?.isFile()) return new Response("no join preview", { status: 404 }); return rangeResponse(request, { abs, size: st.size, headers: { "content-type": "video/mp4", "accept-ranges": "bytes", "cache-control": "private, no-store" }, }); } const manifest = await readManifest(r.project.dir); const running = runningJob(); return Response.json( { preview: await readJoinPreview(r.project.dir, manifest), running: running && running.project === r.project.id && isPreviewJob(running.kind) ? jobView(running) : null, }, { headers }, ); } export async function POST(request: Request) { 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 manifest = await readManifest(r.project.dir); if (!manifest) return Response.json({ error: "no manifest" }, { status: 400, headers }); const from = body.from === undefined || body.from === null || body.from === "" ? null : String(body.from); if (from && !clipsOf(manifest).some((e: { id: string }) => e.id === from)) return Response.json({ error: `no clip with id ${from}` }, { 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 job = startJob(`join preview ${r.project.id}`, joinPreviewSteps(r.project, from), { project: r.project.id }); return Response.json({ ok: true, job: jobView(job) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409, headers }); } }