import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/auth"; import { db } from "@/lib/db"; import { uploadToHetzner, deleteFromHetzner } from "@/lib/storage"; export const maxDuration = 120; /** POST /api/shots/[shotId]/highres — upload a high-res file to Hetzner */ export async function POST( req: NextRequest, { params }: { params: Promise<{ shotId: string }> } ) { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const { shotId } = await params; const shot = await db.shot.findUnique({ where: { id: shotId }, select: { id: true, highResKey: true }, }); if (!shot) { return NextResponse.json({ error: "Shot not found" }, { status: 404 }); } const formData = await req.formData(); const file = formData.get("file") as File | null; if (!file) { return NextResponse.json({ error: "No file provided" }, { status: 400 }); } if (!file.type.startsWith("video/")) { return NextResponse.json({ error: "Only video files are accepted" }, { status: 400 }); } // Remove previous high-res file if one exists if (shot.highResKey) { await deleteFromHetzner(shot.highResKey).catch(() => {}); } const buffer = Buffer.from(await file.arrayBuffer()); const { key } = await uploadToHetzner(buffer, file.name, file.type, "highres"); await db.shot.update({ where: { id: shotId }, data: { highResKey: key, highResFilename: file.name }, }); return NextResponse.json({ success: true, filename: file.name }); } /** DELETE /api/shots/[shotId]/highres — remove the high-res file */ export async function DELETE( req: NextRequest, { params }: { params: Promise<{ shotId: string }> } ) { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const { shotId } = await params; const shot = await db.shot.findUnique({ where: { id: shotId }, select: { id: true, highResKey: true }, }); if (!shot) { return NextResponse.json({ error: "Shot not found" }, { status: 404 }); } if (shot.highResKey) { await deleteFromHetzner(shot.highResKey).catch(() => {}); } await db.shot.update({ where: { id: shotId }, data: { highResKey: null, highResFilename: null }, }); return NextResponse.json({ success: true }); }