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 { readShorts, setShortAside, shortFile, shortSteps } from "@/lib/report/shorts.mjs"; export const dynamic = "force-dynamic"; // VERTICAL SHORTS (release 36 lane C): `/shorts/.mp4`, one // clip at 1080x1920 (build-video.mjs --short, in the heavy slot), as ONE job // on the app's runner (lib/jobs.ts: one at a time, a log, Cancel) -- the // clip editor's Make a short and the Deliver tab's Shorts list. // // GET ?project= { shorts, running } // GET ?project=&clip=&file=1 the mp4 (ranges; `download=1` as an attachment) // GET ?job=[&since=&sinceEvent=] that job // POST { project, clip, replace? } make it; 409 { needsReplace } when one is there // ?cancel= stop it const headers = { "cache-control": "no-store" }; const isShortJob = (kind: string) => /^short /.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 short is made of a report video's clip" }, { 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 clip = url.searchParams.get("clip") ?? ""; const abs = shortFile(r.project.dir, clip); const st = abs ? await stat(abs).catch(() => null) : null; if (!abs || !st?.isFile()) return new Response(`no short 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", ...(url.searchParams.get("download") ? { "content-disposition": `attachment; filename="${clip}-short.mp4"` } : {}), }, }); } const manifest = await readManifest(r.project.dir); const running = runningJob(); return Response.json( { shorts: await readShorts(r.project.dir, manifest), running: running && running.project === r.project.id && isShortJob(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 clip = String(body.clip ?? ""); if (!clipsOf(manifest).some((e: { id: string }) => e.id === clip) || !shortFile(r.project.dir, 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 }); let aside: string | null = null; try { aside = await setShortAside(r.project.dir, clip, { replace: body.replace === true }); } catch (e) { const needsReplace = !!(e as { needsReplace?: boolean })?.needsReplace; return Response.json({ error: e instanceof Error ? e.message : String(e), needsReplace }, { status: 409, headers }); } try { const job = startJob(`short ${r.project.id} ${clip}`, shortSteps(r.project, clip), { project: r.project.id }); return Response.json({ ok: true, clip, aside, job: jobView(job) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 409, headers }); } }