use Pleroma.Web, :controller
alias Pleroma.Config
- alias Pleroma.Helpers.MogrifyHelper
+ alias Pleroma.Helpers.MediaHelper
alias Pleroma.ReverseProxy
alias Pleroma.Web.MediaProxy
def remote(conn, %{"sig" => sig64, "url" => url64}) do
with {_, true} <- {:enabled, MediaProxy.enabled?()},
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
+ {_, false} <- {:in_banned_urls, MediaProxy.in_banned_urls(url)},
:ok <- MediaProxy.verify_request_path_and_url(conn, url) do
proxy_opts = Config.get([:media_proxy, :proxy_opts], [])
ReverseProxy.call(conn, url, proxy_opts)
{:enabled, false} ->
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
+ {:in_banned_urls, true} ->
+ send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
+
{:error, :invalid_signature} ->
send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
end
end
- defp handle_preview("image/" <> _ = content_type, %{params: params} = conn, url) do
- with {:ok, %{status: status, body: body}} when status in 200..299 <-
- url
- |> MediaProxy.url()
- |> Tesla.get(opts: [adapter: [timeout: preview_timeout()]]),
- {:ok, path} <- MogrifyHelper.store_as_temporary_file(url, body),
- resize_dimensions <-
- Map.get(
- params,
- "limit_dimensions",
- Config.get([:media_preview_proxy, :limit_dimensions])
- ),
- %Mogrify.Image{} <- MogrifyHelper.in_place_resize_to_limit(path, resize_dimensions),
- {:ok, image_binary} <- File.read(path),
- _ <- File.rm(path) do
- conn
- |> put_resp_header("content-type", content_type)
- |> send_resp(200, image_binary)
- else
- {_, %{status: _}} ->
- send_resp(conn, :failed_dependency, "Can't fetch the image.")
+ defp thumbnail_max_dimensions(params) do
+ config = Config.get([:media_preview_proxy], [])
- {:error, :recv_response_timeout} ->
- send_resp(conn, :failed_dependency, "Downstream timeout.")
+ thumbnail_max_width =
+ if w = params["thumbnail_max_width"] do
+ String.to_integer(w)
+ else
+ Keyword.fetch!(config, :thumbnail_max_width)
+ end
+ thumbnail_max_height =
+ if h = params["thumbnail_max_height"] do
+ String.to_integer(h)
+ else
+ Keyword.fetch!(config, :thumbnail_max_height)
+ end
+
+ {thumbnail_max_width, thumbnail_max_height}
+ end
+
+ defp handle_preview("image/" <> _ = _content_type, %{params: params} = conn, url) do
+ with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
+ media_proxy_url <- MediaProxy.url(url),
+ {:ok, thumbnail_binary} <-
+ MediaHelper.ffmpeg_resize_remote(
+ media_proxy_url,
+ %{max_width: thumbnail_max_width, max_height: thumbnail_max_height}
+ ) do
+ conn
+ |> put_resp_header("content-type", "image/jpeg")
+ |> send_resp(200, thumbnail_binary)
+ else
_ ->
send_resp(conn, :failed_dependency, "Can't handle image preview.")
end