I have the multipart form to be validated before file upload in nestjs application. the thing is that I don't want the file to be uploaded if validation of body fails. here is how I wrote the code for.
// User controller method for create user with upload image
@Post()
@UseInterceptors(FileInterceptor('image'))
create(
@Body() userInput: CreateUserDto,
@UploadedFile(
new ParseFilePipe({
validators: [
// some validator here
]
})
) image: Express.Multer.File,
) {
return this.userService.create({ ...userInput, image: image.path });
}
Tried so many ways to turn around this issue, but didn't reach to any solution
CodePudding user response:
Interceptors run before pipes do, so there's no way to make the saving of the file not happen unless you manage that yourself in your service. However, another option could be a custom exception filter that unlink
s the file on error so that you don't have to worry about it post-upload