logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma
commit: 641c24cdd46f36205d91a2de7da8bbbfa7aac3ce
parent: 3184939055fb8c1c9577a9d13e69c2c2f7e070c0
Author: Roger Braun <roger@rogerbraun.net>
Date:   Thu, 14 Sep 2017 08:08:32 +0200

Add media upload endpoint.

Diffstat:

Mlib/pleroma/web/mastodon_api/mastodon_api_controller.ex9+++++++++
Mlib/pleroma/web/router.ex2++
Mtest/web/mastodon_api/mastodon_api_controller_test.exs14++++++++++++++
Mtest/web/mastodon_api/status_view_test.exs4++++
4 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -199,6 +199,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do render conn, AccountView, "relationships.json", %{user: user, targets: targets} 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 Logger.debug("Unimplemented, returning an empty array") json(conn, []) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex @@ -55,6 +55,8 @@ defmodule Pleroma.Web.Router do post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status get "/notifications", MastodonAPIController, :notifications + + post "/media", MastodonAPIController, :upload end scope "/api/v1", Pleroma.Web.MastodonAPI do diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -213,4 +213,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert %{"error" => "Can't find user"} = json_response(conn, 404) 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 diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs @@ -76,5 +76,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do } 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