move to 2.5.5
[anni] / lib / pleroma / upload / filter.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Upload.Filter do
6   @moduledoc """
7   Upload Filter behaviour
8
9   This behaviour allows to run filtering actions just before a file is uploaded. This allows to:
10
11   * morph in place the temporary file
12   * change any field of a `Pleroma.Upload` struct
13   * cancel/stop the upload
14   """
15
16   require Logger
17
18   @callback filter(upload :: struct()) ::
19               {:ok, :filtered}
20               | {:ok, :noop}
21               | {:ok, :filtered, upload :: struct()}
22               | {:error, any()}
23
24   @spec filter([module()], upload :: struct()) :: {:ok, upload :: struct()} | {:error, any()}
25
26   def filter([], upload) do
27     {:ok, upload}
28   end
29
30   def filter([filter | rest], upload) do
31     case filter.filter(upload) do
32       {:ok, :filtered} ->
33         filter(rest, upload)
34
35       {:ok, :filtered, upload} ->
36         filter(rest, upload)
37
38       {:ok, :noop} ->
39         filter(rest, upload)
40
41       error ->
42         Logger.error("#{__MODULE__}: Filter #{filter} failed: #{inspect(error)}")
43         error
44     end
45   end
46 end