Validating .msg file uploads using Laravel's Mime validation
Hey everyone, I'm trying to add .msg files (Microsoft Outlook's main file type for saving emails) to some existing file upload validation. I'm running into an issue where laravel is not guessing the correct Mime Type during validation. Logging the Mime Type before the validation runs returns "application/octet-stream" which according to the list of mime types in the laravel docs (http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) corresponds with these Mimes -"bin dms lrf mar so dist distz pkg bpk dump elc deploy". I do not want to allow any of these file types, only .msg files.
I think the correct mimeType for .msg files is "application/vnd.ms-outlook", but I can't find a reference to that in the MimeTypeExtensionGuesser.php file, or of the .msg extension. It seems that laravel does not support validation for this mime type out of the box, is there a way I can extend this file to add this mime type?
If anyone has any insight into supporting this file type, I'd really appreciate it. For reference I'm running laravel 5.2. Thanks!
I think the only way to do this is creating a custom validation rule for that
Validator::extend('msg-mime', function($attribute, $value, $parameters, $validator) {
// CHeck if the value is a file
// Check if the mimetype is correct
// Return true if mime type or extension is correct
// Return false if wrong mime type or extension
});
@BOBBYBOUWMANN - Thanks Bobby, makes sense that would be a good way to do it. Do you know if its possible to specify an Or during the validation, for example could I use the validation facade in a way that it would check either the default laravel mime validation OR my custom rule? If not, I was thinking about manually making a validator instance to check for the .msg using my custom validation rule, and then running the validation for the other types if that one fails, but I'm not sure if there's a simpler way to do it. Thanks for your help!
@BOBBYBOUWMANN - Thanks Bobby, got it working using custom validation! I really appreciate the help!
If anyone finds this and is curious how to check for the .msg Mime specifically, what I did is just use fopen to read the first few bytes and check those to determine mime type