aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSergey Suprunenko <suprunenko.s@gmail.com>2020-10-06 20:58:32 +0200
committerSergey Suprunenko <suprunenko.s@gmail.com>2020-11-29 18:10:57 +0100
commit1bbbc15925d3cb3a967993a8bd85a1316050af61 (patch)
treea7e9afe3d387b05f71aa6c237a150c09a487b666 /lib
parent019c211353b70764ae2e70f2f486c9790b460a61 (diff)
downloadpleroma-feature/1902-attachment-filename.tar.gz
Allow updating the media filenamefeature/1902-attachment-filename
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/api_spec/operations/media_operation.ex6
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/media_controller.ex33
2 files changed, 35 insertions, 4 deletions
diff --git a/lib/pleroma/web/api_spec/operations/media_operation.ex b/lib/pleroma/web/api_spec/operations/media_operation.ex
index 4c1496dd7..72234591a 100644
--- a/lib/pleroma/web/api_spec/operations/media_operation.ex
+++ b/lib/pleroma/web/api_spec/operations/media_operation.ex
@@ -90,8 +90,14 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do
},
description: %Schema{
type: :string,
+ nullable: true,
description: "A plain-text description of the media, for accessibility purposes."
},
+ filename: %Schema{
+ type: :string,
+ nullable: true,
+ description: "Filename of the media."
+ },
focus: %Schema{
type: :string,
description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
diff --git a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex
index b003a2de6..175254837 100644
--- a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex
@@ -57,18 +57,21 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
def create2(_conn, _data), do: {:error, :bad_request}
@doc "PUT /api/v1/media/:id"
- def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do
+ def update(
+ %{assigns: %{user: user}, body_params: body_params} = conn,
+ %{id: id}
+ ) do
with %Object{} = object <- Object.get_by_id(id),
:ok <- Object.authorize_access(object, user),
- {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
+ params <- prepare_update_params(body_params),
+ :ok <- validate_filename(params["filename"], hd(object.data["url"])),
+ {:ok, %Object{data: data}} <- Object.update_data(object, params) do
attachment_data = Map.put(data, "id", object.id)
render(conn, "attachment.json", %{attachment: attachment_data})
end
end
- def update(conn, data), do: show(conn, data)
-
@doc "GET /api/v1/media/:id"
def show(%{assigns: %{user: user}} = conn, %{id: id}) do
with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
@@ -80,4 +83,26 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
end
def show(_conn, _data), do: {:error, :bad_request}
+
+ defp prepare_update_params(body_params) do
+ body_params
+ |> Map.take([:description, :filename])
+ |> Enum.into(%{}, fn
+ {:description, description} ->
+ {"name", description}
+
+ {:filename, filename} ->
+ {"filename", filename}
+ end)
+ end
+
+ defp validate_filename(nil, _), do: :ok
+
+ defp validate_filename(filename, %{"href" => href}) do
+ if Path.extname(filename) == Path.extname(href) do
+ :ok
+ else
+ {:error, :invalid_filename_extension}
+ end
+ end
end