Files
hoerdle/app/api/audio/[filename]/route.ts

36 lines
1.1 KiB
TypeScript

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 });
}
}