Submitting a process plugin to wrap Drupal's file.mime_type.guesser service and turn filenames into mimetypes.

I've seen #2877455: Processor to generate file extension of a remote URL and don't believe this functionality exists elsewhere. This will need tests added, but I'm getting an early version up here as a patch for community feedback.

Plugin is almost simple enough that it might be done using callback but I don't think that's quite possible?

We're using this in Islandora migrations to identify file mimetypes as part of an import process, then derive Islandora models, media type etc from the mimetype. (Will link examples later, this is step 1.)

Comments

xurizaemon created an issue. See original summary.

xurizaemon’s picture

Issue summary: View changes
Status: Active » Needs work
StatusFileSize
new1.16 KB
xurizaemon’s picture

Title: Process plugin: mimetype » Process plugin: file_mimetype
xurizaemon’s picture

StatusFileSize
new1.16 KB
yogeshmpawar’s picture

Status: Needs work » Needs review

Adding this issue to Needs Review queue.

yogeshmpawar’s picture

Assigned: xurizaemon » Unassigned
srjosh’s picture

Elegant and much needed. Looks good to me!

xurizaemon’s picture

Thanks! If this needs tests added, I'm happy to commit to that (or for someone else to contribute them).

If OTOH you think it can land as-is, I'm OK with that :)

heddn’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
+++ b/src/Plugin/migrate/process/FileMimeType.php
@@ -0,0 +1,37 @@
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    return \Drupal::service('file.mime_type.guesser')->guess($value);

I think this can be just the callback process plugin, no? I guess not because there's no static method. A simple unit test would be good.

xurizaemon’s picture

It *almost* could be the callback process plug-in! But I don't think it can be.

Should be able to add tests this week, thanks!

matroskeen’s picture

There is a feature request to add a general plugin called "service". Please take a look into #3218356: Process plugin: service.
The issue summary contains the example of using the file.mime_type.guesser requested here 😃

I think we can close this one as a duplicate.

xurizaemon’s picture

Status: Needs work » Closed (duplicate)

Nice work! That'll teach me to not get around to adding tests :) Subscribing, looks great.