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 do
|
2020-05-18 00:22:26 -06:00
|
|
|
@moduledoc false
|
|
|
|
|
2020-06-14 12:02:57 -06:00
|
|
|
@callback purge(list(String.t()), Keyword.t()) :: {:ok, list(String.t())} | {:error, String.t()}
|
2020-05-15 12:34:46 -06:00
|
|
|
|
|
|
|
alias Pleroma.Config
|
2020-06-14 12:02:57 -06:00
|
|
|
alias Pleroma.Web.MediaProxy
|
2020-05-15 12:34:46 -06:00
|
|
|
|
2020-06-14 12:02:57 -06:00
|
|
|
@spec enabled?() :: boolean()
|
|
|
|
def enabled?, do: Config.get([:media_proxy, :invalidation, :enabled])
|
|
|
|
|
|
|
|
@spec purge(list(String.t()) | String.t()) :: {:ok, list(String.t())} | {:error, String.t()}
|
2020-05-15 12:34:46 -06:00
|
|
|
def purge(urls) do
|
2020-06-14 12:02:57 -06:00
|
|
|
prepared_urls = prepare_urls(urls)
|
|
|
|
|
|
|
|
if enabled?() do
|
|
|
|
do_purge(prepared_urls)
|
|
|
|
else
|
|
|
|
{:ok, prepared_urls}
|
|
|
|
end
|
2020-05-15 12:34:46 -06:00
|
|
|
end
|
|
|
|
|
2020-06-14 12:02:57 -06:00
|
|
|
defp do_purge(urls) do
|
2020-05-17 21:48:19 -06:00
|
|
|
provider = Config.get([:media_proxy, :invalidation, :provider])
|
2020-06-14 12:02:57 -06:00
|
|
|
provider.purge(urls, Config.get(provider))
|
2020-05-15 12:34:46 -06:00
|
|
|
end
|
|
|
|
|
2020-06-14 12:02:57 -06:00
|
|
|
def prepare_urls(urls) do
|
|
|
|
urls
|
|
|
|
|> List.wrap()
|
|
|
|
|> Enum.map(&MediaProxy.url(&1))
|
|
|
|
end
|
2020-05-15 12:34:46 -06:00
|
|
|
end
|