First
[anni] / lib / pleroma / web / media_proxy / invalidation / http.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MediaProxy.Invalidation.Http do
6   @moduledoc false
7   @behaviour Pleroma.Web.MediaProxy.Invalidation
8
9   require Logger
10
11   @impl Pleroma.Web.MediaProxy.Invalidation
12   def purge(urls, opts \\ []) do
13     method = Keyword.get(opts, :method, :purge)
14     headers = Keyword.get(opts, :headers, [])
15     options = Keyword.get(opts, :options, [])
16
17     Logger.debug("Running cache purge: #{inspect(urls)}")
18
19     Enum.each(urls, fn url ->
20       with {:error, error} <- do_purge(method, url, headers, options) do
21         Logger.error("Error while cache purge: url - #{url}, error: #{inspect(error)}")
22       end
23     end)
24
25     {:ok, urls}
26   end
27
28   defp do_purge(method, url, headers, options) do
29     case Pleroma.HTTP.request(method, url, "", headers, options) do
30       {:ok, %{status: status} = env} when 400 <= status and status < 500 ->
31         {:error, env}
32
33       {:error, _} = error ->
34         error
35
36       _ ->
37         {:ok, "success"}
38     end
39   end
40 end