X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fmedia_proxy%2Fcontroller.ex;h=9327e7253aa949243d63dd4a1de91c7465238552;hb=78516a8daa7094506e0247e8bdaddfde77a4ba67;hp=dc122fc3a4d5d31da3191c67961f7ad23a747291;hpb=1cb5cbdc6c1cd065e90961a9d538cb72610ae481;p=akkoma diff --git a/lib/pleroma/web/media_proxy/controller.ex b/lib/pleroma/web/media_proxy/controller.ex index dc122fc3a..9327e7253 100644 --- a/lib/pleroma/web/media_proxy/controller.ex +++ b/lib/pleroma/web/media_proxy/controller.ex @@ -2,6 +2,10 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do use Pleroma.Web, :controller require Logger + @httpoison Application.get_env(:pleroma, :httpoison) + + @max_body_length 25 * 1048576 + @cache_control %{ default: "public, max-age=1209600", error: "public, must-revalidate, max-age=160", @@ -12,7 +16,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do with \ true <- Keyword.get(config, :enabled, false), {:ok, url} <- Pleroma.Web.MediaProxy.decode_url(sig, url), - url = URI.encode(url), {:ok, content_type, body} <- proxy_request(url) do conn @@ -28,12 +31,15 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do defp proxy_request(link) do headers = [{"user-agent", "Pleroma/MediaProxy; #{Pleroma.Web.base_url()} <#{Application.get_env(:pleroma, :instance)[:email]}>"}] - options = [:insecure, {:follow_redirect, true}] - case :hackney.request(:get, link, headers, "", options) do - {:ok, 200, headers, client} -> - headers = Enum.into(headers, Map.new) - {:ok, body} = :hackney.body(client) - {:ok, headers["Content-Type"], body} + options = @httpoison.process_request_options([:insecure, {:follow_redirect, true}]) + with \ + {:ok, 200, headers, client} <- :hackney.request(:get, link, headers, "", options), + headers = Enum.into(headers, Map.new), + {:ok, body} <- proxy_request_body(client), + content_type <- proxy_request_content_type(headers, body) + do + {:ok, content_type, body} + else {:ok, status, _, _} -> Logger.warn "MediaProxy: request failed, status #{status}, link: #{link}" {:error, {:http, :bad_status, link}} @@ -56,4 +62,23 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do |> send_resp(code, body) end + defp proxy_request_body(client), do: proxy_request_body(client, <<>>) + defp proxy_request_body(client, body) when byte_size(body) < @max_body_length do + case :hackney.stream_body(client) do + {:ok, data} -> proxy_request_body(client, <>) + :done -> {:ok, body} + {:error, reason} -> {:error, reason} + end + end + defp proxy_request_body(client, _) do + :hackney.close(client) + {:error, :body_too_large} + end + + # TODO: the body is passed here as well because some hosts do not provide a content-type. + # At some point we may want to use magic numbers to discover the content-type and reply a proper one. + defp proxy_request_content_type(headers, _body) do + headers["Content-Type"] || headers["content-type"] || "image/jpeg" + end + end