Merge branch 'proxy-redirect' into 'develop'

MediaProxyController: Use 301 for permanent redirects

See merge request pleroma/pleroma!4313
This commit is contained in:
Haelwenn 2025-01-21 09:31:17 +00:00
commit 4461cc984d
3 changed files with 38 additions and 6 deletions

View file

@ -0,0 +1 @@
Performance: Use 301 (permanent) redirect instead of 302 (temporary) when redirecting small images in media proxy. This allows browsers to cache the redirect response.

View file

@ -71,11 +71,15 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
drop_static_param_and_redirect(conn)
content_type == "image/gif" ->
redirect(conn, external: media_proxy_url)
conn
|> put_status(301)
|> redirect(external: media_proxy_url)
min_content_length_for_preview() > 0 and content_length > 0 and
content_length < min_content_length_for_preview() ->
redirect(conn, external: media_proxy_url)
conn
|> put_status(301)
|> redirect(external: media_proxy_url)
true ->
handle_preview(content_type, conn, media_proxy_url)

View file

@ -248,8 +248,8 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
response = get(conn, url)
assert response.status == 302
assert redirected_to(response) == media_proxy_url
assert response.status == 301
assert redirected_to(response, 301) == media_proxy_url
end
test "with `static` param and non-GIF image preview requested, " <>
@ -290,8 +290,8 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
response = get(conn, url)
assert response.status == 302
assert redirected_to(response) == media_proxy_url
assert response.status == 301
assert redirected_to(response, 301) == media_proxy_url
end
test "thumbnails PNG images into PNG", %{
@ -356,5 +356,32 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
assert response.status == 302
assert redirected_to(response) == media_proxy_url
end
test "redirects to media proxy URI with 301 when image is too small for preview", %{
conn: conn,
url: url,
media_proxy_url: media_proxy_url
} do
clear_config([:media_preview_proxy],
enabled: true,
min_content_length: 1000,
image_quality: 85,
thumbnail_max_width: 100,
thumbnail_max_height: 100
)
Tesla.Mock.mock(fn
%{method: :head, url: ^media_proxy_url} ->
%Tesla.Env{
status: 200,
body: "",
headers: [{"content-type", "image/png"}, {"content-length", "500"}]
}
end)
response = get(conn, url)
assert response.status == 301
assert redirected_to(response, 301) == media_proxy_url
end
end
end