fix: update file URL handling for production - supports both old and new URL formats

This commit is contained in:
Erik Silva
2026-01-20 23:01:50 -03:00
parent 1959047d75
commit ec962a7081
2 changed files with 41 additions and 13 deletions

View File

@@ -13,15 +13,22 @@ function xorEncrypt(data: Uint8Array, key: string): Uint8Array {
// Get internal MinIO URL for server-side fetching // Get internal MinIO URL for server-side fetching
function getInternalFileUrl(fileUrl: string): string { function getInternalFileUrl(fileUrl: string): string {
const minioEndpoint = process.env.MINIO_ENDPOINT || 'minio';
const minioPort = process.env.MINIO_PORT || '9000';
try { 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); const url = new URL(fileUrl);
// Extract bucket and file path from URL
const pathParts = url.pathname.split('/').filter(Boolean); const pathParts = url.pathname.split('/').filter(Boolean);
if (pathParts.length >= 2) { if (pathParts.length >= 2) {
const bucket = pathParts[0]; const bucket = pathParts[0];
const fileName = pathParts.slice(1).join('/'); 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}`; return `http://${minioEndpoint}:${minioPort}/${bucket}/${fileName}`;
} }
} catch (e) { } catch (e) {

View File

@@ -1,7 +1,7 @@
import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3"; import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";
export const s3Client = new S3Client({ 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", region: "us-east-1",
credentials: { credentials: {
accessKeyId: process.env.MINIO_ACCESS_KEY || "admin", 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"; 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) { export async function deleteFile(fileUrl: string) {
try { try {
// Extrair o key (nome do arquivo) da URL const key = extractFileKey(fileUrl);
// 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('/');
if (!key) { if (!key) {
console.error("Could not extract file key from URL:", fileUrl); console.error("Could not extract file key from URL:", fileUrl);
@@ -32,10 +52,11 @@ export async function deleteFile(fileUrl: string) {
Key: key, Key: key,
})); }));
console.log(`File deleted from MinIO: ${key}`); console.log(`File deleted: ${key}`);
return { success: true }; return { success: true };
} catch (error) { } catch (error) {
console.error("Error deleting file from MinIO:", error); console.error("Error deleting file:", error);
return { success: false }; return { success: false };
} }
} }