Add production-ready media library
CI / quality (push) Waiting to run

This commit is contained in:
MOH
2026-03-07 16:20:11 +01:00
parent ea64373853
commit 0f9f9e5f79
13 changed files with 1084 additions and 11 deletions
+45
View File
@@ -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,
});
}
}