Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2KB

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