logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

instance_document_controller.ex (1556B)


  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. defmodule Pleroma.Web.AdminAPI.InstanceDocumentController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Web.InstanceDocument
  7. alias Pleroma.Web.Plugs.InstanceStatic
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  10. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  11. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InstanceDocumentOperation
  12. plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :show)
  13. plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:update, :delete])
  14. def show(%{private: %{open_api_spex: %{params: %{name: document_name}}}} = conn, _) do
  15. with {:ok, url} <- InstanceDocument.get(document_name),
  16. {:ok, content} <- File.read(InstanceStatic.file_path(url)) do
  17. conn
  18. |> put_resp_content_type("text/html")
  19. |> send_resp(200, content)
  20. end
  21. end
  22. def update(
  23. %{
  24. private: %{open_api_spex: %{body_params: %{file: file}, params: %{name: document_name}}}
  25. } = conn,
  26. _
  27. ) do
  28. with {:ok, url} <- InstanceDocument.put(document_name, file.path) do
  29. json(conn, %{"url" => url})
  30. end
  31. end
  32. def delete(%{private: %{open_api_spex: %{params: %{name: document_name}}}} = conn, _) do
  33. with :ok <- InstanceDocument.delete(document_name) do
  34. json(conn, %{})
  35. end
  36. end
  37. end