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.

42 line
1.3KB

  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.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)
  10. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  11. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InstanceDocumentOperation
  12. plug(OAuthScopesPlug, %{scopes: ["read"], admin: true} when action == :show)
  13. plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action in [:update, :delete])
  14. def show(conn, %{name: document_name}) 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(%{body_params: %{file: file}} = conn, %{name: document_name}) do
  23. with {:ok, url} <- InstanceDocument.put(document_name, file.path) do
  24. json(conn, %{"url" => url})
  25. end
  26. end
  27. def delete(conn, %{name: document_name}) do
  28. with :ok <- InstanceDocument.delete(document_name) do
  29. json(conn, %{})
  30. end
  31. end
  32. end