2019-10-01 09:36:35 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-01 09:36:35 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.MediaController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Object
|
2019-10-06 16:12:17 +02:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-10-01 09:36:35 +02:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
2020-05-14 10:50:12 +02:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2019-10-01 09:36:35 +02:00
|
|
|
plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
|
|
|
|
|
2019-10-06 16:12:17 +02:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:media"]})
|
|
|
|
|
2020-05-14 10:50:12 +02:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
|
|
|
|
|
2019-10-01 09:36:35 +02:00
|
|
|
@doc "POST /api/v1/media"
|
2020-05-14 14:18:30 +02:00
|
|
|
def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
|
2019-10-01 09:36:35 +02:00
|
|
|
with {:ok, object} <-
|
|
|
|
ActivityPub.upload(
|
|
|
|
file,
|
|
|
|
actor: User.ap_id(user),
|
2020-05-14 14:18:30 +02:00
|
|
|
description: Map.get(data, :description)
|
2019-10-01 09:36:35 +02:00
|
|
|
) do
|
|
|
|
attachment_data = Map.put(object.data, "id", object.id)
|
|
|
|
|
|
|
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 17:38:14 +02:00
|
|
|
def create(_conn, _data), do: {:error, :bad_request}
|
|
|
|
|
|
|
|
@doc "POST /api/v2/media"
|
2020-05-14 14:18:30 +02:00
|
|
|
def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
|
2020-04-29 17:38:14 +02:00
|
|
|
with {:ok, object} <-
|
|
|
|
ActivityPub.upload(
|
|
|
|
file,
|
|
|
|
actor: User.ap_id(user),
|
2020-05-14 14:18:30 +02:00
|
|
|
description: Map.get(data, :description)
|
2020-04-29 17:38:14 +02:00
|
|
|
) do
|
|
|
|
attachment_data = Map.put(object.data, "id", object.id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(202)
|
|
|
|
|> render("attachment.json", %{attachment: attachment_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create2(_conn, _data), do: {:error, :bad_request}
|
|
|
|
|
2019-10-01 09:36:35 +02:00
|
|
|
@doc "PUT /api/v1/media/:id"
|
2020-05-14 14:29:32 +02:00
|
|
|
def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do
|
2019-10-01 09:36:35 +02:00
|
|
|
with %Object{} = object <- Object.get_by_id(id),
|
|
|
|
true <- Object.authorize_mutation(object, user),
|
|
|
|
{:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
|
|
|
|
attachment_data = Map.put(data, "id", object.id)
|
|
|
|
|
|
|
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 14:29:32 +02:00
|
|
|
def update(conn, data), do: show(conn, data)
|
2020-04-29 17:38:14 +02:00
|
|
|
|
|
|
|
@doc "GET /api/v1/media/:id"
|
2020-05-14 14:18:30 +02:00
|
|
|
def show(conn, %{id: id}) do
|
2020-04-29 17:38:14 +02:00
|
|
|
with %Object{data: data, id: object_id} <- Object.get_by_id(id) do
|
|
|
|
attachment_data = Map.put(data, "id", object_id)
|
|
|
|
|
|
|
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_media(_conn, _data), do: {:error, :bad_request}
|
2019-10-01 09:36:35 +02:00
|
|
|
end
|