Problem/Motivation
MimeTypeGuesser is a service for guessing the mime type of a file. It is a service collector that will chain through any services tagged with 'mime_type_guesser'.
In core we only have ExtensionMimeTypeGuesser which looks at the file extension, but there are others that could be used including \Symfony\Component\Mime\FileBinaryMimeTypeGuesser and \Symfony\Component\Mime\FileinfoMimeTypeGuesser.
However, there are at least two places in core where we only want to look up the mime type based on the file extension:
- \responsive_image_get_mime_type()
- \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase::isApplicable()
In both these cases we create a fake file path with the extension and call \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guessMimeType($fakefile).
We should add a new method ExtensionMimeTypeGuesser::getMimeType() can just return the mapped mime type, and make it clear this is different from the logic of guessing the mime type.
Steps to reproduce
Proposed resolution
- Add a new method ExtensionMimeTypeGuesser::getMimeType() can just return the mapped mime type.
- Refactor existing code to use this method.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3476294
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3476294-create-a-method
changes, plain diff MR !9586
Comments
Comment #3
kim.pepperComment #4
mondrakeAs is, this MR would break BC if something is overriding the
file.mime_type.guesser.extensionservice class. (The Sophron guesser module does that, https://git.drupalcode.org/project/sophron/-/blob/2.1.x/sophron_guesser/...)I think we should use a deprecation helper to introduce usage of the new method
::getMimeType()based on its existence in the implementation class.Comment #5
mondrakeComment #6
kim.pepperHmm. I didn't realise how this was being used.
I'm not sure about that approach.
It might be better to add a new interface and service and switch that out with a BC layer.
Comment #7
mondrakeBlast from the past, #2311679: Separate MIME type mapping from ExtensionMimeTypeGuesser.
That one getting stuck led me develop the Sophron module.
Comment #8
kim.pepperAh ok. Let's close this as a duplicate of #2311679: Separate MIME type mapping from ExtensionMimeTypeGuesser