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

Command icon 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:

Comments

kim.pepper created an issue. See original summary.

kim.pepper’s picture

Status: Active » Needs review
mondrake’s picture

As is, this MR would break BC if something is overriding the file.mime_type.guesser.extension service 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.

mondrake’s picture

Status: Needs review » Needs work
kim.pepper’s picture

Hmm. I didn't realise how this was being used.

I think we should use a deprecation helper to introduce usage of the new method ::getMimeType() based on its existence in the implementation class.

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.

mondrake’s picture

Blast from the past, #2311679: Separate MIME type mapping from ExtensionMimeTypeGuesser.

That one getting stuck led me develop the Sophron module.

kim.pepper’s picture

Status: Needs work » Closed (duplicate)