43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { readFileSync, existsSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
|
|
const CONTENT_DIR = join(process.cwd(), 'content');
|
|
|
|
const MIME_TYPES: Record<string, string> = {
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.png': 'image/png',
|
|
'.gif': 'image/gif',
|
|
'.webp': 'image/webp',
|
|
'.svg': 'image/svg+xml',
|
|
'.pdf': 'application/pdf',
|
|
'.txt': 'text/plain',
|
|
'.md': 'text/plain',
|
|
'.mdx': 'text/plain',
|
|
};
|
|
|
|
export async function GET(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ path: string[] }> },
|
|
) {
|
|
const { path } = await params;
|
|
const decodedPath = decodeURIComponent(path.join('/'));
|
|
const fullPath = join(CONTENT_DIR, decodedPath);
|
|
|
|
if (!fullPath.startsWith(CONTENT_DIR) || !existsSync(fullPath)) {
|
|
return new NextResponse('Not found', { status: 404 });
|
|
}
|
|
|
|
const ext = '.' + fullPath.split('.').pop()!;
|
|
const contentType = MIME_TYPES[ext] ?? 'application/octet-stream';
|
|
const data = readFileSync(fullPath);
|
|
|
|
return new NextResponse(data, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
},
|
|
});
|
|
}
|