forked from AkkomaGang/akkoma
Improve upload filter return values so we can identify when filters make no changes to the input
This commit is contained in:
parent
fa347b9c2f
commit
2165a24974
11 changed files with 32 additions and 22 deletions
|
@ -15,7 +15,11 @@ defmodule Pleroma.Upload.Filter do
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@callback filter(Pleroma.Upload.t()) :: :ok | {:ok, Pleroma.Upload.t()} | {:error, any()}
|
@callback filter(Pleroma.Upload.t()) ::
|
||||||
|
{:ok, :filtered}
|
||||||
|
| {:ok, :noop}
|
||||||
|
| {:ok, :filtered, Pleroma.Upload.t()}
|
||||||
|
| {:error, any()}
|
||||||
|
|
||||||
@spec filter([module()], Pleroma.Upload.t()) :: {:ok, Pleroma.Upload.t()} | {:error, any()}
|
@spec filter([module()], Pleroma.Upload.t()) :: {:ok, Pleroma.Upload.t()} | {:error, any()}
|
||||||
|
|
||||||
|
@ -25,10 +29,13 @@ def filter([], upload) do
|
||||||
|
|
||||||
def filter([filter | rest], upload) do
|
def filter([filter | rest], upload) do
|
||||||
case filter.filter(upload) do
|
case filter.filter(upload) do
|
||||||
:ok ->
|
{:ok, :filtered} ->
|
||||||
filter(rest, upload)
|
filter(rest, upload)
|
||||||
|
|
||||||
{:ok, upload} ->
|
{:ok, :filtered, upload} ->
|
||||||
|
filter(rest, upload)
|
||||||
|
|
||||||
|
{:ok, :noop} ->
|
||||||
filter(rest, upload)
|
filter(rest, upload)
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
|
|
|
@ -16,9 +16,11 @@ defmodule Pleroma.Upload.Filter.AnonymizeFilename do
|
||||||
def filter(%Upload{name: name} = upload) do
|
def filter(%Upload{name: name} = upload) do
|
||||||
extension = List.last(String.split(name, "."))
|
extension = List.last(String.split(name, "."))
|
||||||
name = predefined_name(extension) || random(extension)
|
name = predefined_name(extension) || random(extension)
|
||||||
{:ok, %Upload{upload | name: name}}
|
{:ok, :filtered, %Upload{upload | name: name}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filter(_), do: {:ok, :noop}
|
||||||
|
|
||||||
@spec predefined_name(String.t()) :: String.t() | nil
|
@spec predefined_name(String.t()) :: String.t() | nil
|
||||||
defp predefined_name(extension) do
|
defp predefined_name(extension) do
|
||||||
with name when not is_nil(name) <- Config.get([__MODULE__, :text]),
|
with name when not is_nil(name) <- Config.get([__MODULE__, :text]),
|
||||||
|
|
|
@ -17,8 +17,8 @@ def filter(%Upload{name: name, tempfile: tempfile} = upload) do
|
||||||
|> Base.encode16(case: :lower)
|
|> Base.encode16(case: :lower)
|
||||||
|
|
||||||
filename = shasum <> "." <> extension
|
filename = shasum <> "." <> extension
|
||||||
{:ok, %Upload{upload | id: shasum, path: filename}}
|
{:ok, :filtered, %Upload{upload | id: shasum, path: filename}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter(_), do: :ok
|
def filter(_), do: {:ok, :noop}
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,22 +9,22 @@ defmodule Pleroma.Upload.Filter.Exiftool do
|
||||||
"""
|
"""
|
||||||
@behaviour Pleroma.Upload.Filter
|
@behaviour Pleroma.Upload.Filter
|
||||||
|
|
||||||
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
|
@spec filter(Pleroma.Upload.t()) :: {:ok, any()} | {:error, String.t()}
|
||||||
def filter(%Pleroma.Upload{name: file, tempfile: path, content_type: "image" <> _}) do
|
def filter(%Pleroma.Upload{name: file, tempfile: path, content_type: "image" <> _}) do
|
||||||
# webp is not compatible with exiftool at this time
|
# webp is not compatible with exiftool at this time
|
||||||
if Regex.match?(~r/\.(webp)$/i, file) do
|
if Regex.match?(~r/\.(webp)$/i, file) do
|
||||||
:ok
|
{:ok, :noop}
|
||||||
else
|
else
|
||||||
strip_exif(path)
|
strip_exif(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter(_), do: :ok
|
def filter(_), do: {:ok, :noop}
|
||||||
|
|
||||||
defp strip_exif(path) do
|
defp strip_exif(path) do
|
||||||
try do
|
try do
|
||||||
case System.cmd("exiftool", ["-overwrite_original", "-gps:all=", path], parallelism: true) do
|
case System.cmd("exiftool", ["-overwrite_original", "-gps:all=", path], parallelism: true) do
|
||||||
{_response, 0} -> :ok
|
{_response, 0} -> {:ok, :filtered}
|
||||||
{error, 1} -> {:error, error}
|
{error, 1} -> {:error, error}
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
|
|
|
@ -38,16 +38,16 @@ defmodule Pleroma.Upload.Filter.Mogrifun do
|
||||||
[{"fill", "yellow"}, {"tint", "40"}]
|
[{"fill", "yellow"}, {"tint", "40"}]
|
||||||
]
|
]
|
||||||
|
|
||||||
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
|
@spec filter(Pleroma.Upload.t()) :: {:ok, atom()} | {:error, String.t()}
|
||||||
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
|
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
|
||||||
try do
|
try do
|
||||||
Filter.Mogrify.do_filter(file, [Enum.random(@filters)])
|
Filter.Mogrify.do_filter(file, [Enum.random(@filters)])
|
||||||
:ok
|
{:ok, :filtered}
|
||||||
rescue
|
rescue
|
||||||
_e in ErlangError ->
|
_e in ErlangError ->
|
||||||
{:error, "mogrify command not found"}
|
{:error, "mogrify command not found"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter(_), do: :ok
|
def filter(_), do: {:ok, :noop}
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,18 +8,18 @@ defmodule Pleroma.Upload.Filter.Mogrify do
|
||||||
@type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
|
@type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
|
||||||
@type conversions :: conversion() | [conversion()]
|
@type conversions :: conversion() | [conversion()]
|
||||||
|
|
||||||
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
|
@spec filter(Pleroma.Upload.t()) :: {:ok, :atom} | {:error, String.t()}
|
||||||
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
|
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
|
||||||
try do
|
try do
|
||||||
do_filter(file, Pleroma.Config.get!([__MODULE__, :args]))
|
do_filter(file, Pleroma.Config.get!([__MODULE__, :args]))
|
||||||
:ok
|
{:ok, :filtered}
|
||||||
rescue
|
rescue
|
||||||
_e in ErlangError ->
|
_e in ErlangError ->
|
||||||
{:error, "mogrify command not found"}
|
{:error, "mogrify command not found"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter(_), do: :ok
|
def filter(_), do: {:ok, :noop}
|
||||||
|
|
||||||
def do_filter(file, filters) do
|
def do_filter(file, filters) do
|
||||||
file
|
file
|
||||||
|
|
|
@ -24,18 +24,18 @@ defmodule Pleroma.Upload.Filter.AnonymizeFilenameTest do
|
||||||
|
|
||||||
test "it replaces filename on pre-defined text", %{upload_file: upload_file} do
|
test "it replaces filename on pre-defined text", %{upload_file: upload_file} do
|
||||||
Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.png")
|
Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.png")
|
||||||
{:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
{:ok, :filtered, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
||||||
assert name == "custom-file.png"
|
assert name == "custom-file.png"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it replaces filename on pre-defined text expression", %{upload_file: upload_file} do
|
test "it replaces filename on pre-defined text expression", %{upload_file: upload_file} do
|
||||||
Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.{extension}")
|
Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.{extension}")
|
||||||
{:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
{:ok, :filtered, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
||||||
assert name == "custom-file.jpg"
|
assert name == "custom-file.jpg"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it replaces filename on random text", %{upload_file: upload_file} do
|
test "it replaces filename on random text", %{upload_file: upload_file} do
|
||||||
{:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
{:ok, :filtered, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
|
||||||
assert <<_::bytes-size(14)>> <> ".jpg" = name
|
assert <<_::bytes-size(14)>> <> ".jpg" = name
|
||||||
refute name == "an… image.jpg"
|
refute name == "an… image.jpg"
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,6 +25,7 @@ test "adds shasum" do
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
:ok,
|
:ok,
|
||||||
|
:filtered,
|
||||||
%Pleroma.Upload{id: @shasum, path: @shasum <> ".jpg"}
|
%Pleroma.Upload{id: @shasum, path: @shasum <> ".jpg"}
|
||||||
} = Dedupe.filter(upload)
|
} = Dedupe.filter(upload)
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ test "apply exiftool filter" do
|
||||||
tempfile: Path.absname("test/fixtures/DSCN0010_tmp.jpg")
|
tempfile: Path.absname("test/fixtures/DSCN0010_tmp.jpg")
|
||||||
}
|
}
|
||||||
|
|
||||||
assert Filter.Exiftool.filter(upload) == :ok
|
assert Filter.Exiftool.filter(upload) == {:ok, :filtered}
|
||||||
|
|
||||||
{exif_original, 0} = System.cmd("exiftool", ["test/fixtures/DSCN0010.jpg"])
|
{exif_original, 0} = System.cmd("exiftool", ["test/fixtures/DSCN0010.jpg"])
|
||||||
{exif_filtered, 0} = System.cmd("exiftool", ["test/fixtures/DSCN0010_tmp.jpg"])
|
{exif_filtered, 0} = System.cmd("exiftool", ["test/fixtures/DSCN0010_tmp.jpg"])
|
||||||
|
|
|
@ -36,7 +36,7 @@ test "apply mogrify filter" do
|
||||||
save: fn _f, _o -> :ok end
|
save: fn _f, _o -> :ok end
|
||||||
]}
|
]}
|
||||||
]) do
|
]) do
|
||||||
assert Filter.Mogrifun.filter(upload) == :ok
|
assert Filter.Mogrifun.filter(upload) == {:ok, :filtered}
|
||||||
end
|
end
|
||||||
|
|
||||||
Task.await(task)
|
Task.await(task)
|
||||||
|
|
|
@ -33,7 +33,7 @@ test "apply mogrify filter" do
|
||||||
custom: fn _m, _a -> :ok end,
|
custom: fn _m, _a -> :ok end,
|
||||||
custom: fn m, a, o -> send(task.pid, {:apply_filter, {m, a, o}}) end,
|
custom: fn m, a, o -> send(task.pid, {:apply_filter, {m, a, o}}) end,
|
||||||
save: fn _f, _o -> :ok end do
|
save: fn _f, _o -> :ok end do
|
||||||
assert Filter.Mogrify.filter(upload) == :ok
|
assert Filter.Mogrify.filter(upload) == {:ok, :filtered}
|
||||||
end
|
end
|
||||||
|
|
||||||
Task.await(task)
|
Task.await(task)
|
||||||
|
|
Loading…
Reference in a new issue