Skip to content

Commit

Permalink
Allow mimes to be fully overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 25, 2023
1 parent d2b4003 commit 87c160c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
MIX_ENV: test
strategy:
Expand Down
30 changes: 15 additions & 15 deletions lib/mime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ defmodule MIME do
"application/vnd.api+json" => ["json-api"]
}
After adding the configuration, MIME needs to be recompiled.
If you are using mix, it can be done with:
Note that defining a new type will completely override all
previous extensions. You can use `MIME.extensions/1` to get
the existing extension to add whenever defining.
After adding the configuration, MIME needs to be recompiled
if you are using an Elixir version earlier than v1.15. In such
cases, it can be done with:
$ mix deps.clean mime --build
Expand Down Expand Up @@ -121,25 +126,20 @@ defmodule MIME do
end
end

exts =
Map.merge(to_exts.(types), %{
all_types = Map.merge(types, custom_types)

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

for {ext, [_, _ | _] = mimes} <- exts do
for {ext, [_, _ | _] = mimes} <- all_exts do
raise "conflicting MIMEs for extension .#{ext}, please override: #{inspect(mimes)}"
end

all_exts = Map.merge(exts, to_exts.(custom_types))

all_types =
Map.merge(types, custom_types, fn _, default_exts, custom_exts ->
Enum.uniq(custom_exts ++ default_exts)
end)

@doc """
Returns the custom types compiled into the MIME module.
"""
Expand Down Expand Up @@ -192,8 +192,8 @@ defmodule MIME do
## Examples
iex> MIME.type("txt")
"text/plain"
iex> MIME.type("html")
"text/html"
iex> MIME.type("foobarbaz")
#{inspect(@default_type)}
Expand All @@ -209,7 +209,7 @@ defmodule MIME do
## Examples
iex> MIME.has_type?("txt")
iex> MIME.has_type?("html")
true
iex> MIME.has_type?("foobarbaz")
Expand Down
21 changes: 4 additions & 17 deletions test/mime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,16 @@ defmodule MIMETest do
end)
end

test "add custom extensions" do
test "overrides extensions" do
Application.put_env(:mime, :types, %{"text/plain" => ["env"]})

[File.cwd!(), "lib", "mime.ex"]
|> Path.join()
|> Code.compile_file()

assert MIME.extensions("text/plain") == ["env", "txt", "text"]
assert MIME.extensions("text/plain") == ["env"]
assert MIME.type("env") == "text/plain"
assert MIME.type("txt") == "text/plain"
assert MIME.type("text") == "text/plain"
end

test "add custom extensions avoiding duplicates" do
Application.put_env(:mime, :types, %{"text/plain" => ["env", "txt", "text"]})

[File.cwd!(), "lib", "mime.ex"]
|> Path.join()
|> Code.compile_file()

assert MIME.extensions("text/plain") == ["env", "txt", "text"]
assert MIME.type("env") == "text/plain"
assert MIME.type("txt") == "text/plain"
assert MIME.type("text") == "text/plain"
assert MIME.type("txt") == "application/octet-stream"
assert MIME.type("text") == "application/octet-stream"
end
end

0 comments on commit 87c160c

Please sign in to comment.