import { NextResponse } from "next/server"; import { authorizeWorkerRequest } from "yt-dlp-transcript-common/lib/workerToken"; import { startWorkerTranscription, startWorkerTranscriptionShared, } from "yt-dlp-transcript-common/controller/workerServer"; export const dynamic = "force-dynamic"; // Accept a transcription job from another instance. Two body shapes: // - application/json {channelSlug, videoId, audioFilename}: shared-fs fast path, // transcribe the video dir in place on the shared mount. // - else raw audio bytes (X-Audio-Name names the file, X-Job-Label is a label): // write to scratch and transcribe, requester pulls the result. // Either way returns { remoteJobId } (202); the requester polls // /api/worker/transcribe//events and (upload path) pulls /result. export async function POST(request: Request) { const auth = authorizeWorkerRequest(request.headers.get("authorization")); if (!auth.ok) { return NextResponse.json({ error: auth.error }, { status: auth.status }); } const contentType = request.headers.get("content-type") ?? ""; try { if (contentType.includes("application/json")) { const body = (await request.json()) as { channelSlug?: string; videoId?: string; audioFilename?: string; }; if (!body.channelSlug || !body.videoId) { return NextResponse.json( { error: "channelSlug and videoId are required" }, { status: 400 }, ); } const { remoteJobId } = await startWorkerTranscriptionShared({ channelSlug: body.channelSlug, videoId: body.videoId, audioFilename: body.audioFilename ?? "audio.mp3", }); return NextResponse.json({ remoteJobId, inPlace: true }, { status: 202 }); } const audioName = request.headers.get("x-audio-name") ?? "audio"; const label = request.headers.get("x-job-label") ?? undefined; const audio = Buffer.from(await request.arrayBuffer()); if (audio.length === 0) { return NextResponse.json({ error: "empty audio body" }, { status: 400 }); } const { remoteJobId } = await startWorkerTranscription({ audio, audioName, label, }); return NextResponse.json({ remoteJobId }, { status: 202 }); } catch (e) { return NextResponse.json({ error: (e as Error).message }, { status: 500 }); } }