44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { readFile } from "fs/promises";
|
||
import { join } from "path";
|
||
|
||
export async function GET(
|
||
request: NextRequest,
|
||
{ params }: { params: Promise<{ path: string[] }> }
|
||
) {
|
||
try {
|
||
const { path } = await params;
|
||
const filePath = join(process.cwd(), "public", "uploads", ...path);
|
||
const fileBuffer = await readFile(filePath);
|
||
|
||
// Dosya uzantısına göre content-type belirle
|
||
const fileName = path[path.length - 1];
|
||
const ext = fileName.split('.').pop()?.toLowerCase();
|
||
|
||
const contentTypeMap: Record<string, string> = {
|
||
'jpg': 'image/jpeg',
|
||
'jpeg': 'image/jpeg',
|
||
'png': 'image/png',
|
||
'gif': 'image/gif',
|
||
'webp': 'image/webp',
|
||
'avif': 'image/avif',
|
||
'svg': 'image/svg+xml',
|
||
};
|
||
|
||
const contentType = contentTypeMap[ext || ''] || 'application/octet-stream';
|
||
|
||
return new NextResponse(fileBuffer, {
|
||
headers: {
|
||
'Content-Type': contentType,
|
||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||
},
|
||
});
|
||
} catch (error) {
|
||
console.error('Dosya okuma hatası:', error);
|
||
return NextResponse.json(
|
||
{ message: "Dosya bulunamadı" },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
}
|