Compare commits

...

6 Commits

Author SHA1 Message Date
Sergey Suprunenko 1bbbc15925
Allow updating the media filename 2020-11-29 18:10:57 +01:00
Sergey Suprunenko 019c211353
Try to use custom filename in attachment_links 2020-11-29 18:10:57 +01:00
Sergey Suprunenko 3000f3ff7c
Check for custom filename extension 2020-11-29 18:10:56 +01:00
Sergey Suprunenko 19713aca3e
Update docs 2020-11-29 18:10:56 +01:00
Sergey Suprunenko 057da2dd9c
Add `show_attachment_filenames` field to users 2020-11-29 18:10:55 +01:00
Sergey Suprunenko 319177260a
Add `filename` field to post attachments endpoint 2020-11-29 18:10:52 +01:00
24 changed files with 314 additions and 32 deletions

View File

@ -110,6 +110,8 @@ switched to a new configuration mechanism, however it was not officially removed
- Admin API: Importing emoji from a zip file
- Pleroma API: Importing the mutes users from CSV files.
- Pleroma API: Pagination for remote/local packs and emoji.
- Mastodon API: Add `filename` parameter to `POST /api/v1/media`, `POST /api/v2/media` and `PUT /api/v1/media/:id`
- Mastodon API: Add account setting for providing filename to post attachments
</details>

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
@ -77,6 +78,8 @@ Has these additional fields under the `pleroma` object:
- `notification_settings`: object, can be absent. See `/api/pleroma/notification_settings` for the parameters/keys returned.
- `accepts_chat_messages`: boolean, but can be null if we don't have that information about a user
- `favicon`: nullable URL string, Favicon image of the user's instance
- `show_attachment_filenames`: boolean, true when the user wants to display post attachment filenames
- `skip_thread_containment`: boolean, true when the user has skipping filtering out broken threads enabled
### Source
@ -204,6 +207,7 @@ Additional parameters can be added to the JSON body/Form data:
- `show_role` - if true, user's role (e.g admin, moderator) will be exposed to anyone in the API
- `default_scope` - the scope returned under `privacy` key in Source subentity
- `pleroma_settings_store` - Opaque user settings to be saved on the backend.
- `show_attachment_filenames` - if true, the post attachment filenames will be displayed
- `skip_thread_containment` - if true, skip filtering out broken threads
- `allow_following_move` - if true, allows automatically follow moved following accounts
- `pleroma_background_image` - sets the background image of the user. Can be set to "" (an empty string) to reset.

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

@ -72,6 +72,7 @@ def store(upload, opts \\ []) do
with {:ok, upload} <- prepare_upload(upload, opts),
upload = %__MODULE__{upload | path: upload.path || "#{upload.id}/#{upload.name}"},
:ok <- check_filename_extension(upload.name, opts),
{:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
description = get_description(opts, upload),
{_, true} <-
@ -89,7 +90,8 @@ def store(upload, opts \\ []) do
"href" => url_from_spec(upload, opts.base_url, url_spec)
}
],
"name" => description
"name" => description,
"filename" => Map.get(opts, :filename)
}}
else
{:description_limit, _} ->
@ -130,6 +132,7 @@ defp get_opts(opts) 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,
@ -197,6 +200,16 @@ defp check_file_size(path, size_limit) when is_integer(size_limit) and size_limi
defp check_file_size(_, _), do: :ok
defp check_filename_extension(name, %{filename: filename}) when is_binary(filename) do
if Path.extname(name) == Path.extname(filename) do
:ok
else
{:error, :invalid_filename_extension}
end
end
defp check_filename_extension(_, _), do: :ok
# Creates a tempfile using the Plug.Upload Genserver which cleans them up
# automatically.
defp tempfile_for_image(data) do

View File

@ -138,6 +138,7 @@ defmodule Pleroma.User do
field(:is_discoverable, :boolean, default: false)
field(:invisible, :boolean, default: false)
field(:allow_following_move, :boolean, default: true)
field(:show_attachment_filenames, :boolean, default: false)
field(:skip_thread_containment, :boolean, default: false)
field(:actor_type, :string, default: "Person")
field(:also_known_as, {:array, :string}, default: [])
@ -503,6 +504,7 @@ def update_changeset(struct, params \\ %{}) do
:allow_following_move,
:background,
:show_role,
:show_attachment_filenames,
:skip_thread_containment,
:fields,
:raw_fields,

View File

@ -48,6 +48,7 @@ def render("credentials.json", %{user: user, for: for_user}) do
:hide_followers,
:hide_favorites,
:allow_following_move,
:show_attachment_filenames,
:show_role,
:skip_thread_containment,
:pleroma_settings_store,

View File

