39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { stat, readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { resolveAudioFilePath } from "@/lib/audio";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type RouteContext = {
|
|
params: Promise<{ path: string[] }>;
|
|
};
|
|
|
|
export async function GET(_request: NextRequest, context: RouteContext) {
|
|
const { path: audioPath } = await context.params;
|
|
const filePath = resolveAudioFilePath(audioPath);
|
|
|
|
if (!filePath || path.extname(filePath).toLowerCase() !== ".mp3") {
|
|
return new NextResponse("Not found", { status: 404 });
|
|
}
|
|
|
|
try {
|
|
const fileStat = await stat(filePath);
|
|
if (!fileStat.isFile()) {
|
|
return new NextResponse("Not found", { status: 404 });
|
|
}
|
|
|
|
const file = await readFile(filePath);
|
|
return new NextResponse(new Uint8Array(file), {
|
|
headers: {
|
|
"Content-Type": "audio/mpeg",
|
|
"Content-Length": String(fileStat.size),
|
|
"Cache-Control": "public, max-age=31536000, immutable",
|
|
},
|
|
});
|
|
} catch {
|
|
return new NextResponse("Not found", { status: 404 });
|
|
}
|
|
}
|