Skip to content

Commit 87c160c

Browse files
committed
Allow mimes to be fully overridden
1 parent d2b4003 commit 87c160c

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-latest
1212
env:
1313
MIX_ENV: test
1414
strategy:

lib/mime.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ defmodule MIME do
99
"application/vnd.api+json" => ["json-api"]
1010
}
1111
12-
After adding the configuration, MIME needs to be recompiled.
13-
If you are using mix, it can be done with:
12+
Note that defining a new type will completely override all
13+
previous extensions. You can use `MIME.extensions/1` to get
14+
the existing extension to add whenever defining.
15+
16+
After adding the configuration, MIME needs to be recompiled
17+
if you are using an Elixir version earlier than v1.15. In such
18+
cases, it can be done with:
1419
1520
$ mix deps.clean mime --build
1621
@@ -121,25 +126,20 @@ defmodule MIME do
121126
end
122127
end
123128

124-
exts =
125-
Map.merge(to_exts.(types), %{
129+
all_types = Map.merge(types, custom_types)
130+
131+
all_exts =
132+
Map.merge(to_exts.(all_types), %{
126133
"3g2" => ["video/3gpp2"],
127134
"3gp" => ["video/3gpp"],
128135
"js" => ["text/javascript"],
129136
"xml" => ["text/xml"]
130137
})
131138

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

136-
all_exts = Map.merge(exts, to_exts.(custom_types))
137-
138-
all_types =
139-
Map.merge(types, custom_types, fn _, default_exts, custom_exts ->
140-
Enum.uniq(custom_exts ++ default_exts)
141-
end)
142-
143143
@doc """
144144
Returns the custom types compiled into the MIME module.
145145
"""
@@ -192,8 +192,8 @@ defmodule MIME do
192192
193193
## Examples
194194
195-
iex> MIME.type("txt")
196-
"text/plain"
195+
iex> MIME.type("html")
196+
"text/html"
197197
198198
iex> MIME.type("foobarbaz")
199199
#{inspect(@default_type)}
@@ -209,7 +209,7 @@ defmodule MIME do
209209
210210
## Examples
211211
212-
iex> MIME.has_type?("txt")
212+
iex> MIME.has_type?("html")
213213
true
214214
215215
iex> MIME.has_type?("foobarbaz")

test/mime_test.exs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,16 @@ defmodule MIMETest do
5555
end)
5656
end
5757

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

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

65-
assert MIME.extensions("text/plain") == ["env", "txt", "text"]
65+
assert MIME.extensions("text/plain") == ["env"]
6666
assert MIME.type("env") == "text/plain"
67-
assert MIME.type("txt") == "text/plain"
68-
assert MIME.type("text") == "text/plain"
69-
end
70-
71-
test "add custom extensions avoiding duplicates" do
72-
Application.put_env(:mime, :types, %{"text/plain" => ["env", "txt", "text"]})
73-
74-
[File.cwd!(), "lib", "mime.ex"]
75-
|> Path.join()
76-
|> Code.compile_file()
77-
78-
assert MIME.extensions("text/plain") == ["env", "txt", "text"]
79-
assert MIME.type("env") == "text/plain"
80-
assert MIME.type("txt") == "text/plain"
81-
assert MIME.type("text") == "text/plain"
67+
assert MIME.type("txt") == "application/octet-stream"
68+
assert MIME.type("text") == "application/octet-stream"
8269
end
8370
end

0 commit comments

Comments
 (0)