mogrify.ex 937 Bytes
Newer Older
href's avatar
href committed
1
defmodule Pleroma.Upload.Filter.Mogrify do
Maksim's avatar
Maksim committed
2
  @behaviour Pleroma.Upload.Filter
href's avatar
href committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
  @type conversions :: conversion() | [conversion()]

  def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
    filters = Pleroma.Config.get!([__MODULE__, :args])

    file
    |> Mogrify.open()
    |> mogrify_filter(filters)
    |> Mogrify.save(in_place: true)

    :ok
  end

  def filter(_), do: :ok

  defp mogrify_filter(mogrify, nil), do: mogrify

  defp mogrify_filter(mogrify, [filter | rest]) do
    mogrify
    |> mogrify_filter(filter)
    |> mogrify_filter(rest)
  end

  defp mogrify_filter(mogrify, []), do: mogrify

  defp mogrify_filter(mogrify, {action, options}) do
    Mogrify.custom(mogrify, action, options)
  end

  defp mogrify_filter(mogrify, action) when is_binary(action) do
    Mogrify.custom(mogrify, action)
  end
end