Return the file content for `GET /api/pleroma/admin/instance_document/:document_name`

This commit is contained in:
eugenijm 2020-09-17 16:54:38 +03:00
parent 582ad5d4e1
commit c711a2b157
5 changed files with 26 additions and 21 deletions

View File

@ -1458,23 +1458,23 @@ Loads json generated from `config/descriptions.exs`.
## `GET /api/pleroma/admin/instance_document/:document_name`
### Gets an instance document
### Get an instance document
- Authentication: required
- Response:
``` json
{
"url": "https://example.com/instance/panel.html"
}
Returns the content of the document
```html
<h1>Instance panel</h1>
```
## `PATCH /api/pleroma/admin/instance_document/:document_name`
- Params:
- `file` (the file to be uploaded, using multipart form data.)
### Updates an instance document
### Update an instance document
- Authentication: required

View File

@ -5,6 +5,7 @@
defmodule Pleroma.Web.AdminAPI.InstanceDocumentController do
use Pleroma.Web, :controller
alias Pleroma.Plugs.InstanceStatic
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.InstanceDocument
@ -18,8 +19,11 @@ defmodule Pleroma.Web.AdminAPI.InstanceDocumentController do
plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action in [:update, :delete])
def show(conn, %{name: document_name}) do
with {:ok, url} <- InstanceDocument.get(document_name) do
json(conn, %{"url" => url})
with {:ok, url} <- InstanceDocument.get(document_name),
{:ok, content} <- File.read(InstanceStatic.file_path(url)) do
conn
|> put_resp_content_type("text/html")
|> send_resp(200, content)
end
end

View File

@ -26,7 +26,7 @@ def show_operation do
| Helpers.admin_api_params()
],
responses: %{
200 => Operation.response("InstanceDocument", "application/json", instance_document()),
200 => document_content(),
400 => Operation.response("Bad Request", "application/json", ApiError),
403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError)
@ -105,4 +105,11 @@ defp instance_document do
}
}
end
defp document_content do
Operation.response("InstanceDocumentContent", "text/html", %Schema{
type: :string,
example: "<h1>Instance panel</h1>"
})
end
end

View File

@ -14,7 +14,7 @@ defmodule Pleroma.Web.InstanceDocument do
@spec get(String.t()) :: {:ok, String.t()} | {:error, atom()}
def get(document_name) do
case Map.fetch(@instance_documents, document_name) do
{:ok, path} -> {:ok, Path.join(Endpoint.url(), path)}
{:ok, path} -> {:ok, path}
_ -> {:error, :not_found}
end
end

View File

@ -33,10 +33,8 @@ defmodule Pleroma.Web.AdminAPI.InstanceDocumentControllerTest do
test "return the instance document url", %{conn: conn} do
conn = get(conn, "/api/pleroma/admin/instance_document/instance-panel")
assert %{"url" => url} = json_response_and_validate_schema(conn, 200)
index = get(build_conn(), url)
response = html_response(index, 200)
assert String.contains?(response, @default_instance_panel)
assert content = html_response(conn, 200)
assert String.contains?(content, @default_instance_panel)
end
test "it returns 403 if requested by a non-admin" do
@ -91,9 +89,7 @@ test "deletes the instance document", %{conn: conn} do
conn
|> get("/api/pleroma/admin/instance_document/instance-panel")
assert %{"url" => url} = json_response_and_validate_schema(conn_resp, 200)
index = get(build_conn(), url)
assert html_response(index, 200) == "Custom instance panel"
assert html_response(conn_resp, 200) == "Custom instance panel"
conn
|> delete("/api/pleroma/admin/instance_document/instance-panel")
@ -103,10 +99,8 @@ test "deletes the instance document", %{conn: conn} do
conn
|> get("/api/pleroma/admin/instance_document/instance-panel")
assert %{"url" => url} = json_response_and_validate_schema(conn_resp, 200)
index = get(build_conn(), url)
response = html_response(index, 200)
assert String.contains?(response, @default_instance_panel)
assert content = html_response(conn_resp, 200)
assert String.contains?(content, @default_instance_panel)
end
end
end