Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MimeTypes guessMimeType for better and more extensible MimeType detection #3188

Merged
merged 2 commits into from
Feb 17, 2023

Conversation

Orrison
Copy link
Contributor

@Orrison Orrison commented Feb 16, 2023

Rather than calling for and using Finfo directly, this modifies the Support/File@getMimeType method to instead call Symfony\Component\Mime\MimeTypes@guessMimeType. Since by default the vendor/symfony/mime/FileinfoMimeTypeGuesser is registered last, it will be used. Resulting in the same functionality as before by default with a few added benefits.

First, the default and first Guesser vendor/symfony/mime/FileinfoMimeTypeGuesser, while virtually the same as the original implementation as it calls finfo in almost the same way, should be slightly more robust.

Second, for any reason if the vendor/symfony/mime/FileinfoMimeTypeGuesser is unable to be used or fails to recognize the mime_type the vendor/symfony/mime/FileBinaryMimeTypeGuesser is called.

Finally, the most important benefit, is this allows for extensibility in mime_type detection. A user can register a new Guesser which will be used first.
There are many reasons a user would want to do this, one of the most prominent and my personal use case is making and using a custom Guesser (using getID3) that is better at recognizing mp3s. The default finfo and binary scan, while good for most use-cases, is not great at detecting all mp3s. With this change a User can add a custom Guesser to work how ever they need.

No additional tests were added as tests/Support/FileTest.php should be sufficient as it is still passing after this change. Though I can also add a test for registering a new Guesser if that is wanted.

@freekmurze freekmurze merged commit a6d562d into spatie:main Feb 17, 2023
@freekmurze
Copy link
Member

Thank you!

@Orrison Orrison deleted the feature/modifiable-mime-type-detect branch February 17, 2023 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants