pleroma/lib/pleroma/web/media_proxy/invalidations/script.ex

44 lines
1.1 KiB
Elixir
Raw Normal View History

2020-05-18 08:22:26 +02: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 20:34:46 +02:00
defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
2020-05-18 08:22:26 +02:00
@moduledoc false
2020-05-15 20:34:46 +02:00
@behaviour Pleroma.Web.MediaProxy.Invalidation
2020-05-18 08:22:26 +02:00
require Logger
2020-05-15 20:34:46 +02:00
@impl Pleroma.Web.MediaProxy.Invalidation
2020-06-14 20:02:57 +02:00
def purge(urls, opts) do
2020-05-16 14:16:33 +02:00
args =
urls
|> List.wrap()
|> Enum.uniq()
|> Enum.join(" ")
2020-06-14 20:02:57 +02:00
opts
|> Keyword.get(:script_path, nil)
|> do_purge([args])
|> handle_result(urls)
2020-05-18 08:22:26 +02:00
end
2020-06-14 20:02:57 +02: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 08:22:26 +02:00
System.cmd(path, args)
rescue
2020-06-14 20:02:57 +02: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 20:34:46 +02:00
end
end