Files
image-apiv2/app/uploads/[...path]/route.ts
Beyhan Oğur 71eff2d979 first commit
2026-04-26 22:09:32 +03:00

44 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 }
);
}
}