Move checks to application startup

This commit is contained in:
Roman Chvanikov 2020-08-05 19:38:55 +03:00
parent 4672b61106
commit 7569f225f1
4 changed files with 38 additions and 14 deletions

View file

@ -47,6 +47,7 @@ defmodule Pleroma.Application do
Pleroma.ApplicationRequirements.verify!() Pleroma.ApplicationRequirements.verify!()
setup_instrumenters() setup_instrumenters()
load_custom_modules() load_custom_modules()
check_system_commands()
Pleroma.Docs.JSON.compile() Pleroma.Docs.JSON.compile()
adapter = Application.get_env(:tesla, :adapter) adapter = Application.get_env(:tesla, :adapter)
@ -249,4 +250,21 @@ defmodule Pleroma.Application do
end end
defp http_children(_, _), do: [] defp http_children(_, _), do: []
defp check_system_commands do
filters = Config.get([Pleroma.Upload, :filters])
check_filter = fn filter, command_required ->
with true <- filter in filters,
false <- Pleroma.Utils.command_available?(command_required) do
Logger.error(
"#{filter} is specified in list of Pleroma.Upload filters, but the #{command_required} command is not found"
)
end
end
check_filter.(Pleroma.Upload.Filters.Exiftool, "exiftool")
check_filter.(Pleroma.Upload.Filters.Mogrify, "mogrify")
check_filter.(Pleroma.Upload.Filters.Mogrifun, "mogrify")
end
end end

View file

@ -9,12 +9,16 @@ defmodule Pleroma.Upload.Filter.Exiftool do
""" """
@behaviour Pleroma.Upload.Filter @behaviour Pleroma.Upload.Filter
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
if Pleroma.Utils.command_available?("exiftool") do try do
System.cmd("exiftool", ["-overwrite_original", "-gps:all=", file], parallelism: true) case System.cmd("exiftool", ["-overwrite_original", "-gps:all=", file], parallelism: true) do
:ok {_response, 0} -> :ok
else {error, 1} -> {:error, error}
{:error, "exiftool command not found"} end
rescue
_e in ErlangError ->
{:error, "exiftool command not found"}
end end
end end

View file

@ -34,12 +34,14 @@ defmodule Pleroma.Upload.Filter.Mogrifun do
[{"fill", "yellow"}, {"tint", "40"}] [{"fill", "yellow"}, {"tint", "40"}]
] ]
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
if Pleroma.Utils.command_available?("mogrify") do try do
Filter.Mogrify.do_filter(file, [Enum.random(@filters)]) Filter.Mogrify.do_filter(file, [Enum.random(@filters)])
:ok :ok
else rescue
{:error, "mogrify command not found"} _e in ErlangError ->
{:error, "mogrify command not found"}
end end
end end

View file

@ -8,14 +8,14 @@ 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()}
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
if Pleroma.Utils.command_available?("mogrify") do try do
filters = Pleroma.Config.get!([__MODULE__, :args]) do_filter(file, Pleroma.Config.get!([__MODULE__, :args]))
do_filter(file, filters)
:ok :ok
else rescue
{:error, "mogrify command not found"} _e in ErlangError ->
{:error, "mogrify command not found"}
end end
end end