Last active
February 22, 2024 05:00
-
-
Save TimVosch/e2d8504c43b60d9b23cb0796b4127772 to your computer and use it in GitHub Desktop.
Get request parameters in FileInterceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, UseInterceptors, Post } from '@nestjs/common'; | |
import { AppService } from './app.service'; | |
import { diskStorage } from 'multer'; | |
import { Request } from 'express'; | |
import { MyNewFileInterceptor } from './file.interceptor'; | |
@Controller() | |
export class AppController { | |
constructor(private readonly appService: AppService) {} | |
@Post('/profile/:id/picture') | |
@UseInterceptors( | |
/** | |
* The `options` is a now function executed on intercept. | |
* This function receives the context parameter allowing you | |
* to use get the request and subsequentially the parameters | |
*/ | |
MyNewFileInterceptor('picture', ctx => { | |
// Get request from Context | |
const req = ctx.switchToHttp().getRequest() as Request; | |
// Return the options | |
return { | |
storage: diskStorage({ | |
destination: `.upload/profile/${req.params.id}/picture`, | |
// tslint:disable-next-line: variable-name | |
filename: (_req, file, cb) => { | |
const randomName = Array(32) | |
.fill(null) | |
.map(() => Math.round(Math.random() * 16).toString(16)) | |
.join(''); | |
return cb(null, `${randomName}-${file.originalname}`); | |
}, | |
}), | |
}; | |
}), | |
) | |
uploadProfilePicture(): string { | |
return this.appService.getHello(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FileInterceptor, MulterModuleOptions } from '@nestjs/platform-express'; | |
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'; | |
import { MULTER_MODULE_OPTIONS } from '@nestjs/platform-express/multer/files.constants'; | |
import * as multer from 'multer'; | |
import { | |
ExecutionContext, | |
Optional, | |
Inject, | |
CallHandler, | |
mixin, | |
Type, | |
NestInterceptor, | |
} from '@nestjs/common'; | |
export const MyNewFileInterceptor = ( | |
fieldName: string, | |
localOptions?: (context: ExecutionContext) => MulterOptions, | |
) => { | |
const FileInterceptorInstance = FileInterceptor(fieldName); | |
class MixinInterceptor extends FileInterceptorInstance { | |
protected multer: any; | |
protected moduleOptions: {}; | |
constructor( | |
@Optional() | |
@Inject(MULTER_MODULE_OPTIONS) | |
options: MulterModuleOptions = {}, | |
) { | |
super(); | |
this.moduleOptions = options; | |
} | |
intercept(context: ExecutionContext, next: CallHandler<any>): any { | |
this.multer = (multer as any)({ | |
...this.moduleOptions, | |
...localOptions(context), | |
}); | |
return super.intercept(context, next); | |
} | |
} | |
const Interceptor = mixin(MixinInterceptor); | |
return Interceptor as Type<NestInterceptor>; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@UseInterceptors( | |
FileInterceptor('picture', { | |
storage: diskStorage({ | |
destination: function(req, file, cb) { | |
cb(null, `.upload/profile/${req.params.id}/picture`); | |
}, | |
// tslint:disable-next-line: variable-name | |
filename: (_req, file, cb) => { | |
const randomName = Array(32) | |
.fill(null) | |
.map(() => Math.round(Math.random() * 16).toString(16)) | |
.join(''); | |
return cb(null, `${randomName}-${file.originalname}`); | |
}, | |
}), | |
}), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The snippet was exactly what I needed! Thank you!
This was my longer implementation prior to receiving the response
upload.controller.ts
upload.service.ts
The new one is a lot shorter haha