logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

script.ex (1142B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
  5. @moduledoc false
  6. @behaviour Pleroma.Web.MediaProxy.Invalidation
  7. require Logger
  8. @impl Pleroma.Web.MediaProxy.Invalidation
  9. def purge(urls, opts \\ []) do
  10. args =
  11. urls
  12. |> List.wrap()
  13. |> Enum.uniq()
  14. |> Enum.join(" ")
  15. opts
  16. |> Keyword.get(:script_path)
  17. |> do_purge([args])
  18. |> handle_result(urls)
  19. end
  20. defp do_purge(script_path, args) when is_binary(script_path) do
  21. path = Path.expand(script_path)
  22. Logger.debug("Running cache purge: #{inspect(args)}, #{inspect(path)}")
  23. System.cmd(path, args)
  24. rescue
  25. error -> error
  26. end
  27. defp do_purge(_, _), do: {:error, "not found script path"}
  28. defp handle_result({_result, 0}, urls), do: {:ok, urls}
  29. defp handle_result({:error, error}, urls), do: handle_result(error, urls)
  30. defp handle_result(error, _) do
  31. Logger.error("Error while cache purge: #{inspect(error)}")
  32. {:error, inspect(error)}
  33. end
  34. end