[#2497] Configurability of :min_content_length (preview proxy). Refactoring, documentation, tests.

This commit is contained in:
Ivan Tashkinov 2020-09-17 17:13:40 +03:00
commit 7cdbd91d83
8 changed files with 329 additions and 63 deletions

View file

@ -58,6 +58,7 @@ defmodule Pleroma.Helpers.MediaHelper do
defp prepare_image_resize_args(_), do: {:error, :missing_options}
# Note: video thumbnail is intentionally not resized (always has original dimensions)
def video_framegrab(url) do
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
{:ok, env} <- HTTP.get(url, [], pool: :media),

View file

@ -12,8 +12,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
alias Pleroma.Web.MediaProxy
alias Plug.Conn
@min_content_length_for_preview 100 * 1024
def remote(conn, %{"sig" => sig64, "url" => url64}) do
with {_, true} <- {:enabled, MediaProxy.enabled?()},
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
@ -37,7 +35,8 @@ 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) do
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
:ok <- MediaProxy.verify_request_path_and_url(conn, url) do
handle_preview(conn, url)
else
{:enabled, false} ->
@ -59,8 +58,25 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
content_type = Tesla.get_header(head_response, "content-type")
content_length = Tesla.get_header(head_response, "content-length")
content_length = content_length && String.to_integer(content_length)
static = conn.params["static"] in ["true", true]
handle_preview(content_type, content_length, conn, media_proxy_url)
cond do
static and content_type == "image/gif" ->
handle_jpeg_preview(conn, media_proxy_url)
static ->
drop_static_param_and_redirect(conn)
content_type == "image/gif" ->
redirect(conn, 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)
true ->
handle_preview(content_type, conn, media_proxy_url)
end
else
# If HEAD failed, redirecting to media proxy URI doesn't make much sense; returning an error
{_, %{status: status}} ->
@ -74,58 +90,27 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
defp handle_preview(
"image/gif" = _content_type,
_content_length,
%{params: %{"static" => static}} = conn,
media_proxy_url
)
when static in ["true", true] do
handle_jpeg_preview(conn, media_proxy_url)
end
defp handle_preview(
_content_type,
_content_length,
%{params: %{"static" => static}} = conn,
_media_proxy_url
)
when static in ["true", true] do
uri_without_static_param = UriHelper.modify_uri_params(current_url(conn), %{}, ["static"])
redirect(conn, external: uri_without_static_param)
end
defp handle_preview("image/gif" = _content_type, _content_length, conn, media_proxy_url) do
redirect(conn, external: media_proxy_url)
end
defp handle_preview("image/" <> _ = _content_type, content_length, conn, media_proxy_url)
when is_integer(content_length) and content_length > 0 and
content_length < @min_content_length_for_preview do
redirect(conn, external: media_proxy_url)
end
defp handle_preview("image/png" <> _ = _content_type, _content_length, conn, media_proxy_url) do
defp handle_preview("image/png" <> _ = _content_type, conn, media_proxy_url) do
handle_png_preview(conn, media_proxy_url)
end
defp handle_preview("image/" <> _ = _content_type, _content_length, conn, media_proxy_url) do
defp handle_preview("image/" <> _ = _content_type, conn, media_proxy_url) do
handle_jpeg_preview(conn, media_proxy_url)
end
defp handle_preview("video/" <> _ = _content_type, _content_length, conn, media_proxy_url) do
defp handle_preview("video/" <> _ = _content_type, conn, media_proxy_url) do
handle_video_preview(conn, media_proxy_url)
end
defp handle_preview(_unsupported_content_type, _content_length, conn, media_proxy_url) do
defp handle_preview(_unsupported_content_type, conn, media_proxy_url) do
fallback_on_preview_error(conn, media_proxy_url)
end
defp handle_png_preview(conn, media_proxy_url) do
quality = Config.get!([:media_preview_proxy, :image_quality])
{thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(),
{:ok, thumbnail_binary} <-
with {:ok, thumbnail_binary} <-
MediaHelper.image_resize(
media_proxy_url,
%{
@ -146,9 +131,9 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
defp handle_jpeg_preview(conn, media_proxy_url) do
quality = Config.get!([:media_preview_proxy, :image_quality])
{thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(),
{:ok, thumbnail_binary} <-
with {:ok, thumbnail_binary} <-
MediaHelper.image_resize(
media_proxy_url,
%{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
@ -174,6 +159,15 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
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
redirect(conn, external: media_proxy_url)
end
@ -189,7 +183,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
defp thumbnail_max_dimensions do
config = Config.get([:media_preview_proxy], [])
config = media_preview_proxy_config()
thumbnail_max_width = Keyword.fetch!(config, :thumbnail_max_width)
thumbnail_max_height = Keyword.fetch!(config, :thumbnail_max_height)
@ -197,6 +191,14 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
{thumbnail_max_width, thumbnail_max_height}
end
defp min_content_length_for_preview do
Keyword.get(media_preview_proxy_config(), :min_content_length, 0)
end
defp media_preview_proxy_config do
Config.get!([:media_preview_proxy])
end
defp media_proxy_opts do
Config.get([:media_proxy, :proxy_opts], [])
end