import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { rangeResponse } from "@/lib/report/serve.mjs"; import { variantFile, variantsState } from "@/lib/report/variantsView.mjs"; export const dynamic = "force-dynamic"; // MAKE VARIANTS and SYNC, read back (video knobs, plans/video-knobs-ui.md). // "Make variants" itself is a job: POST /api/report/deliver // { action: "variants", take? }. // // GET ?project=[&take=] { take, slug, built, ladder, made, stale, sync, takes } // GET ?project=[&take=]&file= that variant's mp4 (a name from the ladder, never a path) const noStore = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers: noStore }); if (!kindMakesTakes(project.kind)) return Response.json({ error: "variants are a report video's" }, { status: 400, headers: noStore }); const manifest = await readManifest(project.dir); if (!manifest) return Response.json({ error: "no manifest" }, { status: 400, headers: noStore }); const take = url.searchParams.get("take") || null; const name = url.searchParams.get("file"); try { if (name !== null) { const f = await variantFile(project, manifest, name, take); if (!f) return new Response("that variant has not been made", { status: 404 }); return rangeResponse(request, { abs: f.abs, size: f.size, headers: { "content-type": "video/mp4", "accept-ranges": "bytes", "cache-control": "private, no-store", "content-disposition": `inline; filename="${f.rel}"`, }, }); } return Response.json(await variantsState(project, manifest, take), { headers: noStore }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400, headers: noStore }); } }