fix tests
[akkoma] / lib / pleroma / web / media_proxy / invalidations / script.ex
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
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, %{script_path: script_path} = _options) do
14 args =
15 urls
16 |> List.wrap()
17 |> Enum.uniq()
18 |> Enum.join(" ")
19
20 path = Path.expand(script_path)
21
22 Logger.debug("Running cache purge: #{inspect(urls)}, #{path}")
23
24 case do_purge(path, [args]) do
25 {result, exit_status} when exit_status > 0 ->
26 Logger.error("Error while cache purge: #{inspect(result)}")
27 {:error, inspect(result)}
28
29 _ ->
30 {:ok, "success"}
31 end
32 end
33
34 def purge(_, _), do: {:error, "not found script path"}
35
36 defp do_purge(path, args) do
37 System.cmd(path, args)
38 rescue
39 error -> {inspect(error), 1}
40 end
41 end