1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Web.ApiSpec.Admin, as: Spec
9 alias Pleroma.Web.MediaProxy
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
12 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
14 plug(Pleroma.Web.ApiSpec.CastAndValidate)
18 %{scopes: ["read:media_proxy_caches"], admin: true} when action in [:index]
23 %{scopes: ["write:media_proxy_caches"], admin: true} when action in [:purge, :delete]
26 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
28 defdelegate open_api_operation(action), to: Spec.MediaProxyCacheOperation
30 def index(%{assigns: %{user: _}} = conn, params) do
31 entries = fetch_entries(params)
32 urls = paginate_entries(entries, params.page, params.page_size)
34 render(conn, "index.json",
36 page_size: params.page_size,
37 count: length(entries)
41 defp fetch_entries(params) do
42 MediaProxy.cache_table()
43 |> @cachex.stream!(Cachex.Query.create(true, :key))
44 |> filter_entries(params[:query])
47 defp filter_entries(stream, query) when is_binary(query) do
51 |> Enum.filter(fn url -> String.match?(url, regex) end)
55 defp filter_entries(stream, _), do: Enum.to_list(stream)
57 defp paginate_entries(entries, page, page_size) do
58 offset = page_size * (page - 1)
59 Enum.slice(entries, offset, page_size)
62 def delete(%{assigns: %{user: _}, body_params: %{urls: urls}} = conn, _) do
63 MediaProxy.remove_from_banned_urls(urls)
67 def purge(%{assigns: %{user: _}, body_params: %{urls: urls, ban: ban}} = conn, _) do
68 MediaProxy.Invalidation.purge(urls)
71 MediaProxy.put_in_banned_urls(urls)