Giter Club home page Giter Club logo

mime's People

Contributors

0urobor0s avatar al1raza avatar aseigo avatar bcardarella avatar binarytemple avatar chulkilee avatar clone1018 avatar eksperimental avatar fedeotaran avatar fertapric avatar gmile avatar goncalotomas avatar hauleth avatar icedragon200 avatar joninvski avatar josevalim avatar kotsius avatar marktran avatar michelson avatar milmazz avatar mullpointer avatar oldhammade avatar rogelio2k avatar tameribrahim avatar theoneric avatar tomciopp avatar whatyouhide avatar wojtekmach avatar xadhoom avatar xtian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mime's Issues

Simplify validating incoming MIME types

I'm trying to validate incoming MIME types are of a given document type. That is, I download what is theoretically an image, and I'd like to:

  1. Verify that its Content-Type header matches a known image/* type
  2. Verify that its file extension matches its MIME type

Both are possible now, but a little roundabout.

  1. For verifying a MIME type is a known image/* type, you can do something like:

    known_image_type? =
      with "image/" <> _ <- mime_type,
           extensions when extensions != [] <- MIME.extensions(mime_type) do
        true
      else
        false
      end
    

    I think it'd be really cool if MIME.extensions/1 supported partial MIME types (e.g., either "image/" or "image/*")

  2. Checking that the given file extension is known for a MIME type is even more roundabout. Something like:

    given_extension =
      image.filename
      |> Path.extname()
      |> String.replace(".", "")
      |> String.downcase()
    
    extension_matches_mime_type? = given_extension in MIME.extensions(image.content_type)
    

    This one strikes me as being worth a new top-level function... something like MIME.path_matches_type?(path, mime_type)

I'm happy to submit a PR if this is something you all are interested in. ☺️

modify types at runtime

Hello!
If I understand correctly, there is no way to add new custom types at runtime, right?

Compile Warning on Elixir 1.8.0-otp-21.2.2

==> mime
Compiling 2 files (.ex)
warning: this clause cannot match because a previous clause at line 2 always matches
  lib/mime.ex:2

Generated mime app

Looks like it's in the generated code.

Change in file extensions of text/plain

Hello there. I have a question for you.

I noticed that the extensions returned by MIME.extensions("text/plain") has changed between 1.6.0 and 2.0.0. In 1.6.0, the return value is ["txt", "asc", "text", "pm", "el", "c", "h", "cc", "hh", "cxx", "hxx", "f90", "conf", "log"]. In 2.0.0, it is ["txt"].

Is this intentional? I had trouble verifying what to expect from the IANA data.

audio/3gpp has no extensions causing MIME.has_type?/1 to always return false

It's causing a few issues at the moment, for one because it has no extensions MIME.has_type?/1 returns false:

iex(1)> MIME.has_type?("audio/3gpp")
false

I've quickly scanned the mime.types and it is listed without extensions: https://github.com/elixir-plug/mime/blob/master/priv/mime.types#L1512

I'm sure other types have this same issue as well, I'm not sure what should be added to the existing API to support cases like this where a type can exist, even without known extensions:

iex(1)> MIME.type?("audio/3gpp")
false
iex(2)> MIME.type_exists?("audio/3gpp")
true

Release 1.5.0 changed behaviour of MIME.extensions with nil

In 1.4.0:

iex> MIME.extensions(nil)                      
[]

In 1.5.0:

iex> MIME.extensions(nil) 
** (FunctionClauseError) no function clause matching in MIME.downcase/2    
    
    The following arguments were given to MIME.downcase/2:
    
        # 1
        nil
    
        # 2
        ""
    
    Attempted function clauses (showing 3 out of 3):
    
        defp downcase(<<h, t::binary()>>, acc) when is_integer(h) and (h >= 65 and h <= 90)
        defp downcase(<<h, t::binary()>>, acc)
        defp downcase(<<>>, acc)
    
    (mime 1.5.0) lib/mime.ex:2: MIME.downcase/2
    (mime 1.5.0) lib/mime.ex:2: MIME.extensions/1

Cannot add new type with same extension from the list on new version

Hello Team! Thank you for this amazing library. However, we encountered an error when upgrading. To provide some background, we need to support the audio/x-wav type from the https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Containers#wave_wav. According to the documentation, audio/x-wav should also be recognized as wav.

Our configuration looks like this:

config :mime, :types, %{
  "audio/x-wav" => ["wav"]
}

In the previous version, v2.0.3, it worked as expected. However, when we tried to upgrade to v2.0.4, we received the following error message:

conflicting MIMEs for extension .wav, please override: ["audio/wav", "audio/x-wav"]

Upon investigation, I noticed similar types that share the same extension in the list, such as 3g2 and 3gp. Prior to raising, these types are merged in the code section found here:

mime/lib/mime.ex

Lines 139 to 145 in 90852eb

all_exts =
Map.merge(to_exts.(all_types), %{
"3g2" => ["video/3gpp2"],
"3gp" => ["video/3gpp"],
"js" => ["text/javascript"],
"xml" => ["text/xml"]
})

Replace deprecated String.strip/1 with String.trim/1

On Elixir 1.5.x and 1.6-rc mime 1.1.0 issues a compiler warning

==> mime
Compiling 1 file (.ex)
warning: String.strip/1 is deprecated, use String.trim/1
  lib/mime.ex:28

Are you open to a PR or is backwards compatibility constrained until Elixir 2.0?

Publish new release

Not sure how to contact you José, but it would be a nice time for a new MIME release.

Thank you!

Atom and RSS MIMEs are missing

While RSS is not registered in IANA it is pretty common format used by many sources. On the other hand Atom is fully speced and registered in IANA registry and is also used by some sources as a feed format.

Add MIME type for .aac files

It is possible to support aac audio files by extending the mime types config.

config :mime, :types, %{
  "audio/aac" => ["aac"]
}

Should this type be integrated into the static mime types file?

@josevalim if this makes sense I will create a PR that adds the aac type

Support listing known MIME types

It would be useful to expose the built-in list of known MIME types, as the library is a good repository of them (within the Elixir ecosystem).

Could be as simple as MIME.list_types/0.

Support extensionless file

I added

config :mime, :types, %{
  "application/json" => ["apple-app-site-association"]
}

to my app config, but, from_path/1 do not return the proper type:

iex(1)> MIME.from_path("apple-app-site-association")
"application/octet-stream"
iex(2)> MIME.type("apple-app-site-association")     
"application/json"

It looks like it is because you default to the default type is the file has no extension.

Request for New Release

Hi there 👋🏼

With #73 you all helpfully fixed an issue we encountered. Would it be possible to create a new release that includes these changes?

Thank you!

Casing issues with some mimes

The fatest way to demonstrate the problem:

iex(2)> MIME.extensions("audio/amr")
[]
iex(3)> MIME.extensions("audio/AMR")
["amr"]

I believe MIME should at least normalize (lowercase) all it's compiled types, and possibly lowercase it's input to match, unless the latter creates a performance issue

Expectation

MIME.extensions/1 works with any casing thrown at it

Strange behavior when adding a new type/extension

Hi!

I have a question with the behavior when a new extension is added to a type.

For example, I want to add a new extension for a plain text file.

This type originally has the txt and text extensions added.

"text/plain" => ["txt", "text"],

I add the new extension to my project according to the documentation:

config :mime, :types, %{
  "text/plain" => ["env"]
}

The extensions now available for the text/plain type are as follows:

iex -S mix
Erlang/OTP 25 [erts-13.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> MIME.extensions("text/plain")
["env"]
iex(2)>

Which does not seem bad to me if what you are looking for is to overwrite all the extensions for that type.

But when looking at the type for the extensions separately, text/plain matches for all of them.

iex -S mix
Erlang/OTP 25 [erts-13.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> MIME.extensions("text/plain")
["env"]
iex(2)> MIME.type("txt")
"text/plain"
iex(3)> MIME.type("text")
"text/plain"
iex(4)> MIME.type("env")
"text/plain"

The type/1 method works both old and new values.
This seems a bit confusing to me.

I think both options make sense, but it should be consistent for all methods.

What do you think?

I'm happy to send a PR if you think that's right 😄

changing mime setting requires forced recompilation of a dep

Mime currently reads user's setting during compilation.

While this makes the interface convenient, it has a serious downside that when user changes their setting, the change won't take effect until a dep is manually recompiled. To make matters worse, the change is silently ignored, and so it might silently make it to production.

Admittedly, docs mention this issue, but force dep recompile isn't really simple in any automated workflow. For example, at my workplace we have a couple of different servers doing automated builds, and to speed up the work, they all keep the cached version of the last build, so changing the configuration won't make it neither to tests, nor to production. The only solutions for this I can think of:

  1. Add some trickery in the user project to always force build mime dep
  2. Add some more elaborate trickery to detect that mime configuration has been changed since the last time, and then force build mime dep
  3. Do manual refresh along the lines of "Nobody should merge until I manually clean cache on every server, push the change, and make sure that the change has been properly propagated".

None of this seems like a compelling option.

As an added downside, the current design makes it impossible to have separate configurations for separate parts of my system (e.g. the main site and an admin site). Both Plug and Phoenix implicitly rely on the global setting cached during the last build of the mime dep.

I'd like to see a more flexible solution here which at the very least properly propagates user's changes to the runtime without requiring manual recompilation. It would also be nice if globalness was dropped.

One way I can think of is to move generation to the user's code. Here's a quick prototype which relies on the existing code to support extensions/1 and type/1:

iex> defmodule ConfigurableMime do
  defmacro __using__(user_type_to_exts) do
    quote bind_quoted: [user_type_to_exts: user_type_to_exts] do
      for {type, exts} <- user_type_to_exts do
        def extensions(unquote(type)), do: unquote(exts)
      end

      def extensions(type), do: MIME.extensions(type)

      for {type, exts} <- user_type_to_exts,
          ext <- exts do
        def type(unquote(ext)), do: unquote(type)
      end

      def type(file_extension), do: MIME.type(file_extension)
    end
  end
end

iex> defmodule MyMime do
  use ConfigurableMime, %{"application/foo" => ["bar"]}
end

iex> MyMime.extensions("application/gzip")
["gz", "tgz"]

iex> MyMime.extensions("application/foo")
["bar"]

I'm not saying that this is necessarily the best way, but at least it solves both mentioned issues, while still keeping the compiled time guarantees, and without generating multiple copies of the default map in the user's code. If Plug was adapted to explicitly accept the callback module for custom mime configuration, it would be even straightforward to supply parameters at runtime by fetching them from external sources.

Unsupported default browser Accept values

Hello,
I started getting errors reported in Sentry for various Safari versions requesting image/* in Accept headers.
To my surprise, this value is known as a default for at least Firefox and Safari as shown on this page :

https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values

I'm unsure if this package should support this, as it cannot be used to directly map a type to a file extension.
But by default, Phoenix 1.5.0 errors out when an image is requested with only image/* in an Accept header, and according to my Sentry log, this is quite common. I could of course work around this by configuring MIME in my config.exs

I apologize if this is not the right project to open an issue on, but if that would be the case, where could this edge-case be handled ?
Have a nice day,
Lucas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.