2017-11-22 19:06:07 +01:00
|
|
|
defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
|
|
|
use Pleroma.Web, :controller
|
2018-11-23 17:40:45 +01:00
|
|
|
alias Pleroma.{Web.MediaProxy, ReverseProxy}
|
2017-11-22 19:06:07 +01:00
|
|
|
|
2018-11-30 17:44:42 +01:00
|
|
|
@default_proxy_opts [max_body_length: 25 * 1_048_576]
|
|
|
|
|
2018-11-23 17:40:45 +01:00
|
|
|
def remote(conn, params = %{"sig" => sig64, "url" => url64}) do
|
|
|
|
with config <- Pleroma.Config.get([:media_proxy]),
|
|
|
|
true <- Keyword.get(config, :enabled, false),
|
|
|
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
2018-11-13 23:41:33 +01:00
|
|
|
filename <- Path.basename(URI.parse(url).path),
|
2018-11-23 17:40:45 +01:00
|
|
|
:ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do
|
2018-11-30 17:44:42 +01:00
|
|
|
ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_length))
|
2017-11-28 21:44:25 +01:00
|
|
|
else
|
2018-03-30 15:01:53 +02:00
|
|
|
false ->
|
2018-11-23 17:40:45 +01:00
|
|
|
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
2018-11-23 17:40:45 +01:00
|
|
|
send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-11-23 17:40:45 +01:00
|
|
|
{:wrong_filename, filename} ->
|
|
|
|
redirect(conn, external: MediaProxy.build_url(sig64, url64, filename))
|
2017-11-22 19:06:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 17:40:45 +01:00
|
|
|
def filename_matches(has_filename, path, url) do
|
|
|
|
filename = MediaProxy.filename(url)
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-11-23 17:40:45 +01:00
|
|
|
cond do
|
|
|
|
has_filename && filename && Path.basename(path) != filename -> {:wrong_filename, filename}
|
|
|
|
true -> :ok
|
2017-12-11 02:31:37 +01:00
|
|
|
end
|
|
|
|
end
|
2017-11-22 19:06:07 +01:00
|
|
|
end
|