// Author: Dr Hamid MADANI // // POST /api/record/audio/done — finalise the session (no-op for append-only writes). // Returns the URL where the recorded file can be fetched. import { NextRequest, NextResponse } from 'next/server' import { promises as fs } from 'fs' import path from 'path' export const runtime = 'nodejs' export const dynamic = 'force-dynamic' const ROOT = path.join(process.cwd(), 'data', 'records') const SAFE = /^[a-zA-Z0-9_-]{1,128}$/ export async function POST(req: NextRequest) { const sessionId = req.headers.get('x-session-id') if (!sessionId || !SAFE.test(sessionId)) { return NextResponse.json({ error: 'invalid X-Session-Id' }, { status: 400 }) } // Detect which extension was actually written let ext: 'webm' | 'ogg' = 'webm' try { await fs.access(path.join(ROOT, `${sessionId}.ogg`)); ext = 'ogg' } catch {} return NextResponse.json({ ok: true, url: `/api/record/audio/${sessionId}?ext=${ext}` }) }