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