feat: servir arquivos do MinIO via API interna
This commit is contained in:
36
frontend/src/app/api/files/[...path]/route.ts
Normal file
36
frontend/src/app/api/files/[...path]/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { minioClient, bucketName } from '@/lib/minio';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ path: string[] }> }
|
||||
) {
|
||||
const { path } = await params;
|
||||
const key = path.join('/');
|
||||
|
||||
if (!key) {
|
||||
return NextResponse.json({ error: 'Arquivo não especificado' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const object = await minioClient.getObject(bucketName, key);
|
||||
|
||||
if (!object || !object.Body) {
|
||||
return NextResponse.json({ error: 'Arquivo não encontrado' }, { status: 404 });
|
||||
}
|
||||
|
||||
const contentType = object.ContentType || 'application/octet-stream';
|
||||
const body = object.Body instanceof Readable ? Readable.toWeb(object.Body) : object.Body;
|
||||
|
||||
return new Response(body as ReadableStream, {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[files] Erro ao buscar arquivo:', error);
|
||||
return NextResponse.json({ error: 'Erro ao buscar arquivo' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user