import { NextRequest, NextResponse } from "next/server"; import { getServerSession } from "@/lib/auth"; import { db } from "@/lib/db"; import { sounds } from "@/lib/db/schema"; import { writeFile, mkdir } from "fs/promises"; import path from "path"; import { v4 as uuidv4 } from "uuid"; const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds"); export async function POST(req: NextRequest) { const session = await getServerSession(); if (!session || !session.isAdmin) { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } try { const { name, duration, previewUrl } = await req.json(); if (!previewUrl || !name) { return NextResponse.json({ error: "Missing name or previewUrl" }, { status: 400 }); } if (duration > 6) { return NextResponse.json({ error: "Sound too long (max 6s)" }, { status: 400 }); } await mkdir(UPLOAD_DIR, { recursive: true }); const res = await fetch(previewUrl); if (!res.ok) return NextResponse.json({ error: "Failed to download preview" }, { status: 502 }); const buffer = Buffer.from(await res.arrayBuffer()); // trim to 6 seconds: approximate by byte ratio const maxBytes = Math.floor(buffer.length * Math.min(6 / duration, 1)); const trimmed = buffer.subarray(0, maxBytes); const ext = ".mp3"; const filename = `${uuidv4()}${ext}`; const filepath = path.join(UPLOAD_DIR, filename); await writeFile(filepath, trimmed); const id = uuidv4(); const originalName = name.replace(/\.[^.]+$/, "") + ext; db.insert(sounds).values({ id, originalName, storedFilename: filename, duration: Math.min(duration, 6), uploadedBy: session.id, createdAt: new Date().toISOString(), }).run(); return NextResponse.json({ url: `/uploads/sounds/${filename}`, originalName, duration: Math.min(duration, 6), }); } catch { return NextResponse.json({ error: "Import failed" }, { status: 500 }); } }