jshttp / mime-types

The ultimate javascript content-type utility.
MIT License
1.33k stars 129 forks source link

Suggestion: do a valid content-type check in mime.lookup #19

Closed bitinn closed 9 years ago

bitinn commented 9 years ago

Not sure if you consider this an edge case, but I just tried to do this:

mime.lookup('application/x-shockwave-flash')
// return false, because x-shockwave-flash match nothing

I could be using mime.extension instead but it doesn't try to correct any input, and I was testing mime type obtained from parsing opengraph meta, ref: http://ogp.me/#structured, so I would like to correct them if possible, but I understand that may not be what mime.lookup designed to do.

dougwilson commented 9 years ago

It is only meant to look up the mime for a file extension. If you already have the mime, then there is nothing to even look up...

dougwilson commented 9 years ago

If you can provide more concrete examples of what, exactly, you are trying to do and expecting, then I can see what help I can provide, but otherwise all I can say is that mime.lookup is designed only to accept a file name or extension and look up the matching mime type; application/x-shockwave-flash is not distinguishable from a file named x-shockwave-flash in the folder application.

dougwilson commented 9 years ago

Please also let us know how we can expand the documentation at https://github.com/jshttp/mime-types#mimelookuppath to make it clear the only acceptable input to mime.lookup is a file path.

bitinn commented 9 years ago

my code is a big complicated, but it comes down to:

  1. parse an opengraph meta <meta property="og:image:type" content="image/png"> into image/png
  2. now check if image/png is valid by calling mime.lookup('image/png')
  3. if so use the returned result.

and I have switched to mime.extension, it works as long as meta is not messed up, I guess it's the closest to a validation check? or maybe I should use mime-db directly? should mime-types provide such an API for validation?

bitinn commented 9 years ago

As for the doc, it says All functions return false if input is invalid or not found, and my use-case is exactly that, to check if content-type is a known and valid content-type.

https://github.com/jshttp/mime-types#api

dougwilson commented 9 years ago

or maybe I should use mime-db directly?

ah, yes, with that explanation, that is exactly what I was going to suggest.

As for the doc, it says All functions return false if input is invalid or not found

Right, and mime.lookup is defined as Lookup the content-type associated with a file., and the file you gave it was application/x-shockwave-flash, which is the file called x-shockwave-flash in a folder called application, and since that file didn't have an extension, it returned false, for invalid input.

bitinn commented 9 years ago

thx, i see no point to keep this open then.