diff --git a/src/app/api/view/[id]/route.ts b/src/app/api/view/[id]/route.ts index 34e0c6b..fa87680 100644 --- a/src/app/api/view/[id]/route.ts +++ b/src/app/api/view/[id]/route.ts @@ -13,15 +13,22 @@ function xorEncrypt(data: Uint8Array, key: string): Uint8Array { // Get internal MinIO URL for server-side fetching function getInternalFileUrl(fileUrl: string): string { + const minioEndpoint = process.env.MINIO_ENDPOINT || 'minio'; + const minioPort = process.env.MINIO_PORT || '9000'; + try { + // Format 1: /api/files/bucket/filename (new relative URL) + if (fileUrl.startsWith('/api/files/')) { + const pathPart = fileUrl.replace('/api/files/', ''); + return `http://${minioEndpoint}:${minioPort}/${pathPart}`; + } + + // Format 2: http://host:port/bucket/filename (old absolute URL) const url = new URL(fileUrl); - // Extract bucket and file path from URL const pathParts = url.pathname.split('/').filter(Boolean); if (pathParts.length >= 2) { const bucket = pathParts[0]; const fileName = pathParts.slice(1).join('/'); - const minioEndpoint = process.env.MINIO_ENDPOINT || 'minio'; - const minioPort = process.env.MINIO_PORT || '9000'; return `http://${minioEndpoint}:${minioPort}/${bucket}/${fileName}`; } } catch (e) { diff --git a/src/lib/s3.ts b/src/lib/s3.ts index 302abe0..7dc5546 100644 --- a/src/lib/s3.ts +++ b/src/lib/s3.ts @@ -1,7 +1,7 @@ import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3"; export const s3Client = new S3Client({ - endpoint: `http://${process.env.MINIO_ENDPOINT}:${process.env.MINIO_PORT}`, + endpoint: `http://${process.env.MINIO_ENDPOINT || 'minio'}:${process.env.MINIO_PORT || '9000'}`, region: "us-east-1", credentials: { accessKeyId: process.env.MINIO_ACCESS_KEY || "admin", @@ -12,15 +12,35 @@ export const s3Client = new S3Client({ export const BUCKET_NAME = process.env.MINIO_BUCKET || "portal-transparencia"; -// Função para deletar arquivo do MinIO +// Extract file key from various URL formats +function extractFileKey(fileUrl: string): string | null { + try { + // Format 1: /api/files/bucket/filename (new relative URL) + if (fileUrl.startsWith('/api/files/')) { + const parts = fileUrl.replace('/api/files/', '').split('/'); + if (parts.length >= 2) { + // Skip bucket name, get the rest as key + return parts.slice(1).join('/'); + } + } + + // Format 2: http://host:port/bucket/filename (old absolute URL) + const url = new URL(fileUrl); + const pathParts = url.pathname.split('/').filter(Boolean); + if (pathParts.length >= 2) { + // Skip bucket name, get the rest as key + return pathParts.slice(1).join('/'); + } + } catch (e) { + console.error("Error parsing file URL:", e); + } + return null; +} + +// Delete file from storage export async function deleteFile(fileUrl: string) { try { - // Extrair o key (nome do arquivo) da URL - // URL format: http://host:port/bucket/filename - const url = new URL(fileUrl); - const pathParts = url.pathname.split('/'); - // Remove empty string and bucket name, get the file key - const key = pathParts.slice(2).join('/'); + const key = extractFileKey(fileUrl); if (!key) { console.error("Could not extract file key from URL:", fileUrl); @@ -32,10 +52,11 @@ export async function deleteFile(fileUrl: string) { Key: key, })); - console.log(`File deleted from MinIO: ${key}`); + console.log(`File deleted: ${key}`); return { success: true }; } catch (error) { - console.error("Error deleting file from MinIO:", error); + console.error("Error deleting file:", error); return { success: false }; } } +