@ -604,6 +604,11 @@ defp update_credentials_request do
nullable: true,
description: "Opaque user settings to be saved on the backend."
},
show_attachment_filenames: %Schema{
allOf: [BooleanLike],
nullable: true,
description: "Show the attachment filenames"
},
skip_thread_containment: %Schema{
allOf: [BooleanLike],
nullable: true,
@ -642,6 +647,7 @@ defp update_credentials_request do
show_role: false,
default_scope: "private",
pleroma_settings_store: %{"pleroma-fe" => %{"key" => "val"}},
show_attachment_filenames: false,
skip_thread_containment: false,
allow_following_move: false,
discoverable: false,

View File

@ -46,6 +46,11 @@ defp create_request do
type: :string,
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."
@ -85,8 +90,14 @@ defp update_request 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."

View File

@ -76,6 +76,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
type: :boolean,
description: "whether the user is a moderator of the local instance"
},
show_attachment_filenames: %Schema{type: :boolean},
skip_thread_containment: %Schema{type: :boolean},
tags: %Schema{
type: :array,
@ -172,6 +173,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
"hide_follows_count" => false,
"is_admin" => false,
"is_moderator" => false,
"show_attachment_filenames" => false,
"skip_thread_containment" => false,
"accepts_chat_messages" => true,
"chat_token" =>

View File

@ -50,7 +50,12 @@ 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,
nullable: true,
description: "filename of the attachment"
}
}
}
},
@ -62,7 +67,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

@ -31,6 +31,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Chat do
"hide_follows_count" => false,
"hide_follows" => false,
"background_image" => nil,
"show_attachment_filenames" => false,
"skip_thread_containment" => false,
"hide_followers" => false,
"relationship" => %{},

View File

@ -278,6 +278,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Status do
"showing_reblogs" => true,
"subscribing" => false
},
"show_attachment_filenames" => false,
"skip_thread_containment" => false,
"tags" => []
},

View File

@ -255,7 +255,7 @@ def add_attachments(text, attachments) do
end
defp build_attachment_link(%{"url" => [%{"href" => href} | _]} = attachment) do
name = attachment["name"] || URI.decode(Path.basename(href))
name = attachment["filename"] || attachment["name"] || URI.decode(Path.basename(href))
href = MediaProxy.url(href)
"<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
end

View File

@ -182,6 +182,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p
:hide_followers,
:hide_follows,
:hide_favorites,
:show_attachment_filenames,
:show_role,
:skip_thread_containment,
:allow_following_move,

View File

@ -26,7 +26,8 @@ def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn,
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 @@ def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn,
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)
@ -55,18 +57,21 @@ def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn,
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),
@ -78,4 +83,26 @@ def show(%{assigns: %{user: user}} = conn, %{id: id}) 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

View File

@ -277,6 +277,7 @@ defp do_render("show.json", %{user: user} = opts) do
hide_follows: user.hide_follows,
hide_favorites: user.hide_favorites,
relationship: relationship,
show_attachment_filenames: user.show_attachment_filenames,
skip_thread_containment: user.skip_thread_containment,
background_image: image_url(user.background) |> MediaProxy.url(),
accepts_chat_messages: user.accepts_chat_messages,

View File

@ -431,7 +431,10 @@ def render("attachment.json", %{attachment: attachment}) 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

@ -0,0 +1,9 @@
defmodule Pleroma.Repo.Migrations.AddAttachmentFilenameSettingToUsers do
use Ecto.Migration
def change do
alter table(:users) do
add(:show_attachment_filenames, :boolean, default: false)
end
end
end

View File

@ -61,7 +61,8 @@ test "it returns file" do
"mediaType" => "image/jpeg",
"type" => "Link"
}
]
],
"filename" => nil
}}
Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
@ -120,6 +121,57 @@ test "does not allow descriptions longer than the post limit" 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 "sets filename to nil 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
refute data["filename"]
end
@tag capture_log: true
test "raise error when custom filename has different extension than original one" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
fake_name = "free_coins.exe"
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "image_tmp.jpg"
}
assert Upload.store(file, filename: fake_name) == {:error, :invalid_filename_extension}
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 @@ test "/api/v1/media", %{conn: conn, image: image} 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,14 +65,41 @@ test "/api/v2/media", %{conn: conn, user: user, image: image} do
assert media["type"] == "image"
assert media["description"] == desc
assert media["pleroma"]["filename"] == filename
assert media["id"]
object = Object.get_by_id(media["id"])
assert object.data["actor"] == user.ap_id
end
test "returns error when description is too long", %{conn: conn, image: image} do
clear_config([:instance, :description_limit], 2)
response =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/media", %{"file" => image, "description" => "test-media"})
|> json_response(400)
assert response["error"] == "description_too_long"
end
@tag capture_log: true
test "returns error when custom filename has different extension than original one", %{
conn: conn,
image: image
} do
response =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/media", %{"file" => image, "filename" => "wrong.gif"})
|> json_response(400)
assert response["error"] == "invalid_filename_extension"
end
end
describe "Update media description" do
describe "Update media" do
setup do: oauth_access(["write:media"])
setup %{user: actor} do
@ -83,21 +113,69 @@ test "/api/v2/media", %{conn: conn, user: user, image: image} do
ActivityPub.upload(
file,
actor: User.ap_id(actor),
description: "test-m"
description: "test-m",
filename: "test_image.jpg"
)
[object: object]
end
test "/api/v1/media/:id good request", %{conn: conn, object: object} do
test "/api/v1/media/:id update description", %{conn: conn, object: object} do
new_description = "test-media"
media =
conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
|> put("/api/v1/media/#{object.id}", %{"description" => new_description})
|> json_response_and_validate_schema(:ok)
assert media["description"] == "test-media"
assert refresh_record(object).data["name"] == "test-media"
assert media["description"] == new_description
assert refresh_record(object).data["name"] == new_description
end
test "/api/v1/media/:id update filename", %{conn: conn, object: object} do
new_filename = "media.jpg"
media =
conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{"filename" => new_filename})
|> json_response_and_validate_schema(:ok)
assert media["pleroma"]["filename"] == new_filename
assert refresh_record(object).data["filename"] == new_filename
end
test "/api/v1/media/:id update description and filename", %{conn: conn, object: object} do
new_description = "test-media"
new_filename = "media.jpg"
media =
conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{
"description" => new_description,
"filename" => new_filename,
"foo" => "bar"
})
|> json_response_and_validate_schema(:ok)
assert media["description"] == new_description
assert media["pleroma"]["filename"] == new_filename
assert refresh_record(object).data["name"] == new_description
assert refresh_record(object).data["filename"] == new_filename
end
test "/api/v1/media/:id filename with a wrong extension", %{conn: conn, object: object} do
new_filename = "media.exe"
response =
conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{"filename" => new_filename})
|> json_response(400)
assert response["error"] == "invalid_filename_extension"
end
end

