Files
hoerdle/app/api/songs/route.ts
Hördle Bot 29d43effe3 feat: Add cover art support and auto-migration
- Extract cover art from MP3s during upload
- Display cover art in game result screens (win/loss)
- Add coverImage field to Song model
- Add migration script to backfill covers for existing songs
- Configure Docker to run migration script on startup
2025-11-21 15:51:22 +01:00

167 lines
5.1 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,
},
});
// 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,
}));
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());
// Extract metadata from file
try {
const metadata = await parseBuffer(buffer, file.type);
if (metadata.common.title) {
title = metadata.common.title;
}
if (metadata.common.artist) {
artist = metadata.common.artist;
}
} catch (e) {
console.error('Failed to parse metadata:', e);
}
// Fallback if still missing
if (!title) title = 'Unknown Title';
if (!artist) artist = 'Unknown Artist';
const filename = `${Date.now()}-${file.name.replace(/[^a-zA-Z0-9.]/g, '_')}`;
const uploadDir = path.join(process.cwd(), 'public/uploads');
await writeFile(path.join(uploadDir, filename), buffer);
// Handle cover image
let coverImage = null;
try {
const metadata = await parseBuffer(buffer, file.type);
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,
},
});
return NextResponse.json(song);
} 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 } = await request.json();
if (!id || !title || !artist) {
return NextResponse.json({ error: 'Missing fields' }, { status: 400 });
}
const updatedSong = await prisma.song.update({
where: { id: Number(id) },
data: { title, artist },
});
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 });
}
}