diff --git a/app/admin/page.tsx b/app/admin/page.tsx index b497b62..03c9869 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -561,7 +561,7 @@ export default function AdminPage() { audioElement?.pause(); // Play new song - const audio = new Audio(`/uploads/${song.filename}`); + const audio = new Audio(`/api/audio/${song.filename}`); // Handle playback errors audio.onerror = () => { diff --git a/app/api/admin/daily-puzzles/route.ts b/app/api/admin/daily-puzzles/route.ts index 4910b84..3985071 100644 --- a/app/api/admin/daily-puzzles/route.ts +++ b/app/api/admin/daily-puzzles/route.ts @@ -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}` } })); diff --git a/app/api/audio/[filename]/route.ts b/app/api/audio/[filename]/route.ts new file mode 100644 index 0000000..1ad8e82 --- /dev/null +++ b/app/api/audio/[filename]/route.ts @@ -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 }); + } +} diff --git a/lib/dailyPuzzle.ts b/lib/dailyPuzzle.ts index 9cc582a..15a421c 100644 --- a/lib/dailyPuzzle.ts +++ b/lib/dailyPuzzle.ts @@ -98,7 +98,7 @@ export async function getOrCreateDailyPuzzle(genreName: string | null = null) { return { id: dailyPuzzle.id, - audioUrl: `/uploads/${dailyPuzzle.song.filename}`, + audioUrl: `/api/audio/${dailyPuzzle.song.filename}`, songId: dailyPuzzle.songId, title: dailyPuzzle.song.title, artist: dailyPuzzle.song.artist, @@ -185,7 +185,7 @@ export async function getOrCreateSpecialPuzzle(specialName: string) { return { id: dailyPuzzle.id, - audioUrl: `/uploads/${dailyPuzzle.song.filename}`, + audioUrl: `/api/audio/${dailyPuzzle.song.filename}`, songId: dailyPuzzle.songId, title: dailyPuzzle.song.title, artist: dailyPuzzle.song.artist,