fix(media_proxy): serve static previews instead of redirecting to the main host
Ported from Akkoma 4a810bb2e. Co-authored-by: Oneric <oneric@oneric.stub>
This commit is contained in:
parent
c3fb297b89
commit
58a6534a44
3 changed files with 10 additions and 17 deletions
1
changelog.d/media-proxy-static-redirect.fix
Normal file
1
changelog.d/media-proxy-static-redirect.fix
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Media proxy: serve a preview for static=true on non-GIF files instead of redirecting to the main endpoint host
|
||||||
|
|
@ -7,7 +7,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
||||||
|
|
||||||
alias Pleroma.Config
|
alias Pleroma.Config
|
||||||
alias Pleroma.Helpers.MediaHelper
|
alias Pleroma.Helpers.MediaHelper
|
||||||
alias Pleroma.Helpers.UriHelper
|
|
||||||
alias Pleroma.ReverseProxy
|
alias Pleroma.ReverseProxy
|
||||||
alias Pleroma.Web.MediaProxy
|
alias Pleroma.Web.MediaProxy
|
||||||
alias Plug.Conn
|
alias Plug.Conn
|
||||||
|
|
@ -67,9 +66,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
||||||
static and content_type == "image/gif" ->
|
static and content_type == "image/gif" ->
|
||||||
handle_jpeg_preview(conn, media_proxy_url)
|
handle_jpeg_preview(conn, media_proxy_url)
|
||||||
|
|
||||||
static ->
|
|
||||||
drop_static_param_and_redirect(conn)
|
|
||||||
|
|
||||||
content_type == "image/gif" ->
|
content_type == "image/gif" ->
|
||||||
conn
|
conn
|
||||||
|> put_status(301)
|
|> put_status(301)
|
||||||
|
|
@ -166,15 +162,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp drop_static_param_and_redirect(conn) do
|
|
||||||
uri_without_static_param =
|
|
||||||
conn
|
|
||||||
|> current_url()
|
|
||||||
|> UriHelper.modify_uri_params(%{}, ["static"])
|
|
||||||
|
|
||||||
redirect(conn, external: uri_without_static_param)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp fallback_on_preview_error(conn, media_proxy_url) do
|
defp fallback_on_preview_error(conn, media_proxy_url) do
|
||||||
redirect(conn, external: media_proxy_url)
|
redirect(conn, external: media_proxy_url)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -252,22 +252,27 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
|
||||||
assert redirected_to(response, 301) == 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, responds with a preview",
|
||||||
"redirects to media preview proxy URI without `static` param",
|
|
||||||
%{
|
%{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
url: url,
|
url: url,
|
||||||
media_proxy_url: media_proxy_url
|
media_proxy_url: media_proxy_url
|
||||||
} do
|
} do
|
||||||
|
assert_dependencies_installed()
|
||||||
|
|
||||||
Tesla.Mock.mock(fn
|
Tesla.Mock.mock(fn
|
||||||
%{method: :head, url: ^media_proxy_url} ->
|
%{method: :head, url: ^media_proxy_url} ->
|
||||||
%Tesla.Env{status: 200, body: "", headers: [{"content-type", "image/jpeg"}]}
|
%Tesla.Env{status: 200, body: "", headers: [{"content-type", "image/jpeg"}]}
|
||||||
|
|
||||||
|
%{method: :get, url: ^media_proxy_url} ->
|
||||||
|
%Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
response = get(conn, url <> "?static=true")
|
response = get(conn, url <> "?static=true")
|
||||||
|
|
||||||
assert response.status == 302
|
assert response.status == 200
|
||||||
assert redirected_to(response) == url
|
assert Conn.get_resp_header(response, "content-type") == ["image/jpeg"]
|
||||||
|
assert response.resp_body != ""
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with :min_content_length setting not matched by Content-Length header, " <>
|
test "with :min_content_length setting not matched by Content-Length header, " <>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue