// Author: Dr Hamid MADANI // POST /api/record/video — append a video chunk to the per-session file. 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 }) } await fs.mkdir(ROOT, { recursive: true }) const filePath = path.join(ROOT, `${sessionId}.webm`) const body = await req.arrayBuffer() if (body.byteLength === 0) return NextResponse.json({ ok: true, bytes: 0 }) await fs.appendFile(filePath, Buffer.from(body)) return NextResponse.json({ ok: true, bytes: body.byteLength }) }