0f66c2fe323a1b351983f2789a26f3476878fa41
[akkoma] / lib / pleroma / web / media_proxy / invalidation / script.ex
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
5 defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
6 @moduledoc false
7
8 @behaviour Pleroma.Web.MediaProxy.Invalidation
9
10 require Logger
11
12 @impl Pleroma.Web.MediaProxy.Invalidation
13 def purge(urls, opts \\ []) do
14 args =
15 urls
16 |> List.wrap()
17 |> Enum.uniq()
18 |> Enum.join(" ")
19
20 opts
21 |> Keyword.get(:script_path)
22 |> do_purge([args])
23 |> handle_result(urls)
24 end
25
26 defp do_purge(script_path, args) when is_binary(script_path) do
27 path = Path.expand(script_path)
28 Logger.debug("Running cache purge: #{inspect(args)}, #{inspect(path)}")
29 System.cmd(path, args)
30 rescue
31 error -> error
32 end
33
34 defp do_purge(_, _), do: {:error, "not found script path"}
35
36 defp handle_result({_result, 0}, urls), do: {:ok, urls}
37 defp handle_result({:error, error}, urls), do: handle_result(error, urls)
38
39 defp handle_result(error, _) do
40 Logger.error("Error while cache purge: #{inspect(error)}")
41 {:error, inspect(error)}
42 end
43 end