How can I validate a file type using Nestjs Pipes and FileTypeValidator

Viewed 14

I have implemented a simple Nestjs route inside of an controller with a file upload. The file is handled with Multer. Since its porpuse is to edit a profile picture of a user I need to validate the file to be an image. However for some reason I can't get it running with the FileTypeValidator. The uploaded file is being denied each time.

@UseInterceptors(
  FileInterceptor('file', {
    storage: MulterService.getStorage((req, file, cb) => {
      const filename = `${uuidv4()}`;
      const extension = path.parse(file.originalname).ext;

      cb(null, `${filename}${extension}`);
    }, MulterService.destinations.profilePictures),
  })
)
@Post('profile-picture')
editProfilePicture(
  @UploadedFile(
    new ParseFilePipe({
      validators: [new FileTypeValidator({ fileType: 'png' })],
      // png files always denied
      // /\^(jpeg|jpg|png|gif)$/ regex isn't working either
    })
  )
  file: Express.Multer.File
): Promise<User> {
  // ...
}
0 Answers
Related