Files
hoerdle/app/api/songs/route.ts

247 lines
8.0 KiB
TypeScript

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';
const prisma = new PrismaClient();
export async function GET() {
const songs = await prisma.song.findMany({
orderBy: { createdAt: 'desc' },
include: {
puzzles: true,
genres: true,
specials: true,
},
});
// Map to include activation count
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,
}));
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;
}
if (metadata.common.artist) {
artist = metadata.common.artist;
}
// 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';
// 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 }))
};
}
if (specialIds) {
data.specials = {
set: specialIds.map((sId: number) => ({ id: sId }))
};
}
const updatedSong = await prisma.song.update({
where: { id: Number(id) },
data,
include: { genres: true, specials: 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 });
}
}