Fix audio playback for newly uploaded files by using dynamic API route

This commit is contained in:
Hördle Bot
2025-11-22 17:53:37 +01:00
parent 6086e1903c
commit 7fc1c2c201
4 changed files with 39 additions and 4 deletions

View File

@@ -38,7 +38,7 @@ export async function GET() {
title: puzzle.song.title,
artist: puzzle.song.artist,
filename: puzzle.song.filename,
audioUrl: `/uploads/${puzzle.song.filename}`
audioUrl: `/api/audio/${puzzle.song.filename}`
}
}));

View File

@@ -0,0 +1,35 @@
import { NextRequest, NextResponse } from 'next/server';
import { readFile, stat } from 'fs/promises';
import path from 'path';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ filename: string }> }
) {
try {
const { filename } = await params;
const filePath = path.join(process.cwd(), 'public/uploads', filename);
// Check if file exists
try {
await stat(filePath);
} catch {
return new NextResponse('File not found', { status: 404 });
}
// Read file
const fileBuffer = await readFile(filePath);
// Return with proper headers
return new NextResponse(fileBuffer, {
headers: {
'Content-Type': 'audio/mpeg',
'Accept-Ranges': 'bytes',
'Cache-Control': 'public, max-age=3600, must-revalidate',
},
});
} catch (error) {
console.error('Error serving audio file:', error);
return new NextResponse('Internal Server Error', { status: 500 });
}
}