import path from "node:path"; import { stat } from "node:fs/promises"; import { resolveInRoots } from "@/lib/paths.mjs"; import { rangeResponse, resolveReport } from "@/lib/report/serve.mjs"; import { narrationsOf } from "umtool-report-to-video/narration"; import { narrationDir, narrationWavPath, readNarrationRecord } from "umtool-report-to-video/narration-store"; export const dynamic = "force-dynamic"; // GET ?project&card: the narration's kept voice for that card's CURRENT words // and voice (out/narration/.wav), or a 404 until it is synthesized. Names, // never paths: the file is found from the manifest's narration. `&key=`: // another reading kept for that card (lane line-rerun) -- its record must name // the card. 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 card = url.searchParams.get("card") ?? ""; const n = narrationsOf(r.manifest).find((x: { id: string }) => x.id === card); if (!n) return new Response("not a card with a narration", { status: 404 }); const dir = narrationDir(path.join(r.project.dir, "out")); const asked = url.searchParams.get("key"); if (asked !== null && !/^[0-9a-f]{64}$/.test(asked)) return new Response("not a reading's key", { status: 400 }); const key = asked ?? n.key; const rec = await readNarrationRecord(dir, key); if (!rec || rec.id !== card) return new Response("this narration has not been synthesized yet", { status: 404 }); const abs = resolveInRoots(narrationWavPath(dir, key)); const st = abs ? await stat(abs).catch(() => null) : null; if (!abs || !st?.isFile()) return new Response("this narration has not been synthesized yet", { status: 404 }); return rangeResponse(request, { abs, size: st.size, headers: { "content-type": "audio/wav", "accept-ranges": "bytes", "cache-control": "private, no-store" }, }); }