Add media upload endpoint.

This commit is contained in:
Roger Braun 2017-09-14 08:08:32 +02:00
parent 3184939055
commit 641c24cdd4
4 changed files with 29 additions and 0 deletions

View file

@ -199,6 +199,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
render conn, AccountView, "relationships.json", %{user: user, targets: targets} render conn, AccountView, "relationships.json", %{user: user, targets: targets}
end end
def upload(%{assigns: %{user: user}} = conn, %{"file" => file}) do
with {:ok, object} <- ActivityPub.upload(file) do
data = object.data
|> Map.put("id", object.id)
render conn, StatusView, "attachment.json", %{attachment: data}
end
end
def empty_array(conn, _) do def empty_array(conn, _) do
Logger.debug("Unimplemented, returning an empty array") Logger.debug("Unimplemented, returning an empty array")
json(conn, []) json(conn, [])

View file

@ -55,6 +55,8 @@ defmodule Pleroma.Web.Router do
post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status
get "/notifications", MastodonAPIController, :notifications get "/notifications", MastodonAPIController, :notifications
post "/media", MastodonAPIController, :upload
end end
scope "/api/v1", Pleroma.Web.MastodonAPI do scope "/api/v1", Pleroma.Web.MastodonAPI do

View file

@ -213,4 +213,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert %{"error" => "Can't find user"} = json_response(conn, 404) assert %{"error" => "Can't find user"} = json_response(conn, 404)
end end
test "media upload", %{conn: conn} do
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
user = insert(:user)
conn = conn
|> assign(:user, user)
|> post("/api/v1/media", %{"file" => file})
assert media = json_response(conn, 200)
assert media["type"] == "image"
end
end end

View file

@ -76,5 +76,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
} }
assert expected == StatusView.render("attachment.json", %{attachment: object}) assert expected == StatusView.render("attachment.json", %{attachment: object})
# If theres a "id", use that instead of the generated one
object = Map.put(object, "id", 2)
assert %{id: 2} = StatusView.render("attachment.json", %{attachment: object})
end end
end end