I am facing an issue with implementing a Nestjs route in a controller that includes a file upload using Multer. The goal is to edit a user's profile picture, so I need to validate that the uploaded file is an image. However, despite using the FileTypeValidator, I am unable to get it to work properly. Every time I try to upload a file, it gets denied.
@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> {
// ...
}