Merge remote-tracking branch 'upstream/develop' into pr-upstream-http-proxy
[akkoma] / lib / pleroma / web / media_proxy / controller.ex
index d6a1866bffd622b5b31cc1aa72a6b0adea8569e7..9327e7253aa949243d63dd4a1de91c7465238552 100644 (file)
@@ -2,6 +2,8 @@ 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 %{
@@ -14,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
@@ -30,13 +31,14 @@ 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}]
+    options = @httpoison.process_request_options([:insecure, {:follow_redirect, true}])
     with \
       {:ok, 200, headers, client} <- :hackney.request(:get, link, headers, "", options),
-      {:ok, body} <- proxy_request_body(client)
+      headers = Enum.into(headers, Map.new),
+      {:ok, body} <- proxy_request_body(client),
+      content_type <- proxy_request_content_type(headers, body)
     do
-      headers = Enum.into(headers, Map.new)
-      {:ok, headers["Content-Type"], body}
+      {:ok, content_type, body}
     else
       {:ok, status, _, _} ->
         Logger.warn "MediaProxy: request failed, status #{status}, link: #{link}"
@@ -73,5 +75,10 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
     {: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