mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2025-02-02 04:12:22 +00:00
MediaProxyController: Use 301 for permanent redirects
This commit is contained in:
parent
138ead9856
commit
2226171890
3 changed files with 38 additions and 6 deletions
1
changelog.d/301-small-image-redirect.change
Normal file
1
changelog.d/301-small-image-redirect.change
Normal 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.
|
|
@ -71,11 +71,15 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
||||||
drop_static_param_and_redirect(conn)
|
drop_static_param_and_redirect(conn)
|
||||||
|
|
||||||
content_type == "image/gif" ->
|
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
|
min_content_length_for_preview() > 0 and content_length > 0 and
|
||||||
content_length < min_content_length_for_preview() ->
|
content_length < min_content_length_for_preview() ->
|
||||||
redirect(conn, external: media_proxy_url)
|
conn
|
||||||
|
|> put_status(301)
|
||||||
|
|> redirect(external: media_proxy_url)
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
handle_preview(content_type, conn, media_proxy_url)
|
handle_preview(content_type, conn, media_proxy_url)
|
||||||
|
|
|
@ -248,8 +248,8 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
|
||||||
|
|
||||||
response = get(conn, url)
|
response = get(conn, url)
|
||||||
|
|
||||||
assert response.status == 302
|
assert response.status == 301
|
||||||
assert redirected_to(response) == media_proxy_url
|
assert redirected_to(response, 301) == media_proxy_url
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with `static` param and non-GIF image preview requested, " <>
|
test "with `static` param and non-GIF image preview requested, " <>
|
||||||
|
@ -290,8 +290,8 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
|
||||||
|
|
||||||
response = get(conn, url)
|
response = get(conn, url)
|
||||||
|
|
||||||
assert response.status == 302
|
assert response.status == 301
|
||||||
assert redirected_to(response) == media_proxy_url
|
assert redirected_to(response, 301) == media_proxy_url
|
||||||
end
|
end
|
||||||
|
|
||||||
test "thumbnails PNG images into PNG", %{
|
test "thumbnails PNG images into PNG", %{
|
||||||
|
@ -356,5 +356,32 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
|
||||||
assert response.status == 302
|
assert response.status == 302
|
||||||
assert redirected_to(response) == media_proxy_url
|
assert redirected_to(response) == media_proxy_url
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue