http.ex 1.1 KB
Newer Older
Maksim's avatar
Maksim committed
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

Maksim's avatar
Maksim committed
5
defmodule Pleroma.Web.MediaProxy.Invalidation.Http do
Maksim's avatar
Maksim committed
6
  @moduledoc false
Maksim's avatar
Maksim committed
7
8
  @behaviour Pleroma.Web.MediaProxy.Invalidation

Maksim's avatar
Maksim committed
9
10
  require Logger

Maksim's avatar
Maksim committed
11
  @impl Pleroma.Web.MediaProxy.Invalidation
feld's avatar
feld committed
12
  def purge(urls, opts \\ []) do
Maksim's avatar
Maksim committed
13
14
15
    method = Keyword.get(opts, :method, :purge)
    headers = Keyword.get(opts, :headers, [])
    options = Keyword.get(opts, :options, [])
Maksim's avatar
Maksim committed
16

Maksim's avatar
Maksim committed
17
18
    Logger.debug("Running cache purge: #{inspect(urls)}")

Maksim's avatar
Maksim committed
19
    Enum.each(urls, fn url ->
Maksim's avatar
Maksim committed
20
21
22
      with {:error, error} <- do_purge(method, url, headers, options) do
        Logger.error("Error while cache purge: url - #{url}, error: #{inspect(error)}")
      end
Maksim's avatar
Maksim committed
23
24
    end)

Maksim's avatar
Maksim committed
25
    {:ok, urls}
Maksim's avatar
Maksim committed
26
  end
Maksim's avatar
Maksim committed
27
28
29
30
31
32
33
34
35
36
37
38
39

  defp do_purge(method, url, headers, options) do
    case Pleroma.HTTP.request(method, url, "", headers, options) do
      {:ok, %{status: status} = env} when 400 <= status and status < 500 ->
        {:error, env}

      {:error, error} = error ->
        error

      _ ->
        {:ok, "success"}
    end
  end
Maksim's avatar
Maksim committed
40
end