-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How far should Infer support file extensions ? #24
Comments
Since the idea is to leave infer and mime-db as separate crates anyway, I think we could have // infer::get and mime_db::get return Type
// Type implements Default with the the default mime type application/octet-stream and extension ""
let kind = infer::get(&buf).or_else(|| mime_db::get(&extension)).unwrap_or_default(); We would remove file extensions limited support and put something like this in all examples so that users could just use it for their crates. |
Looking more into it they don't require the network to build, since the build script is disabled. Maybe it's an odd way of doing it, but they have to change that line to make it run. Else the vendored extensions.rs and types.rs are used |
I made an initial PR to improve |
Tank you for your time and comments here!
I would let
Yes, once we clarified further our intentions with
I fully agree. Also maybe
It sounds great. Then we would have a
Let's see if the maintainer is opened for changes and hopefully move forward on this issue with him. Otherwise we could progress on our own (it would be sad, but this would be a classic open source story). |
So, now that mime-db v1 has been out for a month and no more progress has been made on this, I think we should give another look at the current situation. Now something like this is possible: let buf = // some file header
let ext = // some file extension
let mime_type = infer::get(&buf).map_or_else(
|| mime_db::lookup(ext).unwrap_or("application/octet-stream"),
|t| t.mime_type(),
); // this returns a &'static str
let extensions = mime_db::extensions(mime_type); // this returns an Option<impl Iterator<Item = &'static str>> It's not perfect, for example At least now we have What do you think @ririsoft? |
Hi @paolobarbolini, The situation is clearly improving thanks to your commits. My understanding is that you are favoring mime-db instead of mime_guess. This is totally fine for me, since mime_guess authors do not show much interest in the discussion. I agree that mime-db released a v1.0 a little bit early. A v2 will be needed if we want to change the I believe that Last I believe that we should offer users a single API with combination of For instance, taking your example above, I would personnally like to have something like: let buf = // some file header
let ext = // some file extension
let mime_type = infer::get_or_ext(&buf, ext); // this returns a &'static str and default to "application/octet-stream" if no match found.
let extensions = infer::extensions(mime_type); // this returns a struct that implements Iterator<Item = &'static str> |
mime_guess is in passive maintenance mode. It's a tangent of a tangent of a side-project I haven't touched in years. |
Thanks a lot for the clarification, it helps us move forward. Cheers. |
I wouldn't mind working with mime_guess instead, on the bright side it would have the advantage of not having both mime-db and mime_guess on most web projects (because of crates like reqwest and warp), but first we would have to change the
The more I think about this API that I start to think that having a third crate just for "merging" infer's and mime-db's functionality together wouldn't be too bad. But I haven't really made progress on this, so I'll take some more time to decide. In the meantime I'm going to work on a PR to fix the |
This issue is following discussion started in #21 code review.
Infer has limited support for file extensions. Other crates address file extension conversion from/to mime type, but I feel frustrated with all of them. Let's clarify wether my concerns could/should be addressed by
Infer
or in another crate.User story
I am building web applications which serves some files on the file system or proxied from other network locations. I want to set the
Content-Type
header which drives the web browser behavior in some contexts.I am also using the mime-type to build URL's with file extensions. I am using
mime_guess
for this task.Video and image mime types can be detected by
Infer
but many text types cannot: Javascript, CSS in particular. My algorithm is to detect file content withInfer
first, fallback tomime_guess
when I know the file extension second, and default to "application/octet-stream" when no match found.Concerns
Infer
uses its own hard coded data source.mime-db
andmime_guess
share the same data source from jshttp/mime-db, but there is no versioning of the DB: their might be some different behaviors across compilations. Also for a given mime typeInfer
results may be different frommime_guess
results. The official source of media types is IANAInfer
andmime_db
works with&'static str
whilemime_guess
works withMIME
from themime
crate as well as a customMimeGuess
type. UsingInfer
withmime_guess
is thus not much convenient: you need boilerplate to convert MIME to &str and back.Infer
,mime_guess
ormime_db
support type aliases. For instancetext/javascript
is a type alias ofapplication/javascript
:Infer
results tois_mime_supported
should betrue
in both cases.Infer
does not support extension aliases, whilemime_db
andmime_guess
do.Infer
allows to have custom types, and even overloading the default ones (since match custom matchers before default matchers #13), whilemime-db
andmime_guess
does not.Infer
allows forno_std
,no_alloc
since Add no_std, no_alloc support #18, whilemime-db
andmime_guess
does not.Infer
is maintained, so ismime-db
, butmime_guess
does not seem to be maintained anymore.mime_db
has a build script which has network dependency, whileInfer
andmime_guess
can be built offline.All this make it difficult for
Infer
to work hand in hand with eithermime-db
ormime_guess
.Options
mime_guess
but the maintainer does not answer to pull requests. I am abandoning this option for now.mime-db
. I will eventually study this option once we clarified on 1. and 3.Infer
, which is what I want to discuss here.Infer
andmime-db
or be a complete rewrite of all. I will consider this if 1, 2 or 3 fail.Option 2 - Improving
mime-db
We could improve
mime-db
at a point whereInfer
would remove its current limited support for file extensions and advert to usemime-db
instead. We could adjust both crates API so that they work hand-in-hand.Option 3 - Merging
mime_guess
andmime_db
intoInfer
.As per my user story above having a single crate to guess a mime type from either content or file extension makes sens. Such ideal crate would have the following features:
no_alloc
,no_std
.@abonander, @viz-rs you are welcome to join the discussion!
The text was updated successfully, but these errors were encountered: