akkoma/lib/pleroma/web/media_proxy/invalidation/script.ex

44 lines
1.1 KiB
Elixir
Raw Normal View History

2020-05-18 00:22:26 -06:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2020-05-15 12:34:46 -06:00
defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
2020-05-18 00:22:26 -06:00
@moduledoc false
2020-05-15 12:34:46 -06:00
@behaviour Pleroma.Web.MediaProxy.Invalidation
2020-05-18 00:22:26 -06:00
require Logger
2020-05-15 12:34:46 -06:00
@impl Pleroma.Web.MediaProxy.Invalidation
def purge(urls, opts \\ []) do
2020-05-16 06:16:33 -06:00
args =
urls
|> List.wrap()
|> Enum.uniq()
|> Enum.join(" ")
2020-06-14 12:02:57 -06:00
opts
|> Keyword.get(:script_path)
2020-06-14 12:02:57 -06:00
|> do_purge([args])
|> handle_result(urls)
2020-05-18 00:22:26 -06:00
end
2020-06-14 12:02:57 -06:00
defp do_purge(script_path, args) when is_binary(script_path) do
path = Path.expand(script_path)
Logger.debug("Running cache purge: #{inspect(args)}, #{inspect(path)}")
2020-05-18 00:22:26 -06:00
System.cmd(path, args)
rescue
2020-06-14 12:02:57 -06:00
error -> error
end
defp do_purge(_, _), do: {:error, "not found script path"}
defp handle_result({_result, 0}, urls), do: {:ok, urls}
defp handle_result({:error, error}, urls), do: handle_result(error, urls)
defp handle_result(error, _) do
Logger.error("Error while cache purge: #{inspect(error)}")
{:error, inspect(error)}
2020-05-15 12:34:46 -06:00
end
end