The Needs Review Queue Bot → tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
kim.pepper → made their first commit to this issue’s fork.
- Merge request !9690[#2311679] Separate MIME type mapping from ExtensionMimeTypeGuesser → (Open) created by kim.pepper
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
kim.pepper → changed the visibility of the branch 11.x to hidden.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Did a fair amount of cleanup on this issue.
ExtensionMimeTypeGuesser
is now using theMimeTypeMapper
- Removed the
getMapping()
method which was temporary anyway and refactored tests to not call it - Split up tests and moved them to the
Drupal\KernelTests\Core\File\MimeType
namespace to make the namespace underlib/
- Handle NULL returned from
ExtensionMimeTypeGuesser::guessMimeType()
- Updated the changes to the default map that went in since this was last worked on a year ago
- Added typehints
- Added more dependency injection
- Fixed linting errors
- Updated the deprecation message to refer to 11.1.0 and 12.0.0
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Also created a follow-up 📌 Generate file extension mime type map with fileeye/mimemap update utility Active
- 🇺🇸United States mfb San Francisco
Why not allow contrib modules to call getMapping() so they can arbitrarily alter the mapping?
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Can't already alter the mapping with the alter hook?
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Also different mapping implementations could use different data structures, so what gets returned from getMapping() could be different.
- 🇺🇸United States mfb San Francisco
Well I'm looking at how to still allow File MIME module → work without using the deprecated hook_file_mimetype_mapping_alter(). It looks like the new alter hook doesn't allow contrib module to get the mapping so it can make various changes, and then apply it via setMapping().
And different data structures? Well that would make it even more difficult for a contrib module to alter the mapping, lol sigh. I confess to not actually following this issue.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
function hook_mimetype_alter(?MimeTypeMapperInterface $mime_type_mapper = NULL)
gives you the ability to add/remove/update mappings by using the API interface rather than the raw data structure, which is better IMO. - 🇺🇸United States mfb San Francisco
Yes I see know it's possible to call addMapping(). But I'd have to iterate through and make hundreds of method calls (if someone configures File MIME module to parse their /etc/mime.types file), with an array_search() in each one, so it just seems slower, that's all. And I'd have to double check there is no behavior change from how it worked previously.
- 🇺🇸United States mfb San Francisco
Realized I was being lazy and I should benchmark it. So, I found that implementing the new hook in File MIME module (configured to add all the MIME types from the /etc/mime.types file) adds about 10 milliseconds of execution time. This is probably fine for those cases where the hook is invoked during file uploads - such requests are going to be extremely slow regardless - but not ideal for other pages where the hook might be invoked (e.g. managing display of an entity with a file field). Admittedly, File MIME module doesn't have very many installs (and more installs of the Drupal 7 branch than the current branch).
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
#128 Thinking about this further, we should probably remove
setMapping()
to hide the underlying data structure. Projects like https://www.drupal.org/project/sophron → that use the https://github.com/FileEye/MimeMap library have a different data structure.#129 hmm. Shouldn't that hook only get fired on container build? It should be cached after that.
- 🇺🇸United States mfb San Francisco
@kim.pepper no, there is nothing caching the map, whether in the container or otherwise. Both the old and new alter hooks run whenever the map is used.
- 🇺🇸United States mfb San Francisco
Projects like https://www.drupal.org/project/sophron → that use the https://github.com/FileEye/MimeMap library have a different data structure.
I looked at Sophron module, and it has a totally different mechanism for allowing other modules to alter its mapping - it dispatches an event. I guess you are saying that a future version of Sophron module could start invoking this alter hook?
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
#131 I don't think that's right.
file.mime_type.mapper: class: Drupal\Core\File\MimeType\MimeTypeMapper calls: - [alterMapping, ['@module_handler']]
The hook gets fired when this service is created.
- 🇺🇸United States mfb San Francisco
alterMapping is called whenever the
file.mime_type.mapper
service is created (for example when ExtensionMimeTypeGuesser is constructed). So the alter hook fires on any request that needs to create that service.E.g. if a page renders an entity that displays an audio file field, the service is needed and the alter hook fires. But once that entity is cached in the render cache, it will no longer render, and therefore the service won't be created, so the alter hook won't fire. Maybe that is the caching you were referring to, I'm not sure? There are also admin pages where the service is created, and the alter hook fires, without any sort of cache being involved (e.g. managing display of an entity with a file field).
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
#134 Yeah I guess I was talking about the container cache. I misunderstood what you meant by 'Both the old and new alter hooks run whenever the map is used.' thinking you meant when we call guessMimeType() but it's when the service is created.
I did some more work removing setMapping(). I don't think that's going to cause too many issues.
- 🇺🇸United States mfb San Francisco
I was timing the alter hook on requests where the service is created (e.g. because guessMimeType() was called at least once). Since it's when the service is created, the 10ms slowdown only happens at most once per request.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
I've done a fair bit of rework to replace the new hook with tagged services. I think triggering hooks during container build is a bit of an antipattern, and this approach uses a more symfony native approach.
- 🇺🇸United States mfb San Francisco
@kim.pepper I don't understand why you say "triggering hooks during container build" - the hook was triggered when the service was created, i.e. when a request needs to use it for the first time, not when the container is built.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
the hook was triggered when the service was created
Fair enough.
As for the test fail, I have no idea why
\Drupal\Tests\block\Functional\BlockCacheTest::testCachePerPage()
is failing. It's passing locally for me. Could it be a random fail? - 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Found the source of the test fails was a deprecation warning on the legacy
hook_file_mimetype_mapping_alter()
infile_test.module
. We already test this infile_deprecated_test.module
so I removed it.Ready for reviews again!
- 🇮🇹Italy mondrake 🇮🇹
mondrake → changed the visibility of the branch 2311679-separate-mime-type to hidden.
- 🇮🇹Italy mondrake 🇮🇹
I did a review. I am definitely biased by having developed Sophron, feel free to push back.
Also, adding a related issue that should be committed before this IMHO
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Thanks for the detailed review @mondrake. Much appreciated. I've responded to all your comments and made changes where I agree. I resolved a few where I did not agree, but feel free to re-open those threads if you disagree.
- Merge request !9760Draft: [#2311679] POC for FileEye MIME map implementation → (Open) created by kim.pepper
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
I created a POC MR with a FileEye MIME map implementation to check if our interfaces and factory approach works with 3rd party libs.
The Needs Review Queue Bot → tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.
The Needs Review Queue Bot → tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.
- 🇮🇹Italy mondrake 🇮🇹
A bunch of nitpickeries from me. I think this is almost ready, cannot RTBC it though.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Addressed all feedback. Thanks again.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Added tests and answered question.
godotislate → changed the visibility of the branch 2311679-separate-mime-type-11.x to hidden.
godotislate → changed the visibility of the branch 2311679-separate-mime-type-11.x to active.
godotislate → changed the visibility of the branch 2311679-separate-mime-type-fileeye to hidden.
- 🇮🇹Italy mondrake 🇮🇹
🐛 ExtensionMimeTypeGuesser::guessMimeType return less accurate MIME type when file extensions have multiple parts Active went in, and this needs a reroll now.
The Needs Review Queue Bot → tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
I've merged 11.x into this MR. Looks like the Hook Attributes work that got merged is triggering some deprecations that will need looking at.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
This was just a rebase on 11.x so putting back to RTBC
- Status changed to RTBC
about 2 months ago 2:16am 11 December 2024 - 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Spoke to @xjm at DrupalCon Singapore contribution day, and she recommended tagging as Needs framework manager review.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Discussed with @larowlan and this should be a task.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
I squashed all the commits in the MR to make rebasing on 11.x easier.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Looks like we are triggering deprecation errors in
Drupal\Core\File\MimeType\DefaultMimeTypeMap::getMapping()
andDrupal\Core\File\MimeType\DefaultMimeTypeMap::setMapping()
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Removing 'Needs framework manager review' tag as @larowlan has reviewed.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Thanks for the review. Addressed feedback.
Apologies, seeing a couple new things with fresh eyes that I missed last pass:
- One more use of in_array and array_search can be reduced to array_search once
- Discovered in 📌 Memory leak in DrupalKernel when installing modules Active that 🌱 Use \Drupal consistently in tests Needs work should be done, so can get ahead of it here with using \Drupal::service() instead of $this->container->get in test classes
- 🇮🇹Italy mondrake 🇮🇹
Just noticed the CR links redirect to this issue and not to a CR. A CR for this is actually missing.
- 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
Created a draft CR. Will look at changing the links in the morning unless someone else gets there first.
LGTM. Nice work, @kim.pepper. Thanks for sticking with this.
- 🇮🇹Italy mondrake 🇮🇹
Now Sophron is passing tests with this MR applied (deprecation errors are thrown but that is OK). https://github.com/mondrake/d8-unit/actions/runs/12844774697/job/3581814...
We need to keep the protected properties as they are and cannot rely on the new ones (the map and the file system) to be initialised when the methods are called, as the classes that today extend
ExtensionMimeTypeGuesser
overriding the constructor will not have done that. - 🇦🇺Australia kim.pepper 🏄♂️🇦🇺Sydney, Australia
I think we have resolved all threads except one question for @alexpott about changes to
MimeTypeMapFactory