1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Upload.Filter.Mogrify do
6 @behaviour Pleroma.Upload.Filter
8 @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
9 @type conversions :: conversion() | [conversion()]
11 @spec filter(Pleroma.Upload.t()) :: {:ok, :atom} | {:error, String.t()}
12 def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
14 do_filter(file, Pleroma.Config.get!([__MODULE__, :args]))
18 {:error, "mogrify command not found"}
22 def filter(_), do: {:ok, :noop}
24 def do_filter(file, filters) do
27 |> mogrify_filter(filters)
28 |> Mogrify.save(in_place: true)
31 defp mogrify_filter(mogrify, nil), do: mogrify
33 defp mogrify_filter(mogrify, [filter | rest]) do
35 |> mogrify_filter(filter)
36 |> mogrify_filter(rest)
39 defp mogrify_filter(mogrify, []), do: mogrify
41 defp mogrify_filter(mogrify, {action, options}) do
42 Mogrify.custom(mogrify, action, options)
45 defp mogrify_filter(mogrify, action) when is_binary(action) do
46 Mogrify.custom(mogrify, action)