Add filename field to post attachments endpoint

This commit is contained in:
Sergey Suprunenko 2020-07-11 20:30:15 +02:00
parent 5a5ff508aa
commit 319177260a
No known key found for this signature in database
GPG Key ID: 5DCA7D1BE3914F9C
12 changed files with 83 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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"
}
}
```

View File

@ -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,

View File

@ -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."

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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")

View File

@ -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"])

View File

@ -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",

View File

@ -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"
}