2017-11-22 11:06:07 -07:00
|
|
|
defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
require Logger
|
|
|
|
|
2017-12-31 07:25:00 -07:00
|
|
|
@httpoison Application.get_env(:pleroma, :httpoison)
|
2018-03-19 10:42:09 -06:00
|
|
|
|
2018-03-30 07:01:53 -06:00
|
|
|
@max_body_length 25 * 1_048_576
|
2017-12-10 18:31:37 -07:00
|
|
|
|
2017-11-28 13:44:25 -07:00
|
|
|
@cache_control %{
|
|
|
|
default: "public, max-age=1209600",
|
2018-03-30 07:01:53 -06:00
|
|
|
error: "public, must-revalidate, max-age=160"
|
2017-11-28 13:44:25 -07:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:06:07 -07:00
|
|
|
def remote(conn, %{"sig" => sig, "url" => url}) do
|
2017-11-28 13:44:25 -07:00
|
|
|
config = Application.get_env(:pleroma, :media_proxy, [])
|
2018-03-30 07:01:53 -06:00
|
|
|
|
|
|
|
with true <- Keyword.get(config, :enabled, false),
|
|
|
|
{:ok, url} <- Pleroma.Web.MediaProxy.decode_url(sig, url),
|
|
|
|
{:ok, content_type, body} <- proxy_request(url) do
|
2017-11-28 13:44:25 -07:00
|
|
|
conn
|
|
|
|
|> put_resp_content_type(content_type)
|
|
|
|
|> set_cache_header(:default)
|
|
|
|
|> send_resp(200, body)
|
|
|
|
else
|
2018-03-30 07:01:53 -06:00
|
|
|
false ->
|
|
|
|
send_error(conn, 404)
|
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
|
|
|
send_error(conn, 403)
|
|
|
|
|
|
|
|
{:error, {:http, _, url}} ->
|
|
|
|
redirect_or_error(conn, url, Keyword.get(config, :redirect_on_failure, true))
|
2017-11-22 11:06:07 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp proxy_request(link) do
|
2018-03-30 07:01:53 -06:00
|
|
|
headers = [
|
|
|
|
{"user-agent",
|
|
|
|
"Pleroma/MediaProxy; #{Pleroma.Web.base_url()} <#{
|
|
|
|
Application.get_env(:pleroma, :instance)[:email]
|
|
|
|
}>"}
|
|
|
|
]
|
|
|
|
|
|
|
|
options =
|
|
|
|
@httpoison.process_request_options([:insecure, {:follow_redirect, true}]) ++
|
|
|
|
[{:pool, :default}]
|
|
|
|
|
|
|
|
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
|
2017-12-12 03:45:55 -07:00
|
|
|
{:ok, content_type, body}
|
2017-12-10 18:31:37 -07:00
|
|
|
else
|
2017-11-22 11:06:07 -07:00
|
|
|
{:ok, status, _, _} ->
|
2018-03-30 07:01:53 -06:00
|
|
|
Logger.warn("MediaProxy: request failed, status #{status}, link: #{link}")
|
2017-11-28 13:44:25 -07:00
|
|
|
{:error, {:http, :bad_status, link}}
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-11-22 11:06:07 -07:00
|
|
|
{:error, error} ->
|
2018-03-30 07:01:53 -06:00
|
|
|
Logger.warn("MediaProxy: request failed, error #{inspect(error)}, link: #{link}")
|
2017-11-28 13:44:25 -07:00
|
|
|
{:error, {:http, error, link}}
|
2017-11-22 11:06:07 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-28 13:44:25 -07:00
|
|
|
defp set_cache_header(conn, key) do
|
|
|
|
Plug.Conn.put_resp_header(conn, "cache-control", @cache_control[key])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp redirect_or_error(conn, url, true), do: redirect(conn, external: url)
|
|
|
|
defp redirect_or_error(conn, url, _), do: send_error(conn, 502, "Media proxy error: " <> url)
|
2017-11-22 11:06:07 -07:00
|
|
|
|
2017-11-28 13:44:25 -07:00
|
|
|
defp send_error(conn, code, body \\ "") do
|
|
|
|
conn
|
|
|
|
|> set_cache_header(:error)
|
|
|
|
|> send_resp(code, body)
|
|
|
|
end
|
2017-11-22 11:06:07 -07:00
|
|
|
|
2017-12-10 18:31:37 -07:00
|
|
|
defp proxy_request_body(client), do: proxy_request_body(client, <<>>)
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-12-10 18:31:37 -07:00
|
|
|
defp proxy_request_body(client, body) when byte_size(body) < @max_body_length do
|
|
|
|
case :hackney.stream_body(client) do
|
2018-03-30 07:01:53 -06:00
|
|
|
{:ok, data} -> proxy_request_body(client, <<body::binary, data::binary>>)
|
2017-12-10 18:31:37 -07:00
|
|
|
:done -> {:ok, body}
|
|
|
|
{:error, reason} -> {:error, reason}
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-12-10 18:31:37 -07:00
|
|
|
defp proxy_request_body(client, _) do
|
|
|
|
:hackney.close(client)
|
|
|
|
{:error, :body_too_large}
|
|
|
|
end
|
|
|
|
|
2017-12-12 03:45:55 -07:00
|
|
|
# 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
|
2017-11-22 11:06:07 -07:00
|
|
|
end
|