feat: update file upload paths and add dynamic file retrieval endpoint

This commit is contained in:
2025-10-18 20:05:11 +02:00
parent fc13c94d5f
commit 1994ed5810
7 changed files with 54 additions and 4 deletions

View File

@@ -27,10 +27,10 @@ export async function POST(request: NextRequest) {
const buffer = Buffer.from(bytes);
const filename = `${Date.now()}-${file.name}`;
const filepath = path.join(process.cwd(), 'public', 'uploads', filename);
const filepath = path.join(process.cwd(), 'uploads', filename);
await mkdir(path.dirname(filepath), { recursive: true });
await writeFile(filepath, buffer);
return NextResponse.json({ path: `/uploads/${filename}` });
return NextResponse.json({ path: `/api/uploads/${filename}` });
}

View File

@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from 'next/server';
import { readFile } from 'fs/promises';
import path from 'path';
import mime from 'mime-types';
export const dynamic = 'force-dynamic';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ filename: string }> },
) {
const { filename } = await params;
const filepath = path.join(process.cwd(), 'uploads', filename);
try {
const file = await readFile(filepath);
const mimeType = mime.lookup(filename) || 'application/octet-stream';
const uint8Array = new Uint8Array(file);
return new NextResponse(uint8Array as any, {
headers: {
'Content-Type': mimeType,
'Cache-Control': 'public, max-age=31536000',
},
});
} catch (error) {
return NextResponse.json({ error: 'File not found' }, { status: 404 });
}
}