async upload(
@UploadedFile() file: Express.Multer.File,
@Body() body: FileUploadDto,
) {
const forbiddenExt = [
'.exe',
'.bat',
];
const fileName = file.filename || file.originalname;
const fileExtension = fileName
.slice(((fileName.lastIndexOf('.') - 1) >>> 0) + 2);
const hasForbiddenExtension = forbiddenExt.includes(fileExtension);
if (hasForbiddenExtension) {
throw new HttpException(
'File type is not supported. Upload only .apk or .ipa',
400,
);
}
const response = await this.uploadFile(
`${
body.filename
}`,
file.buffer,
);
return {
success: true,
};
}
above code works fine on mac its validating .exe file properly but windows I'm able to upload .exe
files still how to avoid? validation not working only on windows
Need to normalize the fileExtension
const fileName = file.filename || file.originalname;
const fileExtension = fileName
.slice(((fileName.lastIndexOf('.') - 1) >>> 0) + 2)
.toLowerCase();
const normalizedExtension = fileExtension.startsWith('.')
? fileExtension
: `.${fileExtension}`;