mirror of
https://github.com/BreizhHardware/Site-comptage-heure.git
synced 2026-03-18 21:30:40 +01:00
feat: update file upload paths and add dynamic file retrieval endpoint
This commit is contained in:
@@ -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}` });
|
||||
}
|
||||
|
||||
31
app/api/uploads/[filename]/route.ts
Normal file
31
app/api/uploads/[filename]/route.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user