@@ -0,0 +1,45 @@
|
||||
import { readFile } from "fs/promises";
|
||||
import { NextResponse } from "next/server";
|
||||
import path from "path";
|
||||
|
||||
import { resolveMediaUploadPath } from "@/lib/media-storage";
|
||||
|
||||
type MediaFileRouteProps = {
|
||||
params: {
|
||||
segments: string[];
|
||||
};
|
||||
};
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const CONTENT_TYPES: Record<string, string> = {
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".png": "image/png",
|
||||
".webp": "image/webp",
|
||||
".svg": "image/svg+xml",
|
||||
".pdf": "application/pdf",
|
||||
};
|
||||
|
||||
export async function GET(_: Request, { params }: MediaFileRouteProps) {
|
||||
const relativePath = params.segments.join("/");
|
||||
const publicPath = `/uploads/media/${relativePath}`;
|
||||
|
||||
try {
|
||||
const absolutePath = resolveMediaUploadPath(publicPath);
|
||||
const fileBuffer = await readFile(absolutePath);
|
||||
const contentType = CONTENT_TYPES[path.extname(absolutePath).toLowerCase()] ?? "application/octet-stream";
|
||||
|
||||
return new NextResponse(fileBuffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
return new NextResponse("Not Found", {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user