2019-06-26 21:06:58 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
|
|
|
|
@moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
|
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF
|
|
|
|
|
|
|
|
alias Pleroma.HTTP
|
|
|
|
alias Pleroma.Web.MediaProxy
|
2019-08-13 11:20:26 -06:00
|
|
|
alias Pleroma.Workers.BackgroundWorker
|
2019-06-26 21:06:58 -06:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@hackney_options [
|
|
|
|
pool: :media,
|
|
|
|
recv_timeout: 10_000
|
|
|
|
]
|
|
|
|
|
|
|
|
def perform(:prefetch, url) do
|
2019-12-11 03:46:07 -07:00
|
|
|
Logger.debug("Prefetching #{inspect(url)}")
|
2019-06-26 21:06:58 -06:00
|
|
|
|
|
|
|
url
|
|
|
|
|> MediaProxy.url()
|
|
|
|
|> HTTP.get([], adapter: @hackney_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
|
|
|
|
Enum.each(attachments, fn
|
|
|
|
%{"url" => url} when is_list(url) ->
|
|
|
|
url
|
|
|
|
|> Enum.each(fn
|
|
|
|
%{"href" => href} ->
|
2019-08-31 12:58:42 -06:00
|
|
|
BackgroundWorker.enqueue("media_proxy_prefetch", %{"url" => href})
|
2019-06-26 21:06:58 -06:00
|
|
|
|
|
|
|
x ->
|
|
|
|
Logger.debug("Unhandled attachment URL object #{inspect(x)}")
|
|
|
|
end)
|
|
|
|
|
|
|
|
x ->
|
|
|
|
Logger.debug("Unhandled attachment #{inspect(x)}")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(
|
|
|
|
%{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
|
|
|
|
)
|
|
|
|
when is_list(attachments) and length(attachments) > 0 do
|
2019-08-31 12:58:42 -06:00
|
|
|
BackgroundWorker.enqueue("media_proxy_preload", %{"message" => message})
|
2019-06-26 21:06:58 -06:00
|
|
|
|
|
|
|
{:ok, message}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(message), do: {:ok, message}
|
2019-08-13 15:52:54 -06:00
|
|
|
|
|
|
|
@impl true
|
2019-08-13 16:36:24 -06:00
|
|
|
def describe, do: {:ok, %{}}
|
2019-06-26 21:06:58 -06:00
|
|
|
end
|