Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None
diff --git a/config/config.exs b/config/config.exs
index e2c110689..f4f6e9764 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -500,7 +500,8 @@ 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]
config :pleroma, :shout,
enabled: true,
diff --git a/config/description.exs b/config/description.exs
index f435628aa..0902c0873 100644
--- a/config/description.exs
+++ b/config/description.exs
@@ -1663,6 +1663,13 @@ 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]]
}
]
},
diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md
index 0275d2be0..b54a5fa85 100644
--- a/docs/configuration/cheatsheet.md
+++ b/docs/configuration/cheatsheet.md
@@ -389,6 +389,7 @@ 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.
### Purge cache strategy
diff --git a/lib/pleroma/web/media_proxy.ex b/lib/pleroma/web/media_proxy.ex
index f9376b508..4b9c21ddb 100644
--- a/lib/pleroma/web/media_proxy.ex
+++ b/lib/pleroma/web/media_proxy.ex
@@ -64,6 +64,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 a0aafc32e..c2a75e4bf 100644
--- a/lib/pleroma/web/media_proxy/media_proxy_controller.ex
+++ b/lib/pleroma/web/media_proxy/media_proxy_controller.ex
@@ -38,8 +38,9 @@ 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
+ handle_preview(conn, url, dimensions)
else
{:enabled, false} ->
send_resp(conn, 404, Conn.Status.reason_phrase(404))
@@ -49,10 +50,30 @@ 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
+
+ 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)
@@ -65,10 +86,10 @@ 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)
static ->
- drop_static_param_and_redirect(conn)
+ drop_param_and_redirect(conn, "static")
content_type == "image/gif" ->
conn
@@ -82,7 +103,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
@@ -97,25 +118,24 @@ 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/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(
@@ -136,9 +156,8 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
- defp handle_jpeg_preview(conn, media_proxy_url) do
+ 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(
@@ -166,13 +185,13 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
- defp drop_static_param_and_redirect(conn) do
- uri_without_static_param =
+ defp drop_param_and_redirect(conn, param) do
+ uri_without_param =
conn
|> current_url()
- |> UriHelper.modify_uri_params(%{}, ["static"])
+ |> UriHelper.modify_uri_params(%{}, [param])
- redirect(conn, external: uri_without_static_param)
+ redirect(conn, external: uri_without_param)
end
defp fallback_on_preview_error(conn, media_proxy_url) do
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 f7e52483c..d9bcb8088 100644
--- a/test/pleroma/web/media_proxy/media_proxy_controller_test.exs
+++ b/test/pleroma/web/media_proxy/media_proxy_controller_test.exs
@@ -338,6 +338,96 @@ 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
+
+ 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 86d30c04c..03d9e2a75 100644
--- a/test/pleroma/web/media_proxy_test.exs
+++ b/test/pleroma/web/media_proxy_test.exs
@@ -252,4 +252,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, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839839
Default Alt Text
(13 KB)

Event Timeline