import { CallHandler , ExecutionContext , Injectable , Logger , mixin , NestInterceptor , Type , } from '@nestjs/common' ; import { FileInterceptor } from '@nestjs/platform-express' ; import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface' ; import { Observable } from 'rxjs' ; import { AzureStorageService , AzureStorageOptions , } from './azure-storage.service' ; export function AzureStorageFileInterceptor ( fieldName , localOptions ? , azureStorageOptions ? ) { @ Injectable ( ) class MixinInterceptor implements NestInterceptor { interceptor : NestInterceptor ; constructor ( private readonly azureStorage ) { this . interceptor = new ( FileInterceptor ( fieldName , localOptions ) ) ( ) ; } async intercept ( context , next ) { ( await this . interceptor . intercept ( context , next ) ) as Observable < any > ; const request = context . switchToHttp ( ) . getRequest ( ) ; const file = request [ fieldName ] ; if ( ! file ) { Logger . warn ( 'AzureStorageFileInterceptor' , `Can not intercept field "${ fieldName }". Did you specify the correct field name in @AzureStorageFileInterceptor('${ fieldName }')?` ) ; return next . handle ( ) ; } const storageUrl = await this . azureStorage . upload ( file , azureStorageOptions ) ; file . storageUrl = storageUrl ; return next . handle ( ) ; } } const Interceptor = mixin ( MixinInterceptor ) ; return Interceptor as Type < NestInterceptor > ; }