@@ -12,10 +12,40 @@ export class MinioService implements OnModuleInit {
1212 private bucketInitialized = false ;
1313 constructor ( @Inject ( MINIO_CONFIG ) private readonly config : IMinioModuleOptions ) { }
1414
15+ /**
16+ * Parses an endpoint string to extract host and port
17+ * Supports formats: "host", "host:port", "host:port/path"
18+ */
19+ private parseEndpoint ( endpoint : string ) : { host : string ; port ?: number } {
20+ // Remove protocol if present
21+ const cleanEndpoint = endpoint . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
22+
23+ // Split by colon to get host and potential port
24+ const parts = cleanEndpoint . split ( ':' ) ;
25+
26+ if ( parts . length === 1 ) {
27+ // No port specified, use default based on SSL
28+ return { host : parts [ 0 ] } ;
29+ }
30+
31+ // Extract port (may have path after it)
32+ const portPart = parts [ 1 ] . split ( '/' ) [ 0 ] ;
33+ const port = parseInt ( portPart , 10 ) ;
34+
35+ if ( isNaN ( port ) ) {
36+ // Invalid port, treat as hostname with colon
37+ return { host : cleanEndpoint } ;
38+ }
39+
40+ return { host : parts [ 0 ] , port } ;
41+ }
42+
1543 async onModuleInit ( ) : Promise < void > {
44+ const { host, port } = this . parseEndpoint ( this . config . endPoint ) ;
45+
1646 this . minioClient = new Minio . Client ( {
17- endPoint : this . config . endPoint ,
18- port : this . config . port ,
47+ endPoint : host ,
48+ port : port ,
1949 useSSL : this . config . useSSL ,
2050 accessKey : this . config . accessKey ,
2151 secretKey : this . config . secretKey ,
@@ -130,11 +160,12 @@ export class MinioService implements OnModuleInit {
130160 const accessKey = this . config . accessKey ;
131161 const secretKey = this . config . secretKey ;
132162 const region = this . config . region || 'us-east-1' ;
133- const externalHost = endPoint ;
134- const port = this . config . port ;
135-
136- // Use host without port for signing
137- const signingHost = port ? `${ externalHost } :${ port } ` : externalHost ;
163+
164+ // Parse endpoint to get host and port
165+ const { host, port } = this . parseEndpoint ( endPoint ) ;
166+
167+ // Use host with port for signing (if port is specified)
168+ const signingHost = port ? `${ host } :${ port } ` : host ;
138169
139170 const currentDate = new Date ( ) ;
140171 const amzDate = this . getAmzDate ( currentDate ) ;
@@ -215,11 +246,11 @@ export class MinioService implements OnModuleInit {
215246
216247 async getPresignedUrl ( bucketName : string , objectName : string ) : Promise < string > {
217248 if ( ! this . config . buckets . private . includes ( bucketName ) ) {
249+ // For public buckets, return direct URL
218250 const endpoint = this . config . externalEndPoint || this . config . endPoint ;
219251 const protocol = ( this . config . externalUseSSL ?? this . config . useSSL ) ? 'https' : 'http' ;
220- return `${ protocol } ://${ endpoint } ${
221- this . config . port ? `:${ this . config . port } ` : ''
222- } /${ bucketName } /${ objectName } `;
252+ // Endpoint already includes port if specified
253+ return `${ protocol } ://${ endpoint } /${ bucketName } /${ objectName } ` ;
223254 }
224255
225256 return await this . calculatePresignedGetUrl (
0 commit comments