import { existsSync } from "node:fs"; import { sourceWav } from "@/lib/clips"; import { readWavWindow, peakBuckets } from "@/lib/wav"; export const dynamic = "force-dynamic"; // Waveform peaks for a window, so the canvas can redraw when the view widens // without shipping samples. A 13-minute view is 1,200 buckets either way. export async function GET(request: Request, ctx: { params: Promise<{ key: string }> }) { const { key } = await ctx.params; const url = new URL(request.url); const from = Number(url.searchParams.get("from")); const to = Number(url.searchParams.get("to")); const buckets = Math.max(64, Math.min(4000, Number(url.searchParams.get("n") ?? 1200))); if (!Number.isFinite(from) || !Number.isFinite(to) || to <= from) { return new Response("bad window", { status: 400 }); } const decoded = decodeURIComponent(key); const at = decoded.lastIndexOf("@"); const video = at > 0 ? decoded.slice(0, at) : decoded; const file = sourceWav(video); if (!existsSync(file)) return Response.json({ error: "source missing" }, { status: 404 }); const region = await readWavWindow(file, from, to); const { min, max } = peakBuckets(region.x, buckets); return Response.json( { from: region.from, to: region.to, duration: region.duration, min, max }, { headers: { "cache-control": "no-store" } }, ); }