logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

script.ex (1572B)


  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. 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. |> maybe_format_urls(Keyword.get(opts, :url_format))
  13. |> List.wrap()
  14. |> Enum.uniq()
  15. |> Enum.join(" ")
  16. opts
  17. |> Keyword.get(:script_path)
  18. |> do_purge([args])
  19. |> handle_result(urls)
  20. end
  21. defp do_purge(script_path, args) when is_binary(script_path) do
  22. path = Path.expand(script_path)
  23. Logger.debug("Running cache purge: #{inspect(args)}, #{inspect(path)}")
  24. System.cmd(path, args)
  25. rescue
  26. error -> error
  27. end
  28. defp do_purge(_, _), do: {:error, "not found script path"}
  29. defp handle_result({_result, 0}, urls), do: {:ok, urls}
  30. defp handle_result({:error, error}, urls), do: handle_result(error, urls)
  31. defp handle_result(error, _) do
  32. Logger.error("Error while cache purge: #{inspect(error)}")
  33. {:error, inspect(error)}
  34. end
  35. def maybe_format_urls(urls, :htcacheclean) do
  36. urls
  37. |> Enum.map(fn url ->
  38. uri = URI.parse(url)
  39. query =
  40. if !is_nil(uri.query) do
  41. "?" <> uri.query
  42. else
  43. "?"
  44. end
  45. uri.scheme <> "://" <> uri.host <> ":#{inspect(uri.port)}" <> uri.path <> query
  46. end)
  47. end
  48. def maybe_format_urls(urls, _), do: urls
  49. end