Plug: change in conn.body_params too
This commit is contained in:
parent
166353a620
commit
91a10cfa0c
3 changed files with 106 additions and 28 deletions
|
@ -42,40 +42,59 @@ if Code.ensure_loaded?(Plug) do
|
|||
true -> raise(Majic.PlugError, "No server/pool/once option defined")
|
||||
end
|
||||
|
||||
opts =
|
||||
opts
|
||||
|> Keyword.put_new(:fix_extension, true)
|
||||
|> Keyword.put_new(:append_extension, false)
|
||||
|
||||
opts
|
||||
|> Keyword.put_new(:fix_extension, true)
|
||||
|> Keyword.put_new(:append_extension, false)
|
||||
end
|
||||
|
||||
@impl Plug
|
||||
def call(%{params: params} = conn, opts) do
|
||||
%{conn | params: collect_uploads(params, opts)}
|
||||
def call(conn, opts) do
|
||||
collected = collect_uploads([], conn.body_params, [])
|
||||
|
||||
Enum.reduce(collected, conn, fn {param_path, upload}, conn ->
|
||||
param_path = Enum.reverse(param_path)
|
||||
|
||||
upload =
|
||||
case Majic.perform(upload.path, opts) do
|
||||
{:ok, magic} -> fix_upload(upload, magic, opts)
|
||||
{:error, error} -> raise(Majic.PlugError, "Failed to majic: #{inspect(error)}")
|
||||
end
|
||||
|
||||
conn =
|
||||
if get_in(conn.params, param_path) do
|
||||
%{conn | params: put_in(conn.params, param_path, upload)}
|
||||
end
|
||||
|
||||
if get_in(conn.body_params, param_path) do
|
||||
%{conn | body_params: put_in(conn.body_params, param_path, upload)}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
def call(conn, _) do
|
||||
conn
|
||||
defp collect_uploads(path, params, acc) do
|
||||
Enum.reduce(params, acc, fn value, acc -> collect_upload(path, value, acc) end)
|
||||
end
|
||||
|
||||
defp collect_uploads(params, opts) do
|
||||
Enum.reduce(params, Map.new(), fn value, acc -> collect_upload(value, acc, opts) end)
|
||||
# An upload!
|
||||
defp collect_upload(path, {k, %{__struct__: Plug.Upload} = upload}, acc) do
|
||||
[{[k | path], upload} | acc]
|
||||
end
|
||||
|
||||
defp collect_upload({k, %{__struct__: Plug.Upload, path: path} = upload}, acc, opts) do
|
||||
case Majic.perform(path, opts) do
|
||||
{:ok, magic} ->
|
||||
Map.put(acc, k, fix_upload(upload, magic, opts))
|
||||
|
||||
{:error, error} ->
|
||||
raise(Majic.PlugError, "Failed to gen_magic: #{inspect(error)}")
|
||||
end
|
||||
# Ignore structs.
|
||||
defp collect_upload(_path, {_, %{__struct__: _}}, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp collect_upload({k, v}, acc, opts) when is_map(v) do
|
||||
Map.put(acc, k, collect_uploads(v, opts))
|
||||
# Nested map.
|
||||
defp collect_upload(path, {k, v}, acc) when is_map(v) do
|
||||
collect_uploads([k | path], v, acc)
|
||||
end
|
||||
|
||||
defp collect_upload({k, v}, acc, _opts) do
|
||||
Map.put(acc, k, v)
|
||||
defp collect_upload(_path, _, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp fix_upload(upload, magic, opts) do
|
||||
|
@ -114,7 +133,16 @@ if Code.ensure_loaded?(Plug) do
|
|||
|
||||
# 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))}
|
||||
%{
|
||||
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
|
||||
|
|
52
test/builds-sr-ht.exs
Normal file
52
test/builds-sr-ht.exs
Normal file
|
@ -0,0 +1,52 @@
|
|||
name =
|
||||
case System.cmd("git", ~w(describe --all --long --dirty --broken --always)) do
|
||||
{name, 0} -> String.trim(name)
|
||||
_ -> "cannot-git-describe"
|
||||
end
|
||||
|
||||
repo = System.get_env("TEST_REPO") || "https://git.sr.ht/~href/gen_magic"
|
||||
|
||||
IO.puts("Using repository: #{repo}")
|
||||
|
||||
token = System.get_env("SR_HT_TOKEN")
|
||||
|
||||
unless token do
|
||||
IO.puts("""
|
||||
sr.ht token not defined (SR_HT_TOKEN)\n\n
|
||||
Get one at https://meta.sr.ht/oauth/personal-token\n
|
||||
Define one by setting the SR_HT_TOKEN environment variable
|
||||
""")
|
||||
else
|
||||
Application.ensure_all_started(:ssl)
|
||||
Application.ensure_all_started(:inets)
|
||||
|
||||
File.ls!(".builds")
|
||||
|> Enum.filter(fn file -> Path.extname(file) == ".yaml" end)
|
||||
|> Enum.each(fn file ->
|
||||
file = Path.join(".builds", file)
|
||||
build = Path.basename(file, ".yaml")
|
||||
|
||||
build =
|
||||
%{
|
||||
"manifest" => File.read!(file),
|
||||
"note" => "gen_magic/#{name} #{build}",
|
||||
"tags" => ["gen_magic"]
|
||||
}
|
||||
|> Jason.encode!()
|
||||
|
||||
case :httpc.request(
|
||||
:post,
|
||||
{'https://builds.sr.ht/api/jobs', [{'authorization', 'token ' ++ to_charlist(token)}],
|
||||
'application/json', build},
|
||||
[],
|
||||
[]
|
||||
) do
|
||||
{:ok, {{_http_v, 200, 'OK'}, _headers, body}} ->
|
||||
resp = Jason.decode!(body)
|
||||
IO.puts("#{resp["status"]} job #{resp["note"]}, id: #{resp["id"]}")
|
||||
|
||||
error ->
|
||||
IO.puts("Failed to enqueue job #{inspect(error)}")
|
||||
end
|
||||
end)
|
||||
end
|
|
@ -63,22 +63,20 @@ defmodule Majic.PlugTest do
|
|||
assert conn.state == :sent
|
||||
assert conn.status == 200
|
||||
|
||||
refute get_in(conn.body_params, ["form", "makefile"]).content_type ==
|
||||
get_in(conn.params, ["form", "makefile"]).content_type
|
||||
assert get_in(conn.body_params, ["form", "makefile"]) ==
|
||||
get_in(conn.params, ["form", "makefile"])
|
||||
|
||||
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
|
||||
assert get_in(conn.body_params, ["form", "make", "file"]) ==
|
||||
get_in(conn.params, ["form", "make", "file"])
|
||||
|
||||
assert get_in(conn.params, ["form", "make", "file"]).content_type == "text/x-makefile"
|
||||
|
||||
refute get_in(conn.body_params, ["cat"]).content_type ==
|
||||
get_in(conn.params, ["cat"]).content_type
|
||||
|
||||
assert get_in(conn.body_params, ["cat"]) == get_in(conn.params, ["cat"])
|
||||
assert get_in(conn.params, ["cat"]).content_type == "image/webp"
|
||||
assert get_in(conn.params, ["cat"]).filename == "cute-cat.webp"
|
||||
assert get_in(conn_no_ext.params, ["cat"]).filename == "cute-cat.jpg"
|
||||
|
|
Loading…
Reference in a new issue