Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
31 KB
Referenced Files
None
Subscribers
None
diff --git a/Dockerfile b/Dockerfile
index 1067ef1c8..8170f5b9d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,7 +13,9 @@ COPY . .
ENV MIX_ENV=prod
ENV VIX_COMPILATION_MODE=PLATFORM_PROVIDED_LIBVIPS
-RUN apk add git gcc g++ musl-dev make cmake file-dev vips-dev &&\
+# vips-heif: libvips' HEIF/AVIF module, also at build time (Vix makes its
+# functions from what libvips offers then; AVIF previews need heifsave)
+RUN apk add git gcc g++ musl-dev make cmake file-dev vips-dev vips-heif &&\
echo "import Config" > config/prod.secret.exs &&\
mix local.hex --force &&\
mix local.rebar --force &&\
@@ -42,7 +44,7 @@ ARG HOME=/opt/pleroma
ARG DATA=/var/lib/pleroma
RUN apk update &&\
- apk add ffmpeg vips libmagic ncurses postgresql-client &&\
+ apk add ffmpeg vips vips-heif libmagic ncurses postgresql-client &&\
adduser --system --shell /bin/false --home ${HOME} pleroma &&\
mkdir -p ${DATA}/uploads &&\
mkdir -p ${DATA}/static &&\
diff --git a/changelog.d/media-preview-avif-accept.fix b/changelog.d/media-preview-avif-accept.fix
new file mode 100644
index 000000000..33f5acb1e
--- /dev/null
+++ b/changelog.d/media-preview-avif-accept.fix
@@ -0,0 +1 @@
+Media preview proxy: honor the AVIF Accept quality value; `image/avif;q=0` no longer selects AVIF.
diff --git a/changelog.d/media-preview-image-sizes.add b/changelog.d/media-preview-image-sizes.add
new file mode 100644
index 000000000..fe04ee504
--- /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`, optionally as AVIF (`avif`); the Docker image adds `vips-heif` for it.
diff --git a/config/config.exs b/config/config.exs
index 6d400427c..29b50deb1 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -507,7 +507,11 @@ config :pleroma, :media_preview_proxy,
thumbnail_max_width: 600,
thumbnail_max_height: 600,
image_quality: 85,
- min_content_length: 100 * 1024
+ min_content_length: 100 * 1024,
+ image_sizes: [960, 1600, 2560],
+ avif: false,
+ avif_quality: 55,
+ avif_effort: 3
config :pleroma, :shout,
enabled: true,
diff --git a/config/description.exs b/config/description.exs
index 28edb13f8..56011becf 100644
--- a/config/description.exs
+++ b/config/description.exs
@@ -1663,6 +1663,32 @@ config :pleroma, :config_description, [
type: :integer,
description:
"Min content length (in bytes) to perform preview. Media smaller in size will be served without thumbnailing."
+ },
+ %{
+ key: :image_sizes,
+ type: {:list, :integer},
+ description:
+ "Sizes (the longer side, in pixels) of image previews besides the thumbnail, offered to clients so they can show an image fitting the screen instead of the original. Only these sizes are served. An empty list turns them off.",
+ suggestions: [[960, 1600, 2560]]
+ },
+ %{
+ key: :avif,
+ type: :boolean,
+ description:
+ "Serve the image sizes as AVIF to clients that accept it (about 40% smaller than JPEG at the same quality, but several times slower to encode: put a cache in front). Needs a libvips with libheif and an AV1 encoder (the Docker image has them); without one, the sizes stay JPEG/PNG."
+ },
+ %{
+ key: :avif_quality,
+ type: :integer,
+ description: "Quality of AVIF previews, 0 to 100.",
+ suggestions: [55]
+ },
+ %{
+ key: :avif_effort,
+ type: :integer,
+ description:
+ "CPU effort of the AVIF encoder, 0 (fastest) to 9 (smallest files). Each step up costs much more time for a little less size.",
+ suggestions: [3]
}
]
},
diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md
index 00857c5cb..0fe244ed6 100644
--- a/docs/configuration/cheatsheet.md
+++ b/docs/configuration/cheatsheet.md
@@ -392,6 +392,10 @@ This section describe PWA manifest instance-specific values. Currently this opti
* `thumbnail_max_height`: Max height of preview thumbnail for images (video preview always has original dimensions).
* `image_quality`: Quality of the output. Ranges from 0 (min quality) to 100 (max quality).
* `min_content_length`: Min content length to perform preview, in bytes. If greater than 0, media smaller in size will be served as is, without thumbnailing.
+* `image_sizes`: Sizes of image previews besides the thumbnail, as the longer side in pixels (default: `[960, 1600, 2560]`). The API lists them on each image attachment (`pleroma.image_sizes`, only the sizes smaller than the image), so that clients can show an image fitting the screen instead of the original. A preview of such a size is `/proxy/preview/…?size=1600`; other sizes redirect to the thumbnail. An empty list turns them off. Every preview is made on request, so put a cache in front of `/proxy` (see the nginx example config) to resize each image only once per size.
+* `avif`: Serve the `image_sizes` as AVIF to clients whose `Accept` header includes `image/avif` (default: `false`). AVIF is about 40% smaller than JPEG at the same quality, but encoding takes several times longer, so a cache in front of `/proxy` matters even more. The responses carry `Vary: Accept`: a cache or CDN in front must honour it, or it would give one format to everyone. Needs a libvips with libheif and an AV1 encoder (on Alpine: `vips-heif`, also when compiling Vix); without one, the sizes stay JPEG or PNG. Thumbnails stay JPEG or PNG.
+* `avif_quality`: Quality of the AVIF previews, 0 to 100 (default: `55`).
+* `avif_effort`: CPU effort of the AVIF encoder, 0 (fastest) to 9 (smallest files) (default: `3`).
### Purge cache strategy
diff --git a/lib/pleroma/helpers/media_helper.ex b/lib/pleroma/helpers/media_helper.ex
index 8566ab3ea..8b4b48b73 100644
--- a/lib/pleroma/helpers/media_helper.ex
+++ b/lib/pleroma/helpers/media_helper.ex
@@ -31,16 +31,39 @@ defmodule Pleroma.Helpers.MediaHelper do
height: options.max_height,
size: :VIPS_SIZE_DOWN
) do
- if options[:format] == "png" do
- Operation.pngsave_buffer(resized, Q: options[:quality])
- else
- Operation.jpegsave_buffer(resized, Q: options[:quality], interlace: true)
+ case options[:format] do
+ "png" -> Operation.pngsave_buffer(resized, Q: options[:quality])
+ "avif" -> avif_save(resized, options)
+ _ -> Operation.jpegsave_buffer(resized, Q: options[:quality], interlace: true)
end
else
{:error, _} = error -> error
end
end
+ @doc """
+ Whether this libvips can encode AVIF. Vix makes its operations from the
+ libvips it is compiled against; the bundled one has no HEIF encoder, a
+ platform libvips with libheif and an AV1 encoder (libaom) has.
+ """
+ @spec avif_supported?() :: boolean()
+ def avif_supported? do
+ Code.ensure_loaded?(Operation) and function_exported?(Operation, :heifsave_buffer, 2)
+ end
+
+ # called through apply/3: without libheif the function does not exist
+ defp avif_save(image, options) do
+ # credo:disable-for-next-line Credo.Check.Refactor.Apply
+ apply(Operation, :heifsave_buffer, [
+ image,
+ [
+ Q: options[:quality],
+ effort: options[:effort],
+ compression: :VIPS_FOREIGN_HEIF_COMPRESSION_AV1
+ ]
+ ])
+ end
+
# Note: video thumbnail is intentionally not resized (always has original dimensions)
@spec video_framegrab(String.t()) :: {:ok, binary()} | {:error, any()}
def video_framegrab(url) do
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 5fc46ef69..e8134a86b 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -660,6 +660,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,
@@ -938,4 +939,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/lib/pleroma/web/media_proxy.ex b/lib/pleroma/web/media_proxy.ex
index a318ab10f..ad33c9847 100644
--- a/lib/pleroma/web/media_proxy.ex
+++ b/lib/pleroma/web/media_proxy.ex
@@ -68,6 +68,27 @@ defmodule Pleroma.Web.MediaProxy do
end
end
+ @doc "The sizes (long side, in pixels) image previews can have besides the thumbnail."
+ @spec image_sizes() :: [pos_integer()]
+ def image_sizes do
+ # from the admin config they can come as strings
+ [:media_preview_proxy, :image_sizes]
+ |> Config.get([])
+ |> List.wrap()
+ |> Enum.flat_map(fn
+ size when is_integer(size) and size > 0 -> [size]
+ size when is_binary(size) -> image_size_from_string(size)
+ _ -> []
+ end)
+ end
+
+ defp image_size_from_string(size) do
+ case Integer.parse(size) do
+ {size, ""} when size > 0 -> [size]
+ _ -> []
+ end
+ end
+
def enabled?, do: Config.get([:media_proxy, :enabled], false)
# Note: media proxy must be enabled for media preview proxy in order to load all
diff --git a/lib/pleroma/web/media_proxy/media_proxy_controller.ex b/lib/pleroma/web/media_proxy/media_proxy_controller.ex
index 04e8ffd52..25b718df6 100644
--- a/lib/pleroma/web/media_proxy/media_proxy_controller.ex
+++ b/lib/pleroma/web/media_proxy/media_proxy_controller.ex
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
alias Pleroma.Config
alias Pleroma.Helpers.MediaHelper
+ alias Pleroma.Helpers.UriHelper
alias Pleroma.ReverseProxy
alias Pleroma.Web.MediaProxy
alias Plug.Conn
@@ -37,8 +38,11 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
def preview(%Conn{} = conn, %{"sig" => sig64, "url" => url64}) do
with {_, true} <- {:enabled, MediaProxy.preview_enabled?()},
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
- :ok <- MediaProxy.verify_request_path_and_url(conn, url) do
- handle_preview(conn, url)
+ :ok <- MediaProxy.verify_request_path_and_url(conn, url),
+ {:ok, dimensions} <- preview_dimensions(conn.params["size"]) do
+ conn
+ |> choose_avif(conn.params["size"])
+ |> handle_preview(url, dimensions)
else
{:enabled, false} ->
send_resp(conn, 404, Conn.Status.reason_phrase(404))
@@ -48,10 +52,61 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
{:wrong_filename, filename} ->
redirect(conn, external: MediaProxy.build_preview_url(sig64, url64, filename))
+
+ # only the configured sizes: any other value would be one more resize
+ # and one more cache entry
+ :unknown_size ->
+ drop_param_and_redirect(conn, "size")
end
end
- defp handle_preview(conn, url) do
+ # the box an image preview fits in: the thumbnail's, or a configured size
+ # (the long side)
+ defp preview_dimensions(nil), do: {:ok, thumbnail_max_dimensions()}
+
+ # exactly as configured: "+960" or "0960" would be the same size under
+ # another cache key
+ defp preview_dimensions(size) when is_binary(size) do
+ case Enum.find(MediaProxy.image_sizes(), &(to_string(&1) == size)) do
+ nil -> :unknown_size
+ size -> {:ok, {size, size}}
+ end
+ end
+
+ defp preview_dimensions(_size), do: :unknown_size
+
+ # A size (not the thumbnail) as AVIF, when it is on, this libvips can
+ # encode it and the client takes it. The answer then depends on Accept:
+ # caches must keep the formats apart.
+ defp choose_avif(conn, nil), do: conn
+
+ defp choose_avif(conn, _size) do
+ if Keyword.get(media_preview_proxy_config(), :avif, false) and
+ MediaHelper.avif_supported?() do
+ accepts_avif? =
+ conn
+ |> get_req_header("accept")
+ |> Enum.flat_map(&Plug.Conn.Utils.list/1)
+ |> Enum.any?(&accepts_avif?/1)
+
+ conn
+ |> put_resp_header("vary", "accept")
+ |> assign(:preview_avif, accepts_avif?)
+ else
+ conn
+ end
+ end
+
+ defp accepts_avif?(media_range) do
+ with {:ok, "image", "avif", params} <- Plug.Conn.Utils.media_type(media_range),
+ {quality, ""} <- Float.parse(Map.get(params, "q", "1")) do
+ quality > 0 and quality <= 1
+ else
+ _ -> false
+ end
+ end
+
+ defp handle_preview(conn, url, dimensions) do
media_proxy_url = MediaProxy.url(url)
http_client_opts = Pleroma.Config.get([:media_proxy, :proxy_opts, :http], pool: :media)
@@ -64,7 +119,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
cond do
static and content_type == "image/gif" ->
- handle_jpeg_preview(conn, media_proxy_url)
+ handle_jpeg_preview(conn, media_proxy_url, dimensions)
content_type == "image/gif" ->
conn
@@ -78,7 +133,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|> redirect(external: media_proxy_url)
true ->
- handle_preview(content_type, conn, media_proxy_url)
+ handle_preview(content_type, conn, media_proxy_url, dimensions)
end
else
# If HEAD failed, redirecting to media proxy URI doesn't make much sense; returning an error
@@ -93,25 +148,48 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
- defp handle_preview("image/png" <> _ = _content_type, conn, media_proxy_url) do
- handle_png_preview(conn, media_proxy_url)
+ defp handle_preview(
+ "image/" <> _ = content_type,
+ %{assigns: %{preview_avif: true}} = conn,
+ media_proxy_url,
+ dimensions
+ ) do
+ case avif_preview(media_proxy_url, dimensions) do
+ {:ok, binary} ->
+ conn
+ |> put_preview_response_headers(["image/avif", "preview.avif"])
+ |> send_resp(200, binary)
+
+ # an encoder that fails (a libheif without AV1): JPEG/PNG, not the
+ # original
+ _ ->
+ handle_preview(
+ content_type,
+ assign(conn, :preview_avif, false),
+ media_proxy_url,
+ dimensions
+ )
+ end
+ end
+
+ defp handle_preview("image/png" <> _ = _content_type, conn, media_proxy_url, dimensions) do
+ handle_png_preview(conn, media_proxy_url, dimensions)
end
- defp handle_preview("image/" <> _ = _content_type, conn, media_proxy_url) do
- handle_jpeg_preview(conn, media_proxy_url)
+ defp handle_preview("image/" <> _ = _content_type, conn, media_proxy_url, dimensions) do
+ handle_jpeg_preview(conn, media_proxy_url, dimensions)
end
- defp handle_preview("video/" <> _ = _content_type, conn, media_proxy_url) do
+ defp handle_preview("video/" <> _ = _content_type, conn, media_proxy_url, _dimensions) do
handle_video_preview(conn, media_proxy_url)
end
- defp handle_preview(_unsupported_content_type, conn, media_proxy_url) do
+ defp handle_preview(_unsupported_content_type, conn, media_proxy_url, _dimensions) do
fallback_on_preview_error(conn, media_proxy_url)
end
- defp handle_png_preview(conn, media_proxy_url) do
+ defp handle_png_preview(conn, media_proxy_url, {thumbnail_max_width, thumbnail_max_height}) do
quality = Config.get!([:media_preview_proxy, :image_quality])
- {thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
with {:ok, thumbnail_binary} <-
MediaHelper.image_resize(
@@ -132,9 +210,20 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
- defp handle_jpeg_preview(conn, media_proxy_url) do
+ defp avif_preview(media_proxy_url, {max_width, max_height}) do
+ config = media_preview_proxy_config()
+
+ MediaHelper.image_resize(media_proxy_url, %{
+ max_width: max_width,
+ max_height: max_height,
+ quality: Keyword.get(config, :avif_quality, 55),
+ effort: Keyword.get(config, :avif_effort, 3),
+ format: "avif"
+ })
+ end
+
+ defp handle_jpeg_preview(conn, media_proxy_url, {thumbnail_max_width, thumbnail_max_height}) do
quality = Config.get!([:media_preview_proxy, :image_quality])
- {thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
with {:ok, thumbnail_binary} <-
MediaHelper.image_resize(
@@ -162,6 +251,15 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
+ defp drop_param_and_redirect(conn, param) do
+ uri_without_param =
+ conn
+ |> current_url()
+ |> UriHelper.modify_uri_params(%{}, [param])
+
+ redirect(conn, external: uri_without_param)
+ end
+
defp fallback_on_preview_error(conn, media_proxy_url) do
redirect(conn, external: media_proxy_url)
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 9bf3d9b93..fea2c3fc1 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
@@ -786,6 +787,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(insert(:user), id, resolve: true)
diff --git a/test/pleroma/web/media_proxy/media_proxy_controller_test.exs b/test/pleroma/web/media_proxy/media_proxy_controller_test.exs
index 71ad19c09..596d5909d 100644
--- a/test/pleroma/web/media_proxy/media_proxy_controller_test.exs
+++ b/test/pleroma/web/media_proxy/media_proxy_controller_test.exs
@@ -343,6 +343,221 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
assert response.resp_body != ""
end
+ defp long_side(binary) do
+ {:ok, image} = Vix.Vips.Image.new_from_buffer(binary)
+ max(Vix.Vips.Image.width(image), Vix.Vips.Image.height(image))
+ end
+
+ defp mock_image(media_proxy_url, content_type, fixture) do
+ Tesla.Mock.mock(fn
+ %{method: :head, url: ^media_proxy_url} ->
+ %Tesla.Env{status: 200, body: "", headers: [{"content-type", content_type}]}
+
+ %{method: :get, url: ^media_proxy_url} ->
+ %Tesla.Env{status: 200, body: File.read!(fixture)}
+ end)
+ end
+
+ test "without `size`, the preview fits the thumbnail dimensions", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ assert_dependencies_installed()
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response = get(conn, url)
+
+ assert response.status == 200
+ assert long_side(response.resp_body) == 600
+ end
+
+ test "with a configured `size`, the preview fits that size", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ assert_dependencies_installed()
+ clear_config([:media_preview_proxy, :image_sizes], [300, 800])
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response = get(conn, url <> "?size=800")
+
+ assert response.status == 200
+ assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"]
+ assert long_side(response.resp_body) == 800
+ end
+
+ test "with a configured `size`, a PNG stays a PNG", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ assert_dependencies_installed()
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ mock_image(media_proxy_url, "image/png", "test/fixtures/image.png")
+
+ response = get(conn, url <> "?size=300")
+
+ assert response.status == 200
+ assert Conn.get_resp_header(response, "content-type") == ["image/png"]
+ assert long_side(response.resp_body) == 300
+ end
+
+ @avif_skip if(Pleroma.Helpers.MediaHelper.avif_supported?(),
+ do: false,
+ else: "this libvips cannot encode AVIF"
+ )
+
+ @tag skip: @avif_skip
+ test "with AVIF on and accepted, a size comes as AVIF", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ clear_config([:media_preview_proxy, :avif], true)
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response =
+ conn
+ |> put_req_header("accept", "IMAGE/AVIF; q=0.5,image/webp,*/*")
+ |> get(url <> "?size=300")
+
+ assert response.status == 200
+ assert Conn.get_resp_header(response, "content-type") == ["image/avif"]
+ assert Conn.get_resp_header(response, "vary") == ["accept"]
+ assert long_side(response.resp_body) == 300
+ end
+
+ @tag skip: @avif_skip
+ test "with AVIF on and accepted, a PNG size comes as AVIF too", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ clear_config([:media_preview_proxy, :avif], true)
+ mock_image(media_proxy_url, "image/png", "test/fixtures/image.png")
+
+ response =
+ conn
+ |> put_req_header("accept", "image/avif,*/*")
+ |> get(url <> "?size=300")
+
+ assert Conn.get_resp_header(response, "content-type") == ["image/avif"]
+ assert long_side(response.resp_body) == 300
+ end
+
+ @tag skip: @avif_skip
+ test "with AVIF on but not accepted, a size stays JPEG and varies on Accept", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ clear_config([:media_preview_proxy, :avif], true)
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response =
+ conn
+ |> put_req_header("accept", "image/webp,*/*")
+ |> get(url <> "?size=300")
+
+ assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"]
+ assert Conn.get_resp_header(response, "vary") == ["accept"]
+ end
+
+ @tag skip: @avif_skip
+ test "AVIF requires an explicit media range with positive quality", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ clear_config([:media_preview_proxy, :avif], true)
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ for accept <- [
+ "image/avif;q=0,image/jpeg,*/*",
+ "image/avif; q=0.000, image/*;q=1",
+ "image/avif;q=invalid",
+ "image/avif;q=0.5invalid",
+ "image/avif;q=1.1",
+ "image/avif-sequence,*/*",
+ "image/jpeg; note=\"image/avif\""
+ ] do
+ response = conn |> put_req_header("accept", accept) |> get(url <> "?size=300")
+
+ assert response.status == 200
+ assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"], accept
+ assert Conn.get_resp_header(response, "vary") == ["accept"]
+ end
+ end
+
+ test "with AVIF off, a size stays JPEG and does not vary", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+ clear_config([:media_preview_proxy, :avif], false)
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response =
+ conn
+ |> put_req_header("accept", "image/avif,*/*")
+ |> get(url <> "?size=300")
+
+ assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"]
+ assert Conn.get_resp_header(response, "vary") == []
+ end
+
+ test "a thumbnail is never AVIF", %{
+ conn: conn,
+ url: url,
+ media_proxy_url: media_proxy_url
+ } do
+ clear_config([:media_preview_proxy, :avif], true)
+ mock_image(media_proxy_url, "image/jpeg", "test/fixtures/image.jpg")
+
+ response =
+ conn
+ |> put_req_header("accept", "image/avif,*/*")
+ |> get(url)
+
+ assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"]
+ end
+
+ test "accepts a `size` only as written in the config", %{conn: conn, url: url} do
+ clear_config([:media_preview_proxy, :image_sizes], [800])
+
+ for size <- ["+800", "0800", "800.0", " 800", "800abc"] do
+ response = get(conn, url <> "?size=" <> URI.encode_www_form(size))
+ refute redirected_to(response, 302) =~ "size="
+ end
+ end
+
+ test "redirects a `size` that is not a string", %{conn: conn, url: url} do
+ clear_config([:media_preview_proxy, :image_sizes], [800])
+
+ response = get(conn, url <> "?size[]=800")
+
+ assert response.status == 302
+ end
+
+ test "with a `size` that is not configured, redirects to the preview without it", %{
+ conn: conn,
+ url: url
+ } do
+ clear_config([:media_preview_proxy, :image_sizes], [300])
+
+ response = get(conn, url <> "?size=301")
+
+ assert redirected_to(response, 302) =~ url
+ refute redirected_to(response, 302) =~ "size="
+ end
+
test "redirects to media proxy URI in case of thumbnailing error", %{
conn: conn,
url: url,
diff --git a/test/pleroma/web/media_proxy_test.exs b/test/pleroma/web/media_proxy_test.exs
index 433a0fe95..b6db290bb 100644
--- a/test/pleroma/web/media_proxy_test.exs
+++ b/test/pleroma/web/media_proxy_test.exs
@@ -278,4 +278,16 @@ defmodule Pleroma.Web.MediaProxyTest do
assert String.starts_with?(encoded, media_url)
end
end
+
+ describe "image_sizes/0" do
+ test "gives the configured sizes as integers, also when stored as strings" do
+ clear_config([:media_preview_proxy, :image_sizes], [960, "1600", "x", 0, -5])
+ assert MediaProxy.image_sizes() == [960, 1600]
+ end
+
+ test "gives none when unset" do
+ clear_config([:media_preview_proxy, :image_sizes], nil)
+ assert MediaProxy.image_sizes() == []
+ end
+ end
end

File Metadata

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

Event Timeline