1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.Plugs.OAuthScopesPlug
13 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
14 plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:create, :create2])
15 plug(Pleroma.Web.ApiSpec.CastAndValidate)
16 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
18 plug(OAuthScopesPlug, %{scopes: ["read:media"]} when action == :show)
19 plug(OAuthScopesPlug, %{scopes: ["write:media"]} when action != :show)
21 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
23 @doc "POST /api/v1/media"
24 def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
28 actor: User.ap_id(user),
29 description: Map.get(data, :description)
31 attachment_data = Map.put(object.data, "id", object.id)
33 render(conn, "attachment.json", %{attachment: attachment_data})
37 def create(_conn, _data), do: {:error, :bad_request}
39 @doc "POST /api/v2/media"
40 def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
44 actor: User.ap_id(user),
45 description: Map.get(data, :description)
47 attachment_data = Map.put(object.data, "id", object.id)
51 |> render("attachment.json", %{attachment: attachment_data})
55 def create2(_conn, _data), do: {:error, :bad_request}
57 @doc "PUT /api/v1/media/:id"
58 def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do
59 with %Object{} = object <- Object.get_by_id(id),
60 :ok <- Object.authorize_access(object, user),
61 {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
62 attachment_data = Map.put(data, "id", object.id)
64 render(conn, "attachment.json", %{attachment: attachment_data})
68 def update(conn, data), do: show(conn, data)
70 @doc "GET /api/v1/media/:id"
71 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
72 with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
73 :ok <- Object.authorize_access(object, user) do
74 attachment_data = Map.put(data, "id", object_id)
76 render(conn, "attachment.json", %{attachment: attachment_data})
80 def show(_conn, _data), do: {:error, :bad_request}