Output attachment objects when listing uploaded media, error if used
Produce an error if the attachment is used in any posts
This commit is contained in:
parent
e0dce797ad
commit
70de22bf9b
4 changed files with 18 additions and 7 deletions
|
|
@ -83,7 +83,8 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do
|
||||||
security: [%{"oAuth" => ["write:media"]}],
|
security: [%{"oAuth" => ["write:media"]}],
|
||||||
parameters: [id_param()],
|
parameters: [id_param()],
|
||||||
responses: %{
|
responses: %{
|
||||||
200 => Operation.response("Media", "application/json", Attachment),
|
200 => Operation.response("Media", "text/plain", nil),
|
||||||
|
400 => Operation.response("Media", "application/json", ApiError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -397,7 +397,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaUtilOperation do
|
||||||
def uploaded_media_operation do
|
def uploaded_media_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["Media"],
|
tags: ["Media"],
|
||||||
summary: "List uploaded media",
|
summary: "List media uploaded by the current user",
|
||||||
security: [%{"oAuth" => ["read:accounts"]}],
|
security: [%{"oAuth" => ["read:accounts"]}],
|
||||||
operationId: "PleromaAPI.UtilController.uploaded_media",
|
operationId: "PleromaAPI.UtilController.uploaded_media",
|
||||||
responses: %{
|
responses: %{
|
||||||
|
|
@ -411,9 +411,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaUtilOperation do
|
||||||
example: ["foo@example.org"]
|
example: ["foo@example.org"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
})
|
||||||
400 => Operation.response("Error", "application/json", ApiError),
|
|
||||||
403 => Operation.response("Error", "application/json", ApiError)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc "DELETE /api/v1/media/:id"
|
||||||
def delete(
|
def delete(
|
||||||
%{
|
%{
|
||||||
assigns: %{user: user},
|
assigns: %{user: user},
|
||||||
|
|
@ -88,11 +89,17 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
|
||||||
) do
|
) do
|
||||||
with %Object{} = object <- Object.get_by_id(id),
|
with %Object{} = object <- Object.get_by_id(id),
|
||||||
:ok <- Object.authorize_access(object, user),
|
:ok <- Object.authorize_access(object, user),
|
||||||
|
href <- object.data["url"] |> List.first |> Map.get("href"),
|
||||||
|
{:used, []} <- {:used, Pleroma.Upload.list_objects_with_attachment(href)},
|
||||||
{:ok, _, _} <- Object.delete(object) do
|
{:ok, _, _} <- Object.delete(object) do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_resp_header("content-type", "text/plain")
|
|> put_resp_header("content-type", "text/plain")
|
||||||
|> send_resp(200, "")
|
|> send_resp(200, "")
|
||||||
|
else
|
||||||
|
{:used, list} ->
|
||||||
|
conn
|
||||||
|
|> put_status(:bad_request)
|
||||||
|
|> json(%{error: "Refusing to delete attachment that is still used by #{Enum.count(list)} objects"})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -286,7 +286,12 @@ defmodule Pleroma.Web.PleromaAPI.UtilController do
|
||||||
Pleroma.Upload.list_uploaded_by(user, 0, 50)
|
Pleroma.Upload.list_uploaded_by(user, 0, 50)
|
||||||
|> Enum.map(
|
|> Enum.map(
|
||||||
fn %Pleroma.Object{ id: id, data: %{ "url" => [ mediaUrl ], "name" => name }, inserted_at: uploaded } ->
|
fn %Pleroma.Object{ id: id, data: %{ "url" => [ mediaUrl ], "name" => name }, inserted_at: uploaded } ->
|
||||||
Map.merge(%{ id: id, uploaded: uploaded, name: name }, mediaUrl) |> Map.drop(["type"]) end
|
%{
|
||||||
|
uploaded: uploaded, attachment: %{
|
||||||
|
description: name, id: to_string(id), url: mediaUrl["href"], pleroma: %{ mime_type: mediaUrl["mediaType"] }, type: "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
json(conn, result)
|
json(conn, result)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue