elixir-plug / mime Goto Github PK
View Code? Open in Web Editor NEWA read-only and immutable MIME type module for Elixir
License: Other
A read-only and immutable MIME type module for Elixir
License: Other
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:
Content-Type
header matches a known image/*
typeBoth are possible now, but a little roundabout.
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/*"
)
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.
Hello!
If I understand correctly, there is no way to add new custom types at runtime, right?
==> 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.
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.
Hi, sorry if this is not applicable somehow, but I thought I'd reach out to verify something:
iex(1)> MIME.type("png")
"image/png"
iex(2)> MIME.type("heic")
"application/octet-stream"
This seems to be supported though: https://github.com/elixir-plug/mime/blob/master/priv/mime.types#L1642-L1643
System: Elixir 1.10.3, Erlang 23. MIME version is 1.3.1
.
Is this expected behavior? Thank you in advance!
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
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
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:
Lines 139 to 145 in 90852eb
As per title:
MIME.extensions("application/xml")
returns:
[]
expected:
["xml"]
The current behavior is in contrast with the documentation that states that it "Returns the extensions associated with a given MIME type." since, as per IANA documentation https://www.iana.org/assignments/media-types/application/xml this mime type should be associated with xml
. This is the issue PR#51 was trying to fix.
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?
Not sure how to contact you José, but it would be a nice time for a new MIME release.
Thank you!
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.
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
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
.
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.
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!
@christhekeele @allyraza, just let me know if yes or no. :)
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
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.
Line 101 in 9f3a908
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 😄
Would it be possible to do a new release with the mime patch I made back in august?
Running into a few mixed case mimes more often these days
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:
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.
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
I want to serve .ts files with Content-Type "video/mp2t"
from Plug.Static
, but using the config doesn't change the result from MIME.from_path
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.