1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MediaController do
6 use Pleroma.Web, :controller
9 alias Pleroma.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.ActivityPub.ActivityPub
13 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
14 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
16 plug(OAuthScopesPlug, %{scopes: ["write:media"]})
18 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
20 @doc "POST /api/v1/media"
21 def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
25 actor: User.ap_id(user),
26 description: Map.get(data, "description")
28 attachment_data = Map.put(object.data, "id", object.id)
30 render(conn, "attachment.json", %{attachment: attachment_data})
34 @doc "PUT /api/v1/media/:id"
35 def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
36 when is_binary(description) do
37 with %Object{} = object <- Object.get_by_id(id),
38 true <- Object.authorize_mutation(object, user),
39 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
40 attachment_data = Map.put(data, "id", object.id)
42 render(conn, "attachment.json", %{attachment: attachment_data})
46 def update(_conn, _data), do: {:error, :bad_request}