import { NextRequest, NextResponse } from "next/server"; import { db } from "@/lib/db"; import { validateReviewToken } from "@/lib/review-auth"; import { generateHetznerDownloadUrl } from "@/lib/storage"; import { clientHighResFilename } from "@/lib/shot-code"; /** * GET /api/client/[token]/versions/[versionId]/highres/download * The version's ProRes, for a client reviewer: same visibility rule as the * version itself (GET ../route.ts), and named with the client's shot code. */ export async function GET( req: NextRequest, { params }: { params: Promise<{ token: string; versionId: string }> } ) { const { token, versionId } = await params; const result = await validateReviewToken(token, req); if (result.type !== "ok") { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const version = await db.version.findUnique({ where: { id: versionId }, select: { highResKey: true, highResFilename: true, isClientVisible: true, shot: { select: { projectId: true, shotCode: true, clientCode: true } }, task: { select: { projectId: true, shot: { select: { shotCode: true, clientCode: true } }, }, }, }, }); const projectId = version?.shot?.projectId ?? version?.task?.projectId; if (!version || projectId !== result.session.projectId) { return NextResponse.json({ error: "Not found" }, { status: 404 }); } // Task-only versions need explicit sharing, as in the version route if (!version.shot && !version.isClientVisible) { return NextResponse.json({ error: "Not found" }, { status: 404 }); } if (!version.highResKey) { return NextResponse.json({ error: "No high-res file available" }, { status: 404 }); } const shot = version.shot ?? version.task?.shot ?? null; const url = await generateHetznerDownloadUrl( version.highResKey, clientHighResFilename(version.highResFilename ?? "high-res.mov", shot) ); return NextResponse.json({ url }); }