import { NextResponse } from 'next/server'; import { PrismaClient } from '@prisma/client'; import { writeFile, unlink } from 'fs/promises'; import path from 'path'; import { parseBuffer } from 'music-metadata'; import { isDuplicateSong } from '@/lib/fuzzyMatch'; const prisma = new PrismaClient(); export async function GET() { const songs = await prisma.song.findMany({ orderBy: { createdAt: 'desc' }, include: { puzzles: true, genres: true, specials: { include: { special: true } }, }, }); // Map to include activation count and flatten specials const songsWithActivations = songs.map(song => ({ id: song.id, title: song.title, artist: song.artist, filename: song.filename, createdAt: song.createdAt, coverImage: song.coverImage, activations: song.puzzles.length, puzzles: song.puzzles, genres: song.genres, specials: song.specials.map(ss => ss.special), averageRating: song.averageRating, ratingCount: song.ratingCount, })); return NextResponse.json(songsWithActivations); } export async function POST(request: Request) { try { const formData = await request.formData(); const file = formData.get('file') as File; let title = ''; let artist = ''; if (!file) { return NextResponse.json({ error: 'No file provided' }, { status: 400 }); } const buffer = Buffer.from(await file.arrayBuffer()); // Validate and extract metadata from file let metadata; let validationInfo = { isValid: true, hasCover: false, format: '', bitrate: 0, sampleRate: 0, duration: 0, codec: '', warnings: [] as string[], }; try { metadata = await parseBuffer(buffer, file.type); // Extract basic metadata if (metadata.common.title) { title = metadata.common.title; } // Handle artist - prefer artists array if available if (metadata.common.artists && metadata.common.artists.length > 0) { // Join multiple artists with '/' artist = metadata.common.artists.join('/'); } else if (metadata.common.artist) { artist = metadata.common.artist; } else if (metadata.common.albumartist) { // Fallback to album artist artist = metadata.common.albumartist; } // Validation info validationInfo.hasCover = !!metadata.common.picture?.[0]; validationInfo.format = metadata.format.container || 'unknown'; validationInfo.bitrate = metadata.format.bitrate || 0; validationInfo.sampleRate = metadata.format.sampleRate || 0; validationInfo.duration = metadata.format.duration || 0; validationInfo.codec = metadata.format.codec || 'unknown'; // Validate format if (metadata.format.container !== 'MPEG') { validationInfo.warnings.push('File may not be a standard MP3 (MPEG container expected)'); } // Check bitrate if (validationInfo.bitrate && validationInfo.bitrate < 96000) { validationInfo.warnings.push(`Low bitrate detected: ${Math.round(validationInfo.bitrate / 1000)} kbps`); } // Check sample rate if (validationInfo.sampleRate && ![44100, 48000].includes(validationInfo.sampleRate)) { validationInfo.warnings.push(`Non-standard sample rate: ${validationInfo.sampleRate} Hz (recommended: 44100 or 48000 Hz)`); } // Check duration if (!validationInfo.duration || validationInfo.duration < 30) { validationInfo.warnings.push('Audio file is very short (less than 30 seconds)'); } } catch (e) { console.error('Failed to parse metadata:', e); validationInfo.isValid = false; validationInfo.warnings.push('Failed to parse audio metadata - file may be corrupted'); } // Fallback if still missing if (!title) title = 'Unknown Title'; if (!artist) artist = 'Unknown Artist'; // Check for duplicates const existingSongs = await prisma.song.findMany({ select: { id: true, title: true, artist: true, filename: true } }); for (const existing of existingSongs) { if (isDuplicateSong(artist, title, existing.artist, existing.title)) { return NextResponse.json( { error: 'Duplicate song detected', duplicate: { id: existing.id, title: existing.title, artist: existing.artist, filename: existing.filename } }, { status: 409 } ); } } // Create URL-safe filename const originalName = file.name.replace(/\.mp3$/i, ''); const sanitizedName = originalName .replace(/[^a-zA-Z0-9]/g, '-') // Replace special chars with dash .replace(/-+/g, '-') // Replace multiple dashes with single dash .replace(/^-|-$/g, ''); // Remove leading/trailing dashes // Warn if filename was changed if (originalName !== sanitizedName) { validationInfo.warnings.push(`Filename sanitized: "${originalName}" → "${sanitizedName}"`); } const filename = `${Date.now()}-${sanitizedName}.mp3`; const uploadDir = path.join(process.cwd(), 'public/uploads'); await writeFile(path.join(uploadDir, filename), buffer); // Handle cover image let coverImage = null; try { const picture = metadata?.common.picture?.[0]; if (picture) { const extension = picture.format.split('/')[1] || 'jpg'; const coverFilename = `cover-${Date.now()}.${extension}`; const coverPath = path.join(process.cwd(), 'public/uploads/covers', coverFilename); await writeFile(coverPath, picture.data); coverImage = coverFilename; } } catch (e) { console.error('Failed to extract cover image:', e); } const song = await prisma.song.create({ data: { title, artist, filename, coverImage, }, include: { genres: true, specials: true } }); return NextResponse.json({ song, validation: validationInfo, }); } catch (error) { console.error('Error uploading song:', error); return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); } } export async function PUT(request: Request) { try { const { id, title, artist, genreIds, specialIds } = await request.json(); if (!id || !title || !artist) { return NextResponse.json({ error: 'Missing fields' }, { status: 400 }); } const data: any = { title, artist }; if (genreIds) { data.genres = { set: genreIds.map((gId: number) => ({ id: gId })) }; } // Handle SpecialSong relations separately if (specialIds !== undefined) { // First, get current special assignments const currentSpecials = await prisma.specialSong.findMany({ where: { songId: Number(id) } }); const currentSpecialIds = currentSpecials.map(ss => ss.specialId); const newSpecialIds = specialIds as number[]; // Delete removed specials const toDelete = currentSpecialIds.filter(sid => !newSpecialIds.includes(sid)); if (toDelete.length > 0) { await prisma.specialSong.deleteMany({ where: { songId: Number(id), specialId: { in: toDelete } } }); } // Add new specials const toAdd = newSpecialIds.filter(sid => !currentSpecialIds.includes(sid)); if (toAdd.length > 0) { await prisma.specialSong.createMany({ data: toAdd.map(specialId => ({ songId: Number(id), specialId, startTime: 0 })) }); } } const updatedSong = await prisma.song.update({ where: { id: Number(id) }, data, include: { genres: true, specials: { include: { special: true } } } }); return NextResponse.json(updatedSong); } catch (error) { console.error('Error updating song:', error); return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); } } export async function DELETE(request: Request) { try { const { id } = await request.json(); if (!id) { return NextResponse.json({ error: 'Missing id' }, { status: 400 }); } // Get song to find filename const song = await prisma.song.findUnique({ where: { id: Number(id) }, }); if (!song) { return NextResponse.json({ error: 'Song not found' }, { status: 404 }); } // Delete file const filePath = path.join(process.cwd(), 'public/uploads', song.filename); try { await unlink(filePath); } catch (e) { console.error('Failed to delete file:', e); // Continue with DB deletion even if file deletion fails } // Delete cover image if exists if (song.coverImage) { const coverPath = path.join(process.cwd(), 'public/uploads/covers', song.coverImage); try { await unlink(coverPath); } catch (e) { console.error('Failed to delete cover image:', e); } } // Delete from database (will cascade delete related puzzles) await prisma.song.delete({ where: { id: Number(id) }, }); return NextResponse.json({ success: true }); } catch (error) { console.error('Error deleting song:', error); return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); } }