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!()
setup_instrumenters()
load_custom_modules()
check_system_commands()
Pleroma.Docs.JSON.compile()
adapter = Application.get_env(:tesla, :adapter)
@ -249,4 +250,21 @@ defmodule Pleroma.Application do
end
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

View File

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

View File

@ -34,12 +34,14 @@ defmodule Pleroma.Upload.Filter.Mogrifun do
[{"fill", "yellow"}, {"tint", "40"}]
]
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
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)])
:ok
else
{:error, "mogrify command not found"}
rescue
_e in ErlangError ->
{:error, "mogrify command not found"}
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 conversions :: conversion() | [conversion()]
@spec filter(Pleroma.Upload.t()) :: :ok | {:error, String.t()}
def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
if Pleroma.Utils.command_available?("mogrify") do
filters = Pleroma.Config.get!([__MODULE__, :args])
do_filter(file, filters)
try do
do_filter(file, Pleroma.Config.get!([__MODULE__, :args]))
:ok
else
{:error, "mogrify command not found"}
rescue
_e in ErlangError ->
{:error, "mogrify command not found"}
end
end