Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/changelog.d/media-preview-image-sizes.add b/changelog.d/media-preview-image-sizes.add
new file mode 100644
index 000000000..9da276833
--- /dev/null
+++ b/changelog.d/media-preview-image-sizes.add
@@ -0,0 +1 @@
+Media preview proxy: image previews in configured sizes besides the thumbnail (`image_sizes`), listed on image attachments as `pleroma.image_sizes`.
diff --git a/lib/pleroma/web/api_spec/schemas/attachment.ex b/lib/pleroma/web/api_spec/schemas/attachment.ex
index 4104ed25c..6904ed19c 100644
--- a/lib/pleroma/web/api_spec/schemas/attachment.ex
+++ b/lib/pleroma/web/api_spec/schemas/attachment.ex
@@ -54,6 +54,32 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Attachment do
name: %Schema{
type: :string,
description: "Name of the attachment, typically the filename"
+ },
+ image_sizes: %Schema{
+ type: :array,
+ description:
+ "Previews of an image in the sizes the instance offers, for a client to pick from (as in srcset); only sizes smaller than the image. Absent when there are none (no media preview proxy, not an image, a GIF).",
+ items: %Schema{
+ type: :object,
+ required: [:size, :url],
+ properties: %{
+ size: %Schema{
+ type: :integer,
+ description: "The longer side of the preview's box, in pixels"
+ },
+ width: %Schema{
+ type: :integer,
+ description:
+ "Width of the preview (approximate), when the image's dimensions are known"
+ },
+ height: %Schema{
+ type: :integer,
+ description:
+ "Height of the preview (approximate), when the image's dimensions are known"
+ },
+ url: %Schema{type: :string, format: :uri, description: "The preview's URL"}
+ }
+ }
}
}
}
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index 4625be47c..3e2dd460e 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -657,6 +657,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
pleroma =
%{mime_type: media_type}
|> Maps.put_if_present(:name, name)
+ |> Maps.put_if_present(:image_sizes, image_sizes(href_remote, media_type, attachment_url))
%{
id: attachment_id,
@@ -892,4 +893,53 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
_ -> nil
end
end
+
+ # An image as previews of the configured sizes (the long side), for a
+ # client to pick from (srcset). Only the sizes smaller than the image,
+ # when its dimensions are known; nil when there are none. Not for GIFs
+ # and APNGs: the previews are still images.
+ defp image_sizes(href, "image/" <> _ = media_type, url)
+ when media_type not in ["image/gif", "image/apng"] do
+ if MediaProxy.preview_enabled?() do
+ dimensions = known_dimensions(url["width"], url["height"])
+
+ MediaProxy.image_sizes()
+ |> Enum.sort()
+ |> Enum.flat_map(fn size ->
+ case scaled_dimensions(dimensions, size) do
+ :larger ->
+ []
+
+ scaled ->
+ [Map.merge(%{size: size, url: MediaProxy.preview_url(href, size: size)}, scaled)]
+ end
+ end)
+ |> case do
+ [] -> nil
+ sizes -> sizes
+ end
+ end
+ end
+
+ defp image_sizes(_href, _media_type, _url), do: nil
+
+ defp known_dimensions(width, height)
+ when is_integer(width) and is_integer(height) and width > 0 and height > 0,
+ do: {width, height}
+
+ defp known_dimensions(_width, _height), do: nil
+
+ # a preview's dimensions in a box of `size`; :larger when the image is not
+ # larger than the box (no preview needed)
+ defp scaled_dimensions(nil, _size), do: %{}
+
+ defp scaled_dimensions({width, height}, size) do
+ long_side = max(width, height)
+
+ if size < long_side do
+ %{width: round(width * size / long_side), height: round(height * size / long_side)}
+ else
+ :larger
+ end
+ end
end
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 f7b815ed7..4a024e950 100644
--- a/test/pleroma/web/mastodon_api/views/status_view_test.exs
+++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs
@@ -17,6 +17,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.StatusView
+ alias Pleroma.Web.MediaProxy
alias Pleroma.Web.RichMedia.Card
require Bitwise
@@ -670,6 +671,73 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
end
end
+ describe "attachment image sizes" do
+ setup do
+ clear_config([:media_proxy, :enabled], true)
+ clear_config([:media_preview_proxy, :enabled], true)
+ clear_config([:media_preview_proxy, :image_sizes], [960, 1600, 2560])
+
+ ConfigMock
+ |> stub_with(Pleroma.Test.StaticConfig)
+
+ :ok
+ end
+
+ defp image(media_type, dimensions \\ %{}) do
+ %{
+ "type" => "Image",
+ "url" => [
+ Map.merge(
+ %{"mediaType" => media_type, "href" => "https://remote.example/a.jpg"},
+ dimensions
+ )
+ ]
+ }
+ end
+
+ defp sizes(object) do
+ StatusView.render("attachment.json", %{attachment: object})[:pleroma][:image_sizes]
+ end
+
+ test "lists the sizes smaller than the image, with their dimensions" do
+ object = image("image/jpeg", %{"width" => 2000, "height" => 1000})
+ href = "https://remote.example/a.jpg"
+
+ assert sizes(object) == [
+ %{
+ size: 960,
+ width: 960,
+ height: 480,
+ url: MediaProxy.preview_url(href, size: 960)
+ },
+ %{
+ size: 1600,
+ width: 1600,
+ height: 800,
+ url: MediaProxy.preview_url(href, size: 1600)
+ }
+ ]
+
+ result = StatusView.render("attachment.json", %{attachment: object})
+ assert_schema(result, "Attachment", Pleroma.Web.ApiSpec.spec())
+ end
+
+ test "lists every size when the image's dimensions are unknown" do
+ assert [%{size: 960}, %{size: 1600}, %{size: 2560}] = sizes(image("image/png"))
+ refute Map.has_key?(hd(sizes(image("image/png"))), :width)
+ end
+
+ test "lists none for GIFs and APNGs, videos, small images, or without the preview proxy" do
+ assert sizes(image("image/gif")) == nil
+ assert sizes(image("image/apng")) == nil
+ assert sizes(image("video/mp4")) == nil
+ assert sizes(image("image/jpeg", %{"width" => 900, "height" => 600})) == nil
+
+ clear_config([:media_preview_proxy, :enabled], false)
+ assert sizes(image("image/jpeg")) == nil
+ end
+ end
+
test "put the url advertised in the Activity in to the url attribute" do
id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
[activity] = Activity.search(nil, id)

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 17, 10:19 PM (17 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839838
Default Alt Text
(7 KB)

Event Timeline