1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
8 @behaviour Pleroma.Web.MediaProxy.Invalidation
12 @impl Pleroma.Web.MediaProxy.Invalidation
13 def purge(urls, opts \\ []) do
16 |> maybe_format_urls(Keyword.get(opts, :url_format))
22 |> Keyword.get(:script_path)
24 |> handle_result(urls)
27 defp do_purge(script_path, args) when is_binary(script_path) do
28 path = Path.expand(script_path)
29 Logger.debug("Running cache purge: #{inspect(args)}, #{inspect(path)}")
30 System.cmd(path, args)
35 defp do_purge(_, _), do: {:error, "not found script path"}
37 defp handle_result({_result, 0}, urls), do: {:ok, urls}
38 defp handle_result({:error, error}, urls), do: handle_result(error, urls)
40 defp handle_result(error, _) do
41 Logger.error("Error while cache purge: #{inspect(error)}")
42 {:error, inspect(error)}
45 def maybe_format_urls(urls, :htcacheclean) do
51 if !is_nil(uri.query) do
57 uri.scheme <> "://" <> uri.host <> ":#{inspect(uri.port)}" <> uri.path <> query
61 def maybe_format_urls(urls, _), do: urls