Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Web.ApiSpec.Admin, as: Spec
  7. alias Pleroma.Web.MediaProxy
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  10. plug(
  11. OAuthScopesPlug,
  12. %{scopes: ["read:media_proxy_caches"], admin: true} when action in [:index]
  13. )
  14. plug(
  15. OAuthScopesPlug,
  16. %{scopes: ["write:media_proxy_caches"], admin: true} when action in [:purge, :delete]
  17. )
  18. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  19. defdelegate open_api_operation(action), to: Spec.MediaProxyCacheOperation
  20. def index(%{assigns: %{user: _}} = conn, params) do
  21. entries = fetch_entries(params)
  22. urls = paginate_entries(entries, params.page, params.page_size)
  23. render(conn, "index.json",
  24. urls: urls,
  25. page_size: params.page_size,
  26. count: length(entries)
  27. )
  28. end
  29. defp fetch_entries(params) do
  30. MediaProxy.cache_table()
  31. |> Cachex.stream!(Cachex.Query.create(true, :key))
  32. |> filter_entries(params[:query])
  33. end
  34. defp filter_entries(stream, query) when is_binary(query) do
  35. regex = ~r/#{query}/i
  36. stream
  37. |> Enum.filter(fn url -> String.match?(url, regex) end)
  38. |> Enum.to_list()
  39. end
  40. defp filter_entries(stream, _), do: Enum.to_list(stream)
  41. defp paginate_entries(entries, page, page_size) do
  42. offset = page_size * (page - 1)
  43. Enum.slice(entries, offset, page_size)
  44. end
  45. def delete(%{assigns: %{user: _}, body_params: %{urls: urls}} = conn, _) do
  46. MediaProxy.remove_from_banned_urls(urls)
  47. json(conn, %{})
  48. end
  49. def purge(%{assigns: %{user: _}, body_params: %{urls: urls, ban: ban}} = conn, _) do
  50. MediaProxy.Invalidation.purge(urls)
  51. if ban do
  52. MediaProxy.put_in_banned_urls(urls)
  53. end
  54. json(conn, %{})
  55. end
  56. end