1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MediaProxy.MediaProxyController do
6 use Pleroma.Web, :controller
9 alias Pleroma.Helpers.MediaHelper
10 alias Pleroma.ReverseProxy
11 alias Pleroma.Web.MediaProxy
13 def remote(conn, %{"sig" => sig64, "url" => url64}) do
14 with {_, true} <- {:enabled, MediaProxy.enabled?()},
15 {:ok, url} <- MediaProxy.decode_url(sig64, url64),
16 {_, false} <- {:in_banned_urls, MediaProxy.in_banned_urls(url)},
17 :ok <- MediaProxy.verify_request_path_and_url(conn, url) do
18 ReverseProxy.call(conn, url, media_proxy_opts())
21 send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
23 {:in_banned_urls, true} ->
24 send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
26 {:error, :invalid_signature} ->
27 send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
29 {:wrong_filename, filename} ->
30 redirect(conn, external: MediaProxy.build_url(sig64, url64, filename))
34 def preview(conn, %{"sig" => sig64, "url" => url64}) do
35 with {_, true} <- {:enabled, MediaProxy.preview_enabled?()},
36 {:ok, url} <- MediaProxy.decode_url(sig64, url64),
37 :ok <- MediaProxy.verify_request_path_and_url(conn, url) do
38 handle_preview(conn, url)
41 send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
43 {:error, :invalid_signature} ->
44 send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
46 {:wrong_filename, filename} ->
47 redirect(conn, external: MediaProxy.build_preview_url(sig64, url64, filename))
51 defp handle_preview(conn, url) do
52 with {:ok, %{status: status} = head_response} when status in 200..299 <-
54 opts: [adapter: [timeout: preview_head_request_timeout(), follow_redirect: true]]
56 content_type = Tesla.get_header(head_response, "content-type")
57 handle_preview(content_type, conn, url)
59 {_, %{status: status}} ->
60 send_resp(conn, :failed_dependency, "Can't fetch HTTP headers (HTTP #{status}).")
62 {:error, :recv_response_timeout} ->
63 send_resp(conn, :failed_dependency, "HEAD request timeout.")
66 send_resp(conn, :failed_dependency, "Can't fetch HTTP headers.")
70 # TODO: find a workaround so avatar_static and banner_static can work.
71 # Those only permit GIFs for animation, so we have to permit a way to
72 # allow those to get real static variants.
73 defp handle_preview("image/gif" = _content_type, conn, url) do
74 mediaproxy_url = url |> MediaProxy.url()
76 redirect(conn, external: mediaproxy_url)
79 defp handle_preview("image/" <> _ = _content_type, conn, url) do
80 handle_image_preview(conn, url)
83 defp handle_preview("video/" <> _ = _content_type, conn, url) do
84 handle_video_preview(conn, url)
87 defp handle_preview(content_type, conn, _url) do
88 send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
91 defp handle_image_preview(%{params: params} = conn, url) do
92 quality = Config.get!([:media_preview_proxy, :image_quality])
94 with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
95 {:ok, thumbnail_binary} <-
96 MediaHelper.image_resize(
98 %{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
101 |> put_resp_header("content-type", "image/jpeg")
102 |> put_resp_header("content-disposition", "inline; filename=\"preview.jpg\"")
103 |> send_resp(200, thumbnail_binary)
106 send_resp(conn, :failed_dependency, "Can't handle preview.")
110 defp handle_video_preview(conn, url) do
111 with {:ok, thumbnail_binary} <-
112 MediaHelper.video_framegrab(url) do
114 |> put_resp_header("content-type", "image/jpeg")
115 |> put_resp_header("content-disposition", "inline; filename=\"preview.jpg\"")
116 |> send_resp(200, thumbnail_binary)
119 send_resp(conn, :failed_dependency, "Can't handle preview.")
123 defp thumbnail_max_dimensions(params) do
124 config = Config.get([:media_preview_proxy], [])
126 thumbnail_max_width =
127 if w = params["thumbnail_max_width"] do
130 Keyword.fetch!(config, :thumbnail_max_width)
133 thumbnail_max_height =
134 if h = params["thumbnail_max_height"] do
137 Keyword.fetch!(config, :thumbnail_max_height)
140 {thumbnail_max_width, thumbnail_max_height}
143 defp preview_head_request_timeout do
144 Keyword.get(media_preview_proxy_opts(), :head_request_max_read_duration) ||
145 Keyword.get(media_proxy_opts(), :max_read_duration) ||
146 ReverseProxy.max_read_duration_default()
149 defp media_proxy_opts do
150 Config.get([:media_proxy, :proxy_opts], [])
153 defp media_preview_proxy_opts do
154 Config.get([:media_preview_proxy, :proxy_opts], [])