import { readFile, stat } from "node:fs/promises"; import { resolveClip, resolveReport } from "@/lib/report/serve.mjs"; import { StillRefused, grabStill, listShots, shotFileOf } from "@/lib/report/stillGrab.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; export const dynamic = "force-dynamic"; // A STILL AT SECOND N (umtool redesign L6: Play's Still button; `umtool still` is the twin): one frame of the // clip on screen at the playhead's SOURCE second, grabbed from its fetched window (lib/report/stillGrab.mjs: a // single `ffmpeg -ss -frames:v 1`, not heavy work) into the project's shots/ beside its other shots -- where a // clip's fx.zoom / fx.receipts read pictures from. Nothing is fetched, and nothing in the cut is written. // // GET ?project= { shots: [{ rel, name, bytes, mtime, still: { clip, at } | null }] } // GET ?project=&file=shots/ the picture // POST { project, clip, at, take? } grab it -> { rel, clip, video, at, bytes } const noStore = { "cache-control": "no-store" }; const TYPES: Record = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp" }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const file = url.searchParams.get("file"); if (file) { const abs = shotFileOf(r.project.dir, file); if (!abs) return new Response("not a picture in shots/", { status: 404 }); const st = await stat(abs).catch(() => null); if (!st?.isFile()) return new Response("gone", { status: 404 }); const ext = abs.slice(abs.lastIndexOf(".")).toLowerCase(); return new Response(new Uint8Array(await readFile(abs)), { headers: { "content-type": TYPES[ext] ?? "application/octet-stream", "cache-control": "no-cache" } }); } return Response.json({ shots: await listShots(r.project.dir) }, { headers: noStore }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/still-frame"); if (refused) return refused; const cross = crossSiteRefusal(request.headers); if (cross) return Response.json({ error: cross }, { status: 403 }); let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const take = typeof body.take === "string" && body.take ? body.take : null; const r = await resolveClip(String(body.project ?? ""), String(body.clip ?? ""), { take }); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); try { const done = await grabStill(r.project, r.manifest, r.clip.id, Number(body.at)); const { file: _abs, ...rest } = done; return Response.json({ ok: true, ...rest }, { headers: noStore }); } catch (e) { if (e instanceof StillRefused) return Response.json({ error: e.message }, { status: e.status }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 }); } }