2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-11-22 19:06:07 +01:00
|
|
|
defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
|
|
|
use Pleroma.Web, :controller
|
2020-04-21 15:29:19 +02:00
|
|
|
|
2020-05-08 22:06:47 +02:00
|
|
|
alias Pleroma.Config
|
2020-05-20 19:26:43 +02:00
|
|
|
alias Pleroma.Helpers.MediaHelper
|
2020-09-16 21:30:42 +02:00
|
|
|
alias Pleroma.Helpers.UriHelper
|
2019-02-03 18:44:18 +01:00
|
|
|
alias Pleroma.ReverseProxy
|
|
|
|
alias Pleroma.Web.MediaProxy
|
2020-09-10 20:28:07 +02:00
|
|
|
alias Plug.Conn
|
2017-11-22 19:06:07 +01:00
|
|
|
|
2020-05-11 22:21:53 +02:00
|
|
|
def remote(conn, %{"sig" => sig64, "url" => url64}) do
|
|
|
|
with {_, true} <- {:enabled, MediaProxy.enabled?()},
|
2018-11-23 17:40:45 +01:00
|
|
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
2020-07-05 18:02:43 +02:00
|
|
|
{_, false} <- {:in_banned_urls, MediaProxy.in_banned_urls(url)},
|
2020-05-11 22:21:53 +02:00
|
|
|
:ok <- MediaProxy.verify_request_path_and_url(conn, url) do
|
2020-08-19 20:36:26 +02:00
|
|
|
ReverseProxy.call(conn, url, media_proxy_opts())
|
2017-11-28 21:44:25 +01:00
|
|
|
else
|
2020-05-08 22:06:47 +02:00
|
|
|
{:enabled, false} ->
|
2020-09-10 20:28:07 +02:00
|
|
|
send_resp(conn, 404, Conn.Status.reason_phrase(404))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2020-07-02 15:36:54 +02:00
|
|
|
{:in_banned_urls, true} ->
|
2020-09-10 20:28:07 +02:00
|
|
|
send_resp(conn, 404, Conn.Status.reason_phrase(404))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
2020-09-10 20:28:07 +02:00
|
|
|
send_resp(conn, 403, 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
|
|
|
|
|
2020-09-10 20:28:07 +02:00
|
|
|
def preview(%Conn{} = conn, %{"sig" => sig64, "url" => url64}) do
|
2020-05-11 22:21:53 +02:00
|
|
|
with {_, true} <- {:enabled, MediaProxy.preview_enabled?()},
|
2020-09-17 16:13:40 +02:00
|
|
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
|
|
|
:ok <- MediaProxy.verify_request_path_and_url(conn, url) do
|
2020-05-08 22:06:47 +02:00
|
|
|
handle_preview(conn, url)
|
|
|
|
else
|
|
|
|
{:enabled, false} ->
|
2020-09-10 20:28:07 +02:00
|
|
|
send_resp(conn, 404, Conn.Status.reason_phrase(404))
|
2020-05-08 22:06:47 +02:00
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
2020-09-10 20:28:07 +02:00
|
|
|
send_resp(conn, 403, Conn.Status.reason_phrase(403))
|
2020-05-08 22:06:47 +02:00
|
|
|
|
|
|
|
{:wrong_filename, filename} ->
|
|
|
|
redirect(conn, external: MediaProxy.build_preview_url(sig64, url64, filename))
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2020-05-08 22:06:47 +02:00
|
|
|
defp handle_preview(conn, url) do
|
2020-09-05 15:16:35 +02:00
|
|
|
media_proxy_url = MediaProxy.url(url)
|
|
|
|
|
2020-05-11 22:21:53 +02:00
|
|
|
with {:ok, %{status: status} = head_response} when status in 200..299 <-
|
2020-09-09 18:30:42 +02:00
|
|
|
Pleroma.HTTP.request("head", media_proxy_url, [], [], pool: :media) do
|
2020-05-08 22:06:47 +02:00
|
|
|
content_type = Tesla.get_header(head_response, "content-type")
|
2020-09-12 10:20:41 +02:00
|
|
|
content_length = Tesla.get_header(head_response, "content-length")
|
|
|
|
content_length = content_length && String.to_integer(content_length)
|
2020-09-17 16:13:40 +02:00
|
|
|
static = conn.params["static"] in ["true", true]
|
|
|
|
|
|
|
|
cond do
|
|
|
|
static and content_type == "image/gif" ->
|
|
|
|
handle_jpeg_preview(conn, media_proxy_url)
|
2020-09-12 10:20:41 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
static ->
|
|
|
|
drop_static_param_and_redirect(conn)
|
|
|
|
|
|
|
|
content_type == "image/gif" ->
|
|
|
|
redirect(conn, external: media_proxy_url)
|
|
|
|
|
|
|
|
min_content_length_for_preview() > 0 and content_length > 0 and
|
|
|
|
content_length < min_content_length_for_preview() ->
|
|
|
|
redirect(conn, external: media_proxy_url)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
handle_preview(content_type, conn, media_proxy_url)
|
|
|
|
end
|
2018-12-10 07:39:57 +01:00
|
|
|
else
|
2020-09-12 10:20:41 +02:00
|
|
|
# If HEAD failed, redirecting to media proxy URI doesn't make much sense; returning an error
|
2020-05-08 22:06:47 +02:00
|
|
|
{_, %{status: status}} ->
|
|
|
|
send_resp(conn, :failed_dependency, "Can't fetch HTTP headers (HTTP #{status}).")
|
|
|
|
|
2020-05-11 22:21:53 +02:00
|
|
|
{:error, :recv_response_timeout} ->
|
|
|
|
send_resp(conn, :failed_dependency, "HEAD request timeout.")
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
send_resp(conn, :failed_dependency, "Can't fetch HTTP headers.")
|
2017-12-11 02:31:37 +01:00
|
|
|
end
|
|
|
|
end
|
2019-07-12 18:34:30 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp handle_preview("image/png" <> _ = _content_type, conn, media_proxy_url) do
|
2020-09-05 15:16:35 +02:00
|
|
|
handle_png_preview(conn, media_proxy_url)
|
2020-08-30 16:17:24 +02:00
|
|
|
end
|
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp handle_preview("image/" <> _ = _content_type, conn, media_proxy_url) do
|
2020-09-05 15:16:35 +02:00
|
|
|
handle_jpeg_preview(conn, media_proxy_url)
|
2020-08-18 17:23:27 +02:00
|
|
|
end
|
2020-05-14 19:18:31 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp handle_preview("video/" <> _ = _content_type, conn, media_proxy_url) do
|
2020-09-05 15:16:35 +02:00
|
|
|
handle_video_preview(conn, media_proxy_url)
|
2020-08-26 00:31:55 +02:00
|
|
|
end
|
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp handle_preview(_unsupported_content_type, conn, media_proxy_url) do
|
2020-09-12 09:33:42 +02:00
|
|
|
fallback_on_preview_error(conn, media_proxy_url)
|
2020-05-14 19:18:31 +02:00
|
|
|
end
|
|
|
|
|
2020-09-10 20:28:07 +02:00
|
|
|
defp handle_png_preview(conn, media_proxy_url) do
|
2020-08-30 16:17:24 +02:00
|
|
|
quality = Config.get!([:media_preview_proxy, :image_quality])
|
2020-09-17 16:13:40 +02:00
|
|
|
{thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
|
2020-08-30 16:17:24 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
with {:ok, thumbnail_binary} <-
|
2020-08-30 16:17:24 +02:00
|
|
|
MediaHelper.image_resize(
|
2020-09-05 15:16:35 +02:00
|
|
|
media_proxy_url,
|
2020-08-30 16:17:24 +02:00
|
|
|
%{
|
|
|
|
max_width: thumbnail_max_width,
|
|
|
|
max_height: thumbnail_max_height,
|
|
|
|
quality: quality,
|
|
|
|
format: "png"
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
conn
|
2020-09-05 15:16:35 +02:00
|
|
|
|> put_preview_response_headers(["image/png", "preview.png"])
|
2020-08-30 16:17:24 +02:00
|
|
|
|> send_resp(200, thumbnail_binary)
|
|
|
|
else
|
|
|
|
_ ->
|
2020-09-12 09:33:42 +02:00
|
|
|
fallback_on_preview_error(conn, media_proxy_url)
|
2020-08-30 16:17:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-10 20:28:07 +02:00
|
|
|
defp handle_jpeg_preview(conn, media_proxy_url) do
|
2020-08-26 00:18:22 +02:00
|
|
|
quality = Config.get!([:media_preview_proxy, :image_quality])
|
2020-09-17 16:13:40 +02:00
|
|
|
{thumbnail_max_width, thumbnail_max_height} = thumbnail_max_dimensions()
|
2020-08-21 07:59:08 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
with {:ok, thumbnail_binary} <-
|
2020-08-26 00:18:22 +02:00
|
|
|
MediaHelper.image_resize(
|
2020-09-05 15:16:35 +02:00
|
|
|
media_proxy_url,
|
2020-08-21 07:59:08 +02:00
|
|
|
%{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
|
2020-05-20 19:26:43 +02:00
|
|
|
) do
|
2020-05-11 22:21:53 +02:00
|
|
|
conn
|
2020-08-31 12:08:50 +02:00
|
|
|
|> put_preview_response_headers()
|
2020-05-14 19:18:31 +02:00
|
|
|
|> send_resp(200, thumbnail_binary)
|
2020-05-08 22:06:47 +02:00
|
|
|
else
|
2020-08-27 19:31:55 +02:00
|
|
|
_ ->
|
2020-09-12 09:33:42 +02:00
|
|
|
fallback_on_preview_error(conn, media_proxy_url)
|
2020-08-27 19:31:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-05 15:16:35 +02:00
|
|
|
defp handle_video_preview(conn, media_proxy_url) do
|
2020-08-27 19:31:55 +02:00
|
|
|
with {:ok, thumbnail_binary} <-
|
2020-09-05 15:16:35 +02:00
|
|
|
MediaHelper.video_framegrab(media_proxy_url) do
|
2020-08-27 19:31:55 +02:00
|
|
|
conn
|
2020-08-31 12:08:50 +02:00
|
|
|
|> put_preview_response_headers()
|
2020-08-27 19:31:55 +02:00
|
|
|
|> send_resp(200, thumbnail_binary)
|
|
|
|
else
|
2020-05-08 22:06:47 +02:00
|
|
|
_ ->
|
2020-09-12 09:33:42 +02:00
|
|
|
fallback_on_preview_error(conn, media_proxy_url)
|
2020-05-08 22:06:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp drop_static_param_and_redirect(conn) do
|
|
|
|
uri_without_static_param =
|
|
|
|
conn
|
|
|
|
|> current_url()
|
|
|
|
|> UriHelper.modify_uri_params(%{}, ["static"])
|
|
|
|
|
|
|
|
redirect(conn, external: uri_without_static_param)
|
|
|
|
end
|
|
|
|
|
2020-09-12 09:33:42 +02:00
|
|
|
defp fallback_on_preview_error(conn, media_proxy_url) do
|
|
|
|
redirect(conn, external: media_proxy_url)
|
|
|
|
end
|
|
|
|
|
2020-09-05 15:16:35 +02:00
|
|
|
defp put_preview_response_headers(
|
|
|
|
conn,
|
|
|
|
[content_type, filename] = _content_info \\ ["image/jpeg", "preview.jpg"]
|
|
|
|
) do
|
2020-08-31 12:08:50 +02:00
|
|
|
conn
|
2020-09-01 20:21:58 +02:00
|
|
|
|> put_resp_header("content-type", content_type)
|
|
|
|
|> put_resp_header("content-disposition", "inline; filename=\"#{filename}\"")
|
2020-09-05 19:19:09 +02:00
|
|
|
|> put_resp_header("cache-control", ReverseProxy.default_cache_control_header())
|
2020-08-31 12:08:50 +02:00
|
|
|
end
|
|
|
|
|
2020-09-10 20:54:26 +02:00
|
|
|
defp thumbnail_max_dimensions do
|
2020-09-17 16:13:40 +02:00
|
|
|
config = media_preview_proxy_config()
|
2020-08-18 17:23:27 +02:00
|
|
|
|
2020-09-10 20:28:07 +02:00
|
|
|
thumbnail_max_width = Keyword.fetch!(config, :thumbnail_max_width)
|
|
|
|
thumbnail_max_height = Keyword.fetch!(config, :thumbnail_max_height)
|
2020-08-18 17:23:27 +02:00
|
|
|
|
|
|
|
{thumbnail_max_width, thumbnail_max_height}
|
2020-05-08 22:06:47 +02:00
|
|
|
end
|
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
defp min_content_length_for_preview do
|
|
|
|
Keyword.get(media_preview_proxy_config(), :min_content_length, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp media_preview_proxy_config do
|
|
|
|
Config.get!([:media_preview_proxy])
|
|
|
|
end
|
|
|
|
|
2020-08-19 20:36:26 +02:00
|
|
|
defp media_proxy_opts do
|
|
|
|
Config.get([:media_proxy, :proxy_opts], [])
|
|
|
|
end
|
2017-11-22 19:06:07 +01:00
|
|
|
end
|