diff options
author | Sergey Suprunenko <suprunenko.s@gmail.com> | 2020-07-11 20:30:15 +0200 |
---|---|---|
committer | Sergey Suprunenko <suprunenko.s@gmail.com> | 2020-11-29 18:10:52 +0100 |
commit | 319177260a2ea05977ecc94320a3ff88c8b15aa6 (patch) | |
tree | 3c00ad2d0f37c84aea59449eec271c71bb8c6fe9 | |
parent | 5a5ff508aa47d5cf64f7be5938a9561e856854dd (diff) | |
download | pleroma-319177260a2ea05977ecc94320a3ff88c8b15aa6.tar.gz |
Add `filename` field to post attachments endpoint
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | docs/API/differences_in_mastoapi_responses.md | 3 | ||||
-rw-r--r-- | docs/API/pleroma_api.md | 6 | ||||
-rw-r--r-- | lib/pleroma/upload.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/operations/media_operation.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/schemas/attachment.ex | 8 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/controllers/media_controller.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/views/status_view.ex | 5 | ||||
-rw-r--r-- | test/pleroma/upload_test.exs | 39 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/controllers/media_controller_test.exs | 8 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/controllers/status_controller_test.exs | 10 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/views/status_view_test.exs | 5 |
12 files changed, 83 insertions, 16 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f4ef66408..109a1ed48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -272,6 +272,7 @@ switched to a new configuration mechanism, however it was not officially removed - Mastodon API: Support for `bot` field in `/api/v1/accounts/update_credentials`. - Mastodon API: Support irreversible property for filters. - Mastodon API: Add pleroma.favicon field to accounts. +- Mastodon API: Add `filename` parameter to `POST /api/v1/media` and `POST /api/v2/media`. - Admin API: endpoints for create/update/delete OAuth Apps. - Admin API: endpoint for status view. - OTP: Add command to reload emoji packs diff --git a/docs/API/differences_in_mastoapi_responses.md b/docs/API/differences_in_mastoapi_responses.md index 6b0ad85d1..90814f657 100644 --- a/docs/API/differences_in_mastoapi_responses.md +++ b/docs/API/differences_in_mastoapi_responses.md @@ -37,7 +37,8 @@ Has these additional fields under the `pleroma` object: Has these additional fields under the `pleroma` object: -- `mime_type`: mime type of the attachment. +- `mime_type`: mime type of the attachment +- `filename`: filename of the attachment ### Attachment cap diff --git a/docs/API/pleroma_api.md b/docs/API/pleroma_api.md index 7a0a80dad..411898627 100644 --- a/docs/API/pleroma_api.md +++ b/docs/API/pleroma_api.md @@ -272,7 +272,8 @@ See [Admin-API](admin_api.md) "url": "https://pleroma.example.org/media/abcdefg.png", "type": "image", "pleroma": { - "mime_type": "image/png" + "mime_type": "image/png", + "filename": "abcdefg.png" } } ``` @@ -291,7 +292,8 @@ See [Admin-API](admin_api.md) "url": "https://pleroma.example.org/media/abcdefg.png", "type": "image", "pleroma": { - "mime_type": "image/png" + "mime_type": "image/png", + "filename": "abcdefg.png" } } ``` diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index db2cc1dae..cc4dcf45e 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -89,7 +89,8 @@ defmodule Pleroma.Upload do "href" => url_from_spec(upload, opts.base_url, url_spec) } ], - "name" => description + "name" => description, + "filename" => Map.get(opts, :filename) || upload.name }} else {:description_limit, _} -> @@ -130,6 +131,7 @@ defmodule Pleroma.Upload do uploader: Keyword.get(opts, :uploader, Pleroma.Config.get([__MODULE__, :uploader])), filters: Keyword.get(opts, :filters, Pleroma.Config.get([__MODULE__, :filters])), description: Keyword.get(opts, :description), + filename: Keyword.get(opts, :filename), base_url: Keyword.get( opts, diff --git a/lib/pleroma/web/api_spec/operations/media_operation.ex b/lib/pleroma/web/api_spec/operations/media_operation.ex index d9c3c42db..cfd436f27 100644 --- a/lib/pleroma/web/api_spec/operations/media_operation.ex +++ b/lib/pleroma/web/api_spec/operations/media_operation.ex @@ -46,6 +46,10 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do type: :string, description: "A plain-text description of the media, for accessibility purposes." }, + filename: %Schema{ + type: :string, + 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/api_spec/schemas/attachment.ex b/lib/pleroma/web/api_spec/schemas/attachment.ex index c6edf6d36..c2984546a 100644 --- a/lib/pleroma/web/api_spec/schemas/attachment.ex +++ b/lib/pleroma/web/api_spec/schemas/attachment.ex @@ -50,7 +50,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Attachment do pleroma: %Schema{ type: :object, properties: %{ - mime_type: %Schema{type: :string, description: "mime type of the attachment"} + mime_type: %Schema{type: :string, description: "mime type of the attachment"}, + filename: %Schema{type: :string, description: "filename of the attachment"} } } }, @@ -62,7 +63,10 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Attachment do preview_url: "someurl", text_url: "someurl", description: nil, - pleroma: %{mime_type: "image/png"} + pleroma: %{ + mime_type: "image/png", + filename: "name.png" + } } }) end diff --git a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex index 161193134..b003a2de6 100644 --- a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex @@ -26,7 +26,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do ActivityPub.upload( file, actor: User.ap_id(user), - description: Map.get(data, :description) + description: Map.get(data, :description), + filename: Map.get(data, :filename) ) do attachment_data = Map.put(object.data, "id", object.id) @@ -42,7 +43,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do ActivityPub.upload( file, actor: User.ap_id(user), - description: Map.get(data, :description) + description: Map.get(data, :description), + filename: Map.get(data, :filename) ) do attachment_data = Map.put(object.data, "id", object.id) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 2301e21cf..ffd110968 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -431,7 +431,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do text_url: href, type: type, description: attachment["name"], - pleroma: %{mime_type: media_type}, + pleroma: %{ + mime_type: media_type, + filename: attachment["filename"] + }, blurhash: attachment["blurhash"] } end diff --git a/test/pleroma/upload_test.exs b/test/pleroma/upload_test.exs index f52d4dff6..e975f57c3 100644 --- a/test/pleroma/upload_test.exs +++ b/test/pleroma/upload_test.exs @@ -61,7 +61,8 @@ defmodule Pleroma.UploadTest do "mediaType" => "image/jpeg", "type" => "Link" } - ] + ], + "filename" => "image.jpg" }} Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end)) @@ -120,6 +121,42 @@ defmodule Pleroma.UploadTest do {:error, :description_too_long} = Upload.store(file, description: "123") end + test "saves filename from opts" do + File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg") + + desc = "sample file" + filename = "test image.jpg" + + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image_tmp.jpg"), + filename: "image.jpg" + } + + {:ok, data} = Upload.store(file, description: desc, filename: filename) + + assert data["name"] == desc + assert data["filename"] == filename + end + + test "saves default filename if opts don't have one" do + File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg") + + desc = "sample file" + filename = "image_tmp.jpg" + + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image_tmp.jpg"), + filename: filename + } + + {:ok, data} = Upload.store(file, description: desc) + + assert data["name"] == desc + assert data["filename"] == filename + end + test "returns a media url" do File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg") diff --git a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs index d2bd57515..5aa077cef 100644 --- a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs @@ -27,15 +27,17 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do test "/api/v1/media", %{conn: conn, image: image} do desc = "Description of the image" + filename = "look at this.jpg" media = conn |> put_req_header("content-type", "multipart/form-data") - |> post("/api/v1/media", %{"file" => image, "description" => desc}) + |> post("/api/v1/media", %{"file" => image, "description" => desc, "filename" => filename}) |> json_response_and_validate_schema(:ok) assert media["type"] == "image" assert media["description"] == desc + assert media["pleroma"]["filename"] == filename assert media["id"] object = Object.get_by_id(media["id"]) @@ -44,11 +46,12 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do test "/api/v2/media", %{conn: conn, user: user, image: image} do desc = "Description of the image" + filename = "look at this.jpg" response = conn |> put_req_header("content-type", "multipart/form-data") - |> post("/api/v2/media", %{"file" => image, "description" => desc}) + |> post("/api/v2/media", %{"file" => image, "description" => desc, "filename" => filename}) |> json_response_and_validate_schema(202) assert media_id = response["id"] @@ -62,6 +65,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do assert media["type"] == "image" assert media["description"] == desc + assert media["pleroma"]["filename"] == filename assert media["id"] object = Object.get_by_id(media["id"]) diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index 30d542dfa..3dce0de27 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -413,7 +413,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = + ActivityPub.upload(file, + actor: user.ap_id, + description: "sample image", + filename: "look at this.jpg" + ) conn = conn @@ -427,7 +432,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do assert %{"media_attachments" => [media_attachment]} = json_response_and_validate_schema(conn, 200) - assert %{"type" => "image"} = media_attachment + assert %{"description" => "sample image", "type" => "image"} = media_attachment + assert media_attachment["pleroma"]["filename"] == "look at this.jpg" end test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now", diff --git a/test/pleroma/web/mastodon_api/views/status_view_test.exs b/test/pleroma/web/mastodon_api/views/status_view_test.exs index f2a7469ed..23a39bc93 100644 --- a/test/pleroma/web/mastodon_api/views/status_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs @@ -465,7 +465,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do } ], "blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn", - "uuid" => 6 + "uuid" => 6, + "filename" => "an_image.png" } expected = %{ @@ -476,7 +477,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do preview_url: "someurl", text_url: "someurl", description: nil, - pleroma: %{mime_type: "image/png"}, + pleroma: %{mime_type: "image/png", filename: "an_image.png"}, blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn" } |