Ingest remote attachment width/height

This commit is contained in:
Alex Gleason 2021-05-12 15:38:49 -05:00
parent ab9eabdf20
commit 4c060ae733
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 50 additions and 5 deletions

View File

@ -21,6 +21,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
field(:type, :string) field(:type, :string)
field(:href, ObjectValidators.Uri) field(:href, ObjectValidators.Uri)
field(:mediaType, :string, default: "application/octet-stream") field(:mediaType, :string, default: "application/octet-stream")
field(:width, :integer)
field(:height, :integer)
end end
end end
@ -52,7 +54,7 @@ def url_changeset(struct, data) do
data = fix_media_type(data) data = fix_media_type(data)
struct struct
|> cast(data, [:type, :href, :mediaType]) |> cast(data, [:type, :href, :mediaType, :width, :height])
|> validate_inclusion(:type, ["Link"]) |> validate_inclusion(:type, ["Link"])
|> validate_required([:type, :href, :mediaType]) |> validate_required([:type, :href, :mediaType])
end end

View File

@ -245,6 +245,8 @@ def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachm
"type" => Map.get(url || %{}, "type", "Link") "type" => Map.get(url || %{}, "type", "Link")
} }
|> Maps.put_if_present("mediaType", media_type) |> Maps.put_if_present("mediaType", media_type)
|> Maps.put_if_present("width", (url || %{})["width"])
|> Maps.put_if_present("height", (url || %{})["height"])
%{ %{
"url" => [attachment_url], "url" => [attachment_url],

View File

@ -72,5 +72,38 @@ test "it handles our own uploads" do
assert attachment.mediaType == "image/jpeg" assert attachment.mediaType == "image/jpeg"
end 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
end end
end end

View File

@ -76,7 +76,9 @@ test "Funkwhale Audio object" do
"href" => "href" =>
"https://channels.tests.funkwhale.audio/api/v1/listen/3901e5d8-0445-49d5-9711-e096cf32e515/?upload=42342395-0208-4fee-a38d-259a6dae0871&download=false", "https://channels.tests.funkwhale.audio/api/v1/listen/3901e5d8-0445-49d5-9711-e096cf32e515/?upload=42342395-0208-4fee-a38d-259a6dae0871&download=false",
"mediaType" => "audio/ogg", "mediaType" => "audio/ogg",
"type" => "Link" "type" => "Link",
"width" => nil,
"height" => nil
} }
] ]
} }

View File

@ -60,7 +60,9 @@ test "it remaps video URLs as attachments if necessary" do
"href" => "href" =>
"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
"mediaType" => "video/mp4", "mediaType" => "video/mp4",
"type" => "Link" "type" => "Link",
"width" => nil,
"height" => nil
} }
] ]
} }
@ -83,7 +85,9 @@ test "it remaps video URLs as attachments if necessary" do
"href" => "href" =>
"https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4", "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4",
"mediaType" => "video/mp4", "mediaType" => "video/mp4",
"type" => "Link" "type" => "Link",
"width" => nil,
"height" => nil
} }
] ]
} }
@ -113,7 +117,9 @@ test "it works for peertube videos with only their mpegURL map" do
"href" => "href" =>
"https://peertube.stream/static/streaming-playlists/hls/abece3c3-b9c6-47f4-8040-f3eed8c602e6/abece3c3-b9c6-47f4-8040-f3eed8c602e6-1080-fragmented.mp4", "https://peertube.stream/static/streaming-playlists/hls/abece3c3-b9c6-47f4-8040-f3eed8c602e6/abece3c3-b9c6-47f4-8040-f3eed8c602e6-1080-fragmented.mp4",
"mediaType" => "video/mp4", "mediaType" => "video/mp4",
"type" => "Link" "type" => "Link",
"width" => nil,
"height" => nil
} }
] ]
} }