+ defp handle_preview("image/" <> _, %{params: params} = conn, url) do
+ with {:ok, %{status: status, body: body}} when status in 200..299 <- Tesla.get(url),
+ {: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) do
+ send_file(conn, 200, path)
+ else
+ {_, %{status: _}} ->
+ send_resp(conn, :failed_dependency, "Can't fetch the image.")
+
+ _ ->
+ send_resp(conn, :failed_dependency, "Can't handle image preview.")
+ end
+ end
+
+ defp handle_preview(content_type, conn, _url) do
+ send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
+ end
+
+ defp acceptable_body_length?(head_response) do
+ max_body_length = Config.get([:media_preview_proxy, :max_body_length], nil)
+ content_length = Tesla.get_header(head_response, "content-length")
+ content_length = with {int, _} <- Integer.parse(content_length), do: int