feat: servir arquivos do MinIO via API interna

This commit is contained in:
Erik
2025-11-27 15:20:30 -03:00
parent 30ddb5392e
commit ca3eac5e1e
5 changed files with 69 additions and 23 deletions

View File

@@ -72,11 +72,8 @@ export async function POST(request: NextRequest) {
}
);
// Generate public URL
const protocol = process.env.MINIO_USE_SSL === 'true' ? 'https' : 'http';
const endpoint = process.env.MINIO_ENDPOINT || 'localhost';
const port = process.env.MINIO_PORT || '9000';
const avatarUrl = `${protocol}://${endpoint}:${port}/${BUCKET_NAME}/${fileName}`;
const avatarPath = fileName;
const avatarUrl = `/api/files/${avatarPath}`;
// Delete old avatar if exists
const user = await prisma.user.findUnique({
@@ -86,10 +83,17 @@ export async function POST(request: NextRequest) {
if (user?.avatar) {
try {
// Extract filename from URL
const oldFileName = user.avatar.split(`${BUCKET_NAME}/`)[1];
if (oldFileName) {
await minioClient.removeObject(BUCKET_NAME, oldFileName);
const sanitized = user.avatar.replace(/^https?:\/\/[^/]+/i, '');
let objectKey: string | undefined;
if (sanitized.startsWith('/api/files/')) {
objectKey = sanitized.replace('/api/files/', '');
} else if (sanitized.includes(`${BUCKET_NAME}/`)) {
objectKey = sanitized.split(`${BUCKET_NAME}/`)[1];
}
if (objectKey) {
await minioClient.removeObject(BUCKET_NAME, objectKey);
}
} catch (error) {
console.error('Error deleting old avatar:', error);

View 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 });
}
}

View File

@@ -20,11 +20,9 @@ export async function POST(request: Request) {
'Content-Type': file.type,
});
// Construct public URL
// In a real production env, this should be an env var like NEXT_PUBLIC_STORAGE_URL
const url = `http://localhost:9000/${bucketName}/${filename}`;
const url = `/api/files/${filename}`;
return NextResponse.json({ url });
return NextResponse.json({ url, path: filename });
} catch (error) {
console.error('Upload error:', error);
return NextResponse.json({ error: 'Error uploading file' }, { status: 500 });

View File

@@ -80,11 +80,8 @@ export async function POST(request: NextRequest) {
}
);
// Generate public URL
const protocol = process.env.MINIO_USE_SSL === 'true' ? 'https' : 'http';
const endpoint = process.env.MINIO_ENDPOINT || 'localhost';
const port = process.env.MINIO_PORT || '9000';
const avatarUrl = `${protocol}://${endpoint}:${port}/${BUCKET_NAME}/${fileName}`;
const avatarPath = fileName;
const avatarUrl = `/api/files/${avatarPath}`;
// Delete old avatar if exists
const user = await prisma.user.findUnique({
@@ -94,10 +91,17 @@ export async function POST(request: NextRequest) {
if (user?.avatar) {
try {
// Extract filename from URL
const oldFileName = user.avatar.split(`${BUCKET_NAME}/`)[1];
if (oldFileName) {
await minioClient.removeObject(BUCKET_NAME, oldFileName);
const sanitized = user.avatar.replace(/^https?:\/\/[^/]+/i, '');
let objectKey: string | undefined;
if (sanitized.startsWith('/api/files/')) {
objectKey = sanitized.replace('/api/files/', '');
} else if (sanitized.includes(`${BUCKET_NAME}/`)) {
objectKey = sanitized.split(`${BUCKET_NAME}/`)[1];
}
if (objectKey) {
await minioClient.removeObject(BUCKET_NAME, objectKey);
}
} catch (error) {
console.error('Error deleting old avatar:', error);