import { readFile } from "node:fs/promises"; import { refuseCaller } from "@/lib/report/caller"; import { resolveReport } from "@/lib/report/serve.mjs"; import { drawPromoCards, setPromoPortrait } from "@/lib/report/promoCli.mjs"; export const dynamic = "force-dynamic"; // THE CHANNEL PROMO's card, drawn to look at, and its portrait from a clip (lane clip-kit, slice D; // report-to-video promo-card.mjs, lib/report/promoCli.mjs -- the same functions `umtool promo` runs). // // GET ?project=&shape=v|h the card (PNG), 9:16 (v) or 16:9 (h), drawn from the look as saved // (kept by its key: drawn again only when the promo changes) // POST { project, op: "portrait", clip, at, box? } the frame of `clip` at source second `at` grabbed into shots/ and // made the card's portrait -> { promo } | 400 { error } const noStore = { "cache-control": "no-store" }; 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 new Response(r.error, { status: r.status }); const shape = url.searchParams.get("shape") === "h" ? "h" : "v"; try { const cards = await drawPromoCards(r.project); const card = cards.find((c) => (shape === "v" ? c.height > c.width : c.width > c.height)); if (!card) return new Response("no card", { status: 500 }); return new Response(new Uint8Array(await readFile(card.file)), { headers: { "content-type": "image/png", ...noStore } }); } catch (e) { return new Response(e instanceof Error ? e.message : String(e), { status: 400 }); } } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/promo-card"); if (refused) return refused; let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); if (body.op !== "portrait") return Response.json({ error: 'op is "portrait"' }, { status: 400 }); const at = Number(body.at); const box = Array.isArray(body.box) && body.box.length === 4 && body.box.every((n) => typeof n === "number") ? (body.box as number[]) : null; if (typeof body.clip !== "string" || !Number.isFinite(at)) return Response.json({ error: "clip and at (a source second) are needed" }, { status: 400 }); try { const set = await setPromoPortrait(r.project, { clip: body.clip, at, box }); return Response.json({ promo: set.promo, still: set.still.rel }, { headers: noStore }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }