mbeijen / File-MimeInfo

Perl module for determining file types using the freedesktop.org shared mime-info database
https://metacpan.org/module/File::MimeInfo
20 stars 14 forks source link

WL: Merge in `mimetype_isa` from Role::MimeInfo? #33

Open doriantaylor opened 6 years ago

doriantaylor commented 6 years ago

Hello,

Some time ago I wrote a Moo(se) Role that added some recursive behaviour to mimetype_isa. This was a very quick-and-dirty solution at the time, in order to obviate cutting and pasting the same couple dozen lines of code. Of course, not everything is suitable to be written in Moo(se).

As such I am wondering if you're interested in accepting a patch. I decided on this overture rather than going and diving straight into a pull request because I want some input beforehand:

The behaviour of my mimetype_isa is such that it returns true if the $type and $parent are the same, and will likewise return true if either $type or $parent is an alias or deprecated type identifier, as well as if $parent is not an immediate ancestor of $type. These cases (since last I checked) all return false in File::MimeInfo.

Anyway, since this behaviour is significantly different, I'm wondering if it would make sense to patch in a mimetype_isa_deep or add a flag to mimetype_isa to switch on this behaviour.

That is—of course—if you're interested in the offer.

mbeijen commented 6 years ago

In my opinion a third parameter to mimetype_isa would be a nice option.

Can you give a few examples of actual parameters and their result to this function?

We then could also use those for the documentation.

doriantaylor commented 6 years ago

The proposal is to change the behaviour of mimetype_isa to try harder to answer whether type A isa type B, so the third option would just be a flag to turn that on:

# recursion
mimetype_isa('application/xhtml+xml', 'text/plain'); # false
mimetype_isa('application/xhtml+xml', 'text/plain', 1); # true

# aliases
mimetype_isa('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/x-zip-compressed'); # false
mimetype_isa('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/x-zip-compressed', 1); # true

This is the default behaviour of the wrapper I wrote in Role::MimeInfo. I wrote that thing because I found myself writing the same routines over and over with File::MimeInfo to normalize content types (e.g. from the wild) and compare them recursively.

One use case in particular is testing a claimed MIME type (e.g. from a Content-Type header or derived from a file extension) against the contents of a file or HTTP upload to see if the claimed type is at least as specific as the detected one. I do this in my own work in a number of places.

It makes sense that this functionality be universal and not just in a Moo(se) role, but of course there are probably systems that depend on the current behaviour of mimetype_isa exactly as it is.