Plug: use unified API, handle mimes without extension

This commit is contained in:
Jordan Bracco 2020-06-16 14:50:04 +02:00
parent 65436c1c02
commit 2bc9f9470b
2 changed files with 33 additions and 21 deletions

View file

@ -35,16 +35,14 @@ if Code.ensure_loaded?(Plug) do
@impl Plug
def init(opts) do
mod =
cond do
Keyword.has_key?(opts, :pool) -> {Majic.Pool, Keyword.get(opts, :pool)}
Keyword.has_key?(opts, :server) -> {Majic.Server, Keyword.get(opts, :server)}
Keyword.has_key?(opts, :once) -> {Majic.Once, nil}
Keyword.has_key?(opts, :pool) -> true
Keyword.has_key?(opts, :server) -> true
Keyword.has_key?(opts, :once) -> true
true -> raise(Majic.PlugError, "No server/pool/once option defined")
end
opts
|> Keyword.put(:__module__, mod)
|> Keyword.put_new(:fix_extension, true)
|> Keyword.put_new(:append_extension, false)
end
@ -54,16 +52,22 @@ if Code.ensure_loaded?(Plug) do
%{conn | params: collect_uploads(params, opts)}
end
def call(conn, _) do
conn
end
defp collect_uploads(params, opts) do
Enum.reduce(params, Map.new(), fn value, acc -> collect_upload(value, acc, opts) end)
end
defp collect_upload({k, %{__struct__: Plug.Upload, path: path} = upload}, acc, opts) do
case perform(Keyword.get(opts, :__module__), path) do
case Majic.perform(path, opts) do
{:ok, magic} ->
IO.puts("Fixed upload -- #{inspect {upload,magic,opts}}")
Map.put(acc, k, fix_upload(upload, magic, opts))
{:error, error} ->
IO.puts("UPLOAD GOT BADARG")
raise(Majic.PlugError, "Failed to gen_magic: #{inspect(error)}")
end
end
@ -76,14 +80,6 @@ if Code.ensure_loaded?(Plug) do
Map.put(acc, k, v)
end
defp perform({mod = Majic.Once, _}, path) do
mod.perform(path)
end
defp perform({mod, name}, path) do
mod.perform(name, path)
end
defp fix_upload(upload, magic, opts) do
%{upload | content_type: magic.mime_type}
|> fix_extension(Keyword.get(opts, :fix_extension), opts)
@ -99,10 +95,6 @@ if Code.ensure_loaded?(Plug) do
upload
end
defp rewrite_extension(upload, _old, [], _opts) do
upload
end
defp rewrite_extension(upload, old, [ext | _] = exts, opts) do
if old in exts do
upload
@ -122,10 +114,27 @@ if Code.ensure_loaded?(Plug) do
end
end
# No extension for type.
defp rewrite_extension(upload, old, [], opts) do
%{upload | filename: rewrite_or_append_extension(Path.basename(upload.filename, old), old, nil, Keyword.get(opts, :append_extension))}
end
# Append, no extension for type: keep old extension
defp rewrite_or_append_extension(basename, "." <> old, nil, true) do
basename <> "." <> old
end
# No extension for type: only keep basename
defp rewrite_or_append_extension(basename, _, nil, _) do
basename
end
# Append
defp rewrite_or_append_extension(basename, "." <> old, ext, true) do
Enum.join([basename, old, ext], ".")
end
# Rewrite
defp rewrite_or_append_extension(basename, _, ext, _) do
basename <> "." <> ext
end

View file

@ -67,6 +67,9 @@ defmodule Majic.PlugTest do
get_in(conn.params, ["form", "makefile"]).content_type
assert get_in(conn.params, ["form", "makefile"]).content_type == "text/x-makefile"
assert get_in(conn.params, ["form", "makefile"]).filename == "mymakefile"
assert get_in(conn_no_ext.params, ["form", "makefile"]).filename == "mymakefile.txt"
assert get_in(conn_append_ext.params, ["form", "makefile"]).filename == "mymakefile.txt"
refute get_in(conn.body_params, ["form", "make", "file"]).content_type ==
get_in(conn.params, ["form", "make", "file"]).content_type