Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex b/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex
index 2fb252cf2..dcddfdb74 100644
--- a/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex
@@ -1,154 +1,152 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
use Ecto.Schema
alias Pleroma.EctoType.ActivityPub.ObjectValidators
import Ecto.Changeset
@primary_key false
embedded_schema do
field(:id, :string)
field(:type, :string, default: "Link")
field(:mediaType, ObjectValidators.MIME, default: "application/octet-stream")
field(:name, :string)
field(:summary, :string)
field(:blurhash, :string)
embeds_many :url, UrlObjectValidator, primary_key: false do
field(:type, :string, default: "Link")
field(:href, ObjectValidators.Uri)
field(:mediaType, ObjectValidators.MIME, default: "application/octet-stream")
field(:width, :integer)
field(:height, :integer)
end
end
def cast_and_validate(data) do
data
|> cast_data()
|> validate_data()
end
def cast_data(data) do
%__MODULE__{}
|> changeset(data)
end
def changeset(struct, data) do
data =
data
|> fix_media_type()
|> fix_url()
|> fix_url_media_types()
struct
|> cast(data, [:id, :type, :mediaType, :name, :summary, :blurhash])
|> cast_embed(:url, with: &url_changeset/2, required: true)
|> validate_inclusion(:type, ~w[Link Document Audio Image Video])
|> validate_required([:type, :mediaType])
end
def url_changeset(struct, data) do
data = fix_media_type(data)
struct
|> cast(data, [:type, :href, :mediaType, :width, :height])
|> validate_inclusion(:type, ["Link"])
|> validate_required([:type, :href, :mediaType])
end
@unknown_media_types [nil, "", "application/octet-stream"]
@guessable_media_types ~w[image video audio]
def fix_media_type(data) do
- case data["mediaType"] || data["mimeType"] do
- media_type when media_type in @unknown_media_types ->
- Map.put(data, "mediaType", guess_media_type(data) || "application/octet-stream")
+ media_type =
+ Enum.find([data["mediaType"], data["mimeType"]], &(&1 not in @unknown_media_types)) ||
+ guess_media_type(data) || "application/octet-stream"
- media_type ->
- Map.put_new(data, "mediaType", media_type)
- end
+ Map.put(data, "mediaType", media_type)
end
# Some servers (e.g. Threads) send no media type. Guess one from the
# URL extension or the object type, but only image, video or audio:
# the media type only decides how clients display the attachment.
defp guess_media_type(data) do
media_type_from_extension(media_href(data)) || media_type_from_object_type(data["type"])
end
defp media_href(%{"url" => url}) when is_binary(url), do: url
defp media_href(%{"url" => [%{"href" => href} | _]}) when is_binary(href), do: href
defp media_href(%{"href" => href}) when is_binary(href), do: href
defp media_href(_), do: nil
defp media_type_from_extension(href) when is_binary(href) do
with %URI{path: path} when is_binary(path) <- URI.parse(href),
"." <> extension <- Path.extname(path),
media_type <- MIME.type(String.downcase(extension)),
[kind, _] when kind in @guessable_media_types <- String.split(media_type, "/", parts: 2) do
media_type
else
_ -> nil
end
end
defp media_type_from_extension(_), do: nil
defp media_type_from_object_type("Image"), do: "image/*"
defp media_type_from_object_type("Video"), do: "video/*"
defp media_type_from_object_type("Audio"), do: "audio/*"
defp media_type_from_object_type(_), do: nil
# Url links without a known media type of their own take the attachment's.
defp fix_url_media_types(%{"url" => urls, "mediaType" => media_type} = data)
when is_list(urls) do
Map.put(data, "url", Enum.map(urls, &fix_url_media_type(&1, media_type)))
end
defp fix_url_media_types(data), do: data
defp fix_url_media_type(%{} = url, media_type) do
case fix_media_type(url) do
%{"mediaType" => "application/octet-stream"} = url -> Map.put(url, "mediaType", media_type)
url -> url
end
end
defp fix_url_media_type(url, _media_type), do: url
defp handle_href(href, mediaType, data) do
[
%{
"href" => href,
"type" => "Link",
"mediaType" => mediaType,
"width" => data["width"],
"height" => data["height"]
}
]
end
defp fix_url(data) do
cond do
is_binary(data["url"]) ->
Map.put(data, "url", handle_href(data["url"], data["mediaType"], data))
is_binary(data["href"]) and data["url"] == nil ->
Map.put(data, "url", handle_href(data["href"], data["mediaType"], data))
true ->
data
end
end
defp validate_data(cng) do
cng
|> validate_inclusion(:type, ~w[Document Audio Image Video])
|> validate_required([:mediaType, :type])
end
end
diff --git a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs
index aee10149e..47db99087 100644
--- a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs
+++ b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs
@@ -1,305 +1,322 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
use Pleroma.DataCase, async: true
alias Pleroma.UnstubbedConfigMock, as: ConfigMock
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
alias Pleroma.Web.ActivityPub.Transmogrifier
import Mox
import Pleroma.Factory
describe "attachments" do
test "works with apng" do
attachment =
%{
"mediaType" => "image/apng",
"name" => "",
"type" => "Document",
"url" =>
"https://media.misskeyusercontent.com/io/2859c26e-cd43-4550-848b-b6243bc3fe28.apng"
}
assert {:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "image/apng"
end
test "fails without url" do
attachment = %{
"mediaType" => "",
"name" => "",
"summary" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document"
}
assert {:error, _cng} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
end
test "works with honkerific attachments" do
honk = %{
"mediaType" => "",
"summary" => "Select your spirit chonk",
"name" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document",
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
}
assert {:ok, attachment} =
honk
|> AttachmentValidator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "image/jpeg"
assert attachment.summary == "Select your spirit chonk"
assert attachment.name == "298p3RG7j27tfsZ9RQ.jpg"
end
test "works with an unknown but valid mime type" do
attachment = %{
"mediaType" => "x-custom/x-type",
"type" => "Document",
"url" => "https://example.org"
}
assert {:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "x-custom/x-type"
end
test "works with invalid mime types" do
attachment = %{
"mediaType" => "x-customx-type",
"type" => "Document",
"url" => "https://example.org"
}
assert {:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "application/octet-stream"
attachment = %{
"mediaType" => "https://example.org",
"type" => "Document",
"url" => "https://example.org"
}
assert {:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "application/octet-stream"
end
test "it turns mastodon attachments into our attachments" do
attachment = %{
"url" =>
"http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
"type" => "Document",
"name" => nil,
"mediaType" => "image/jpeg",
"blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert [
%{
href:
"http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
type: "Link",
mediaType: "image/jpeg"
}
] = attachment.url
assert attachment.mediaType == "image/jpeg"
assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
end
test "it guesses a missing media type from the url extension (Threads)" do
attachment = %{
"type" => "Image",
"url" => "https://cdn.example.com/v/t51/900924821_n.webp?stp=dst-jpg_e35&oh=00_AQ",
"width" => 2000,
"height" => 1500
}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "image/webp"
assert [%{mediaType: "image/webp", width: 2000, height: 1500}] = attachment.url
end
test "it guesses a missing media type of a url link object" do
attachment = %{
"type" => "Document",
"url" => [%{"type" => "Link", "href" => "https://example.com/clip.mp4"}]
}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert [%{mediaType: "video/mp4"}] = attachment.url
end
test "it falls back to the object type for a missing media type" do
for {type, expected} <- [{"Image", "image/*"}, {"Video", "video/*"}, {"Audio", "audio/*"}] do
attachment = %{"type" => type, "url" => "https://example.com/media/12345"}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == expected
assert [%{mediaType: ^expected}] = attachment.url
end
end
test "it uses the guessed media type for url links of incoming objects" do
for {url, expected} <- [
{"https://cdn.example.com/a_n.webp?stp=1", "image/webp"},
{"https://example.com/media/12345", "image/*"}
] do
%{"attachment" => [attachment]} =
Transmogrifier.fix_attachments(%{"attachment" => [%{"type" => "Image", "url" => url}]})
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == expected
assert [%{mediaType: ^expected}] = attachment.url
end
end
test "it guesses only image, video or audio media types" do
for url <- ["https://example.com/page.html", "https://example.com/doc.pdf"] do
attachment = %{"type" => "Document", "url" => url}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "application/octet-stream"
end
end
+ test "it uses mimeType when mediaType is empty or unknown" do
+ for media_type <- ["", "application/octet-stream"] do
+ attachment = %{
+ "type" => "Document",
+ "mediaType" => media_type,
+ "mimeType" => "image/png",
+ "url" => "https://example.com/image.jpg"
+ }
+
+ {:ok, attachment} =
+ AttachmentValidator.cast_and_validate(attachment)
+ |> Ecto.Changeset.apply_action(:insert)
+
+ assert attachment.mediaType == "image/png"
+ end
+ end
+
test "it keeps a media type sent by the remote" do
attachment = %{
"type" => "Image",
"mediaType" => "image/png",
"url" => "https://example.com/image.jpg"
}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "image/png"
end
test "it handles our own uploads" do
user = insert(:user)
file = %Plug.Upload{
content_type: "image/jpeg",
path: Path.absname("test/fixtures/image.jpg"),
filename: "an_image.jpg"
}
ConfigMock
|> stub_with(Pleroma.Test.StaticConfig)
{:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
{:ok, attachment} =
attachment.data
|> AttachmentValidator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "image/jpeg"
end
test "it handles image dimensions" do
attachment = %{
"url" => [
%{
"type" => "Link",
"mediaType" => "image/jpeg",
"href" => "https://example.com/images/1.jpg",
"width" => 200,
"height" => 100
}
],
"type" => "Document",
"name" => nil,
"mediaType" => "image/jpeg"
}
{:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
assert [
%{
href: "https://example.com/images/1.jpg",
type: "Link",
mediaType: "image/jpeg",
width: 200,
height: 100
}
] = attachment.url
assert attachment.mediaType == "image/jpeg"
end
test "it transforms image dimensions to our internal format" do
attachment = %{
"type" => "Document",
"name" => "Hello world",
"url" => "https://media.example.tld/1.jpg",
"width" => 880,
"height" => 960,
"mediaType" => "image/jpeg",
"blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
}
expected = %AttachmentValidator{
type: "Document",
name: "Hello world",
mediaType: "image/jpeg",
blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
url: [
%AttachmentValidator.UrlObjectValidator{
type: "Link",
mediaType: "image/jpeg",
href: "https://media.example.tld/1.jpg",
width: 880,
height: 960
}
]
}
{:ok, ^expected} =
AttachmentValidator.cast_and_validate(attachment)
|> Ecto.Changeset.apply_action(:insert)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Wed, Nov 18, 4:04 AM (23 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1840079
Default Alt Text
(15 KB)

Event Timeline