33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
// turbopackIgnore: true — IMAGE_DIR är en runtime env-variabel, inte statisk sökväg
|
|
const IMAGE_DIR: string = /* turbopackIgnore: true */ (process.env.IMAGE_DIR || '/app/public/images') as string;
|
|
|
|
export async function GET(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ filename: string }> },
|
|
) {
|
|
const { filename } = await params;
|
|
|
|
// Förhindra path traversal
|
|
if (!filename || filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
|
|
return new NextResponse('Not found', { status: 404 });
|
|
}
|
|
|
|
const filePath = path.join(IMAGE_DIR, filename);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
return new NextResponse('Not found', { status: 404 });
|
|
}
|
|
|
|
const file = fs.readFileSync(filePath);
|
|
return new NextResponse(file, {
|
|
headers: {
|
|
'Content-Type': 'image/jpeg',
|
|
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
},
|
|
});
|
|
}
|