Prevent the file from being uploaded if the mimeType is invalid
Discussed in https://github.com/payloadcms/payload/discussions/937
Originally posted by predaytor August 14, 2022 Сurrently, regardless of the configuration, the data is loaded to the disk, even after displaying an invalid mimeType error.
upload: {
staticURL: '/uploads',
staticDir: path.resolve(__dirname, '../../uploads'),
mimeTypes: ['image/*'],
}


Successfully recreated issue. Looking into a fix now.
Right now the create handler writes the uploaded file so that the validation can be performed on it in the beforeValidation hook. That means the file is already written before the error is returned.
To change that we have a few options: A) Split the upload handling into two parts, first write the file, then validate it, and if validation fails delete the file. B) Figure out if we can reliably validate the file as it is coming in as a steam before saving the file so that it is never persisted when invalid.
If we can reliably get B to work that would be ideal as it will take up less bandwidth / IO. I did find this package that might help: https://github.com/sindresorhus/file-type/ There are some types it doesn't cover and we'll need to figure out the details on how this will work.