From 0a42a328173ecfc74514ca4dfa356b7e11eff5cd Mon Sep 17 00:00:00 2001 From: Oleg Proskurin Date: Thu, 25 Dec 2025 23:35:52 +0700 Subject: [PATCH] feat: use CDN urls --- .../src/services/MinioStorageService.ts | 23 +++++++++++++++++-- infrastructure/.env.example | 5 ++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/apps/api-service/src/services/MinioStorageService.ts b/apps/api-service/src/services/MinioStorageService.ts index 40e9901..6e15b5a 100644 --- a/apps/api-service/src/services/MinioStorageService.ts +++ b/apps/api-service/src/services/MinioStorageService.ts @@ -268,6 +268,13 @@ export class MinioStorageService implements StorageService { await this.client.removeObject(this.bucketName, filePath); } + /** + * Get public URL for file access + * Returns CDN URL if MINIO_PUBLIC_URL is configured (production), + * otherwise falls back to API endpoint URL (development) + * + * @returns {string} URL for accessing the file + */ getPublicUrl( orgId: string, projectId: string, @@ -275,9 +282,21 @@ export class MinioStorageService implements StorageService { filename: string, ): string { this.validateFilePath(orgId, projectId, category, filename); - // Production-ready: Return API URL for presigned URL access + + // If MINIO_PUBLIC_URL is configured, use direct CDN access + // This provides better performance and reduces API server load + if (this.publicUrl && process.env['USE_DIRECT_CDN'] !== 'false') { + const filePath = this.getFilePath(orgId, projectId, category, filename); + const cdnUrl = `${this.publicUrl}/${this.bucketName}/${filePath}`; + console.log(`[MinIO] Using CDN URL: ${cdnUrl}`); + return cdnUrl; + } + + // Fallback to API URL for local development or when CDN is disabled const apiBaseUrl = process.env['API_BASE_URL'] || 'http://localhost:3000'; - return `${apiBaseUrl}/api/images/${orgId}/${projectId}/${category}/${filename}`; + const apiUrl = `${apiBaseUrl}/api/images/${orgId}/${projectId}/${category}/${filename}`; + console.log(`[MinIO] Using API URL: ${apiUrl}`); + return apiUrl; } async getPresignedUploadUrl( diff --git a/infrastructure/.env.example b/infrastructure/.env.example index 7173962..ff5e5f5 100644 --- a/infrastructure/.env.example +++ b/infrastructure/.env.example @@ -34,6 +34,11 @@ STORAGE_TYPE=minio # Public URL for CDN access (used in API responses) MINIO_PUBLIC_URL=https://cdn.banatie.app +# Use direct CDN URLs instead of API proxy (recommended for production) +# Set to 'false' to force API URLs even when MINIO_PUBLIC_URL is configured +# Default: true (CDN enabled when MINIO_PUBLIC_URL is present) +USE_DIRECT_CDN=true + # ---------------------------------------- # API Configuration # ----------------------------------------