Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F113813
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/majic/plug.ex b/lib/majic/plug.ex
index ad31441..64667e3 100644
--- a/lib/majic/plug.ex
+++ b/lib/majic/plug.ex
@@ -1,142 +1,140 @@
if Code.ensure_loaded?(Plug) do
defmodule Majic.PlugError do
defexception [:message]
end
defmodule Majic.Plug do
@moduledoc """
A `Plug` to automatically set the `content_type` of every `Plug.Upload`.
One of the required option of `pool`, `server` or `once` must be set.
Additional options:
* `fix_extension`, default true: rewrite the user provided `filename` with a valid extension for the detected content type
* `append_extension`, default false: append the valid extension to the previous filename, without removing the user provided extension
To use a gen_magic pool:
```
plug Majic.Plug, pool: MyApp.MajicPool
```
To use a single gen_magic server:
```
plug Majic.Plug, server: MyApp.MajicServer
```
To start a gen_magic process at each file (not recommended):
```
plug Majic.Plug, once: true
```
"""
@behaviour Plug
@impl Plug
def init(opts) do
cond do
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_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)}
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 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
defp collect_upload({k, v}, acc, opts) when is_map(v) do
Map.put(acc, k, collect_uploads(v, opts))
end
defp collect_upload({k, v}, acc, _opts) do
Map.put(acc, k, v)
end
defp fix_upload(upload, magic, opts) do
%{upload | content_type: magic.mime_type}
|> fix_extension(Keyword.get(opts, :fix_extension), opts)
end
defp fix_extension(upload, true, opts) do
old_ext = String.downcase(Path.extname(upload.filename))
extensions = MIME.extensions(upload.content_type)
rewrite_extension(upload, old_ext, extensions, opts)
end
defp fix_extension(upload, _, _) do
upload
end
defp rewrite_extension(upload, old, [ext | _] = exts, opts) do
if old in exts do
upload
else
basename = Path.basename(upload.filename, old)
%{
upload
| filename:
rewrite_or_append_extension(
basename,
old,
ext,
Keyword.get(opts, :append_extension)
)
}
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
end
end
diff --git a/test/builds-sr-ht.exs b/test/builds-sr-ht.exs
deleted file mode 100644
index 7f7c7f8..0000000
--- a/test/builds-sr-ht.exs
+++ /dev/null
@@ -1,52 +0,0 @@
-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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 2:58 PM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39927
Default Alt Text
(5 KB)
Attached To
Mode
R20 majic
Attached
Detach File
Event Timeline
Log In to Comment