rails / marcel

Find the mime type of files, examining file, filename and declared type
Apache License 2.0
387 stars 67 forks source link

Get name and extension from Pathname #87

Closed olivier-thatch closed 9 months ago

olivier-thatch commented 1 year ago

I was surprised that given the following invocation:

Marcel::MimeType.for(Pathname.new("assets/foo.css"))

Marcel returned "application/octet-stream".

I was able to fix this by explicitly passing the file name with the name keyword argument:

Marcel::MimeType.for(Pathname.new("assets/foo.css"), name: "foo.css")

This correctly returns "text/css".

Since both the file name and its extension can be computed from Pathname, I think it makes sense to do that automatically when name and extension are not explicitly provided.

Note that while I think this behavior is preferable and less surprising for users (at least, it's what I expected), it could cause issues for existing users of the gem as they could get different results. As such, if you decide to accept this patch, you may want to release it as a new major version and/or provide a warning in release notes.

jeremy commented 9 months ago

Dig that bit of convenience, but these are indeed meant to be distinct arguments. Otherwise, a tempfile's extension would be treated the same as a filename explicitly provided by the user agent.

A new API could make sense, though!