@@ -4,7 +4,6 @@ import { map } from 'rxjs/operators';
44import { MinioService } from '../minio.service' ;
55import { Socket } from 'net' ;
66import { IncomingMessage , ServerResponse } from 'http' ;
7- import { MINIO_FILE_FIELD_METADATA } from '../constants' ;
87
98@Injectable ( )
109export class FileUrlTransformInterceptor implements NestInterceptor {
@@ -31,6 +30,7 @@ export class FileUrlTransformInterceptor implements NestInterceptor {
3130
3231 /**
3332 * Transforms URLs in the given data to presigned URLs using the Minio service.
33+ * Only processes strings that start with minio:// prefix.
3434 * @param data - The data to transform.
3535 * @returns The transformed data with URLs replaced by presigned URLs.
3636 */
@@ -63,24 +63,14 @@ export class FileUrlTransformInterceptor implements NestInterceptor {
6363 return obj ;
6464 }
6565
66- // Get the schema if it's a Mongoose document
67- const schema = data . schema || ( data . constructor && data . constructor . schema ) ;
68-
6966 // Process each property recursively
7067 for ( const [ key , value ] of Object . entries ( obj ) ) {
71- // Check if this field is decorated with FileSchemaField or FileColumn
72- const isFileField =
73- schema ?. paths ?. [ key ] ?. options ?. isFileField ||
74- this . hasFileFieldMetadata ( data , key ) ||
75- this . hasFileFieldMetadata ( obj , key ) ;
76-
77- const inferredPath = ! isFileField ? this . extractMinioPath ( value ) : null ;
78-
79- if ( ( isFileField || inferredPath ) && typeof value === 'string' ) {
80- const split = inferredPath ?? this . splitBucketAndObject ( value ) ;
81- if ( split ) {
68+ // Only process strings that start with minio:// prefix
69+ if ( typeof value === 'string' && value . startsWith ( 'minio://' ) ) {
70+ const minioPath = this . minioService . parseMinioUrl ( value ) ;
71+ if ( minioPath ) {
8272 try {
83- obj [ key ] = await this . minioService . getPresignedUrl ( split . bucketName , split . objectName ) ;
73+ obj [ key ] = await this . minioService . getPresignedUrl ( minioPath . bucketName , minioPath . objectName ) ;
8474 } catch ( error ) {
8575 this . logger . error ( `Error generating presigned URL for ${ key } :` , error ) ;
8676 }
@@ -107,42 +97,4 @@ export class FileUrlTransformInterceptor implements NestInterceptor {
10797 return obj ;
10898 }
10999
110- private hasFileFieldMetadata ( target : unknown , propertyKey : string ) : boolean {
111- if ( ! target ) return false ;
112-
113- const directMetadata = Reflect . getMetadata ( MINIO_FILE_FIELD_METADATA , target , propertyKey ) ;
114- if ( directMetadata ) {
115- return true ;
116- }
117-
118- const prototype = typeof target === 'object' ? Object . getPrototypeOf ( target ) : undefined ;
119- if ( ! prototype ) {
120- return false ;
121- }
122-
123- return Boolean ( Reflect . getMetadata ( MINIO_FILE_FIELD_METADATA , prototype , propertyKey ) ) ;
124- }
125-
126- private extractMinioPath ( value : unknown ) : { bucketName : string ; objectName : string } | null {
127- if ( typeof value !== 'string' ) {
128- return null ;
129- }
130-
131- const split = this . splitBucketAndObject ( value ) ;
132- return split ;
133- }
134-
135- private splitBucketAndObject ( value : string ) : { bucketName : string ; objectName : string } | null {
136- if ( ! value . includes ( '/' ) ) {
137- return null ;
138- }
139- const [ bucketName , ...pathParts ] = value . split ( '/' ) ;
140- if ( ! bucketName || pathParts . length === 0 ) {
141- return null ;
142- }
143- return {
144- bucketName,
145- objectName : pathParts . join ( '/' ) ,
146- } ;
147- }
148100}
0 commit comments