优化docker
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user