2019-10-01 07:36:35 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-01 07:36:35 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.MediaController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Object
|
2019-10-06 14:12:17 +00:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-10-01 07:36:35 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
|
|
|
|
|
2019-10-06 14:12:17 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:media"]})
|
|
|
|
|
|
|
|
plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
|
|
|
|
|
2019-10-01 07:36:35 +00:00
|
|
|
@doc "POST /api/v1/media"
|
|
|
|
def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
|
|
|
|
with {:ok, object} <-
|
|
|
|
ActivityPub.upload(
|
|
|
|
file,
|
|
|
|
actor: User.ap_id(user),
|
|
|
|
description: Map.get(data, "description")
|
|
|
|
) do
|
|
|
|
attachment_data = Map.put(object.data, "id", object.id)
|
|
|
|
|
|
|
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PUT /api/v1/media/:id"
|
|
|
|
def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description})
|
|
|
|
when is_binary(description) do
|
|
|
|
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
|
|
|
|
|
|
|
|
def update(_conn, _data), do: {:error, :bad_request}
|
|
|
|
end
|