diff --git a/lib/mime/application.ex b/lib/mime/application.ex index f41046d..f4c4e05 100644 --- a/lib/mime/application.ex +++ b/lib/mime/application.ex @@ -96,13 +96,28 @@ defmodule MIME.Application do iex> MIME.extensions("application/json") ["json"] + iex> MIME.extensions("application/vnd.custom+xml") + ["xml"] + iex> MIME.extensions("foo/bar") [] """ @spec extensions(String.t()) :: [String.t()] def extensions(type) do - mime_to_ext(downcase(type, "")) || [] + mime = + type + |> strip_params() + |> downcase("") + + mime_to_ext(mime) || suffix(mime) || [] + end + + defp suffix(type) do + case String.split(type, "+") do + [type_subtype_without_suffix, suffix] -> [suffix] + _ -> nil + end end @default_type "application/octet-stream" @@ -161,6 +176,10 @@ defmodule MIME.Application do end end + defp strip_params(string) do + string |> :binary.split(";") |> hd() + end + defp downcase(<>, acc) when h in ?A..?Z, do: downcase(t, <>) diff --git a/test/mime_test.exs b/test/mime_test.exs index bc8d03a..354fb2f 100644 --- a/test/mime_test.exs +++ b/test/mime_test.exs @@ -10,10 +10,15 @@ defmodule MIMETest do end test "extensions/1" do - assert "json" in extensions("application/json") + assert extensions("application/json") == ["json"] assert extensions("application/vnd.api+json") == ["json-api"] assert extensions("audio/amr") == ["amr"] assert extensions("IMAGE/PNG") == ["png"] + + assert extensions("application/json; charset=utf-8") == ["json"] + + assert extensions("application/vnd.custom+xml") == ["xml"] + assert extensions("application/vnd.custom+xml+xml") == [] end test "type/1" do