Use MimeTypes guessMimeType for better and more extensible MimeType detection #3188
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rather than calling for and using
Finfo
directly, this modifies theSupport/File@getMimeType
method to instead callSymfony\Component\Mime\MimeTypes@guessMimeType
. Since by default thevendor/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 callsfinfo
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 themime_type
thevendor/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
mp3
s. The default finfo and binary scan, while good for most use-cases, is not great at detecting allmp3
s. 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.