View File

@ -126,6 +126,38 @@ test "posting a status", %{conn: conn} do
)
end
test "posting a status with an attachment", %{user: user, conn: conn} do
clear_config([:instance, :attachment_links], true)
filename = "an_image.jpg"
custom_filename = "look at this.jpg"
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image.jpg"),
filename: filename
}
{:ok, upload} =
ActivityPub.upload(file,
actor: user.ap_id,
description: "test image",
filename: custom_filename
)
response =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/statuses", %{
"status" => "cofe",
"spoiler_text" => "2hu",
"media_ids" => [to_string(upload.id)]
})
|> json_response_and_validate_schema(200)
assert String.ends_with?(response["content"], "#{filename}\">#{custom_filename}</a>")
assert length(response["media_attachments"]) == 1
end
test "it fails to create a status if `expires_in` is less or equal than an hour", %{
conn: conn
} do
@ -166,22 +198,28 @@ test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do
end
test "posting an undefined status with an attachment", %{user: user, conn: conn} do
clear_config([:instance, :attachment_links], true)
filename = "an_image.jpg"
description = "test image"
file = %Plug.Upload{
content_type: "image/jpeg",
path: Path.absname("test/fixtures/image.jpg"),
filename: "an_image.jpg"
filename: filename
}
{:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
{:ok, upload} = ActivityPub.upload(file, actor: user.ap_id, description: description)
conn =
response =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/statuses", %{
"media_ids" => [to_string(upload.id)]
})
|> json_response_and_validate_schema(200)
assert json_response_and_validate_schema(conn, 200)
assert String.ends_with?(response["content"], "#{filename}\">#{description}</a>")
assert length(response["media_attachments"]) == 1
end
test "replying to a status", %{user: user, conn: conn} do
@ -413,7 +451,12 @@ test "creates a scheduled activity with a media attachment", %{user: user, conn:
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 +470,8 @@ test "creates a scheduled activity with a media attachment", %{user: user, conn:
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

@ -180,6 +180,16 @@ test "updates the user's skip_thread_containment option", %{user: user, conn: co
assert refresh_record(user).skip_thread_containment
end
test "updates the user's show_attachment_filenames option", %{user: user, conn: conn} do
response =
conn
|> patch("/api/v1/accounts/update_credentials", %{show_attachment_filenames: "true"})
|> json_response_and_validate_schema(200)
assert response["pleroma"]["show_attachment_filenames"]
assert refresh_record(user).show_attachment_filenames
end
test "updates the user's hide_follows status", %{conn: conn} do
conn = patch(conn, "/api/v1/accounts/update_credentials", %{hide_follows: "true"})

View File

@ -35,7 +35,8 @@ test "Represent a user account" do
"<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
inserted_at: ~N[2017-08-15 15:47:06.597036],
emoji: %{"karjalanpiirakka" => "/file.png"},
raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
show_attachment_filenames: true
})
expected = %{
@ -87,6 +88,7 @@ test "Represent a user account" do
hide_followers_count: false,
hide_follows_count: false,
relationship: %{},
show_attachment_filenames: true,
skip_thread_containment: false,
accepts_chat_messages: nil
}
@ -185,6 +187,7 @@ test "Represent a Service(bot) account" do
hide_followers_count: false,
hide_follows_count: false,
relationship: %{},
show_attachment_filenames: false,
skip_thread_containment: false,
accepts_chat_messages: nil
}

View File

@ -465,7 +465,8 @@ test "attachments" do
}
],
"blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn",
"uuid" => 6
"uuid" => 6,
"filename" => "an_image.png"
}
expected = %{
@ -476,7 +477,7 @@ test "attachments" 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"
}