import { NextRequest, NextResponse } from 'next/server'; import { uploadFileToS3, generateFileKey } from '@/lib/s3'; export async function POST(request: NextRequest) { try { const formData = await request.formData(); const file = formData.get('file') as File; const folder = formData.get('folder') as string || 'audio'; if (!file) { return NextResponse.json({ error: 'No file provided' }, { status: 400 }); } // Validate file type (audio only) const allowedTypes = [ 'audio/mpeg', 'audio/mp3', 'audio/wav', 'audio/ogg', 'audio/flac', 'audio/aac', 'audio/m4a', ]; if (!allowedTypes.includes(file.type)) { return NextResponse.json( { error: 'Invalid file type. Only audio files are allowed.' }, { status: 400 } ); } // Validate file size (max 50MB for audio) const maxSize = 50 * 1024 * 1024; // 50MB if (file.size > maxSize) { return NextResponse.json( { error: 'File too large. Maximum size is 50MB.' }, { status: 400 } ); } // Generate unique key const key = generateFileKey(folder, file.name); // Upload to S3 const url = await uploadFileToS3({ file, key, contentType: file.type, }); return NextResponse.json({ url, key }, { status: 200 }); } catch (error) { console.error('Audio upload error:', error); return NextResponse.json( { error: 'Failed to upload audio file' }, { status: 500 } ); } }