[#2497] Media preview proxy: misc. improvements (static param support, dynamic fifo pipe path), refactoring.

This commit is contained in:
Ivan Tashkinov 2020-09-16 22:30:42 +03:00
parent 2f155fb89f
commit a781f41f96
7 changed files with 33 additions and 16 deletions

View File

@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Renamed `:timeout` in `pools` namespace to `:recv_timeout`, old name is deprecated. - Renamed `:timeout` in `pools` namespace to `:recv_timeout`, old name is deprecated.
- Minimum lifetime for ephmeral activities changed to 10 minutes and made configurable (`:min_lifetime` option). - Minimum lifetime for ephmeral activities changed to 10 minutes and made configurable (`:min_lifetime` option).
### Added
- Media preview proxy (requires media proxy be enabled; see `:media_preview_proxy` config for more details).
### Removed ### Removed
- **Breaking:** `Pleroma.Workers.Cron.StatsWorker` setting from Oban `:crontab` (moved to a simpler implementation). - **Breaking:** `Pleroma.Workers.Cron.StatsWorker` setting from Oban `:crontab` (moved to a simpler implementation).

View File

@ -9,8 +9,6 @@ defmodule Pleroma.Helpers.MediaHelper do
alias Pleroma.HTTP alias Pleroma.HTTP
@tmp_base "/tmp/pleroma-media_preview-pipe"
def image_resize(url, options) do def image_resize(url, options) do
with executable when is_binary(executable) <- System.find_executable("convert"), with executable when is_binary(executable) <- System.find_executable("convert"),
{:ok, args} <- prepare_image_resize_args(options), {:ok, args} <- prepare_image_resize_args(options),
@ -103,7 +101,7 @@ defmodule Pleroma.Helpers.MediaHelper do
end end
defp mkfifo do defp mkfifo do
path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}" path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
case System.cmd("mkfifo", [path]) do case System.cmd("mkfifo", [path]) do
{_, 0} -> {_, 0} ->

View File

@ -3,14 +3,17 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Helpers.UriHelper do defmodule Pleroma.Helpers.UriHelper do
def append_uri_params(uri, appended_params) do def modify_uri_params(uri, overridden_params, deleted_params \\ []) do
uri = URI.parse(uri) uri = URI.parse(uri)
appended_params = for {k, v} <- appended_params, into: %{}, do: {to_string(k), v}
existing_params = URI.query_decoder(uri.query || "") |> Enum.into(%{}) existing_params = URI.query_decoder(uri.query || "") |> Map.new()
updated_params_keys = Enum.uniq(Map.keys(existing_params) ++ Map.keys(appended_params)) overridden_params = Map.new(overridden_params, fn {k, v} -> {to_string(k), v} end)
deleted_params = Enum.map(deleted_params, &to_string/1)
updated_params = updated_params =
for k <- updated_params_keys, do: {k, appended_params[k] || existing_params[k]} existing_params
|> Map.merge(overridden_params)
|> Map.drop(deleted_params)
uri uri
|> Map.put(:query, URI.encode_query(updated_params)) |> Map.put(:query, URI.encode_query(updated_params))

View File

@ -182,9 +182,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
display_name = user.name || user.nickname display_name = user.name || user.nickname
avatar = User.avatar_url(user) |> MediaProxy.url() avatar = User.avatar_url(user) |> MediaProxy.url()
avatar_static = User.avatar_url(user) |> MediaProxy.preview_url(output_format: "jpeg") avatar_static = User.avatar_url(user) |> MediaProxy.preview_url(static: true)
header = User.banner_url(user) |> MediaProxy.url() header = User.banner_url(user) |> MediaProxy.url()
header_static = User.banner_url(user) |> MediaProxy.preview_url(output_format: "jpeg") header_static = User.banner_url(user) |> MediaProxy.preview_url(static: true)
following_count = following_count =
if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do

View File

@ -157,7 +157,7 @@ defmodule Pleroma.Web.MediaProxy do
def build_preview_url(sig_base64, url_base64, filename \\ nil, preview_params \\ []) do def build_preview_url(sig_base64, url_base64, filename \\ nil, preview_params \\ []) do
uri = proxy_url("proxy/preview", sig_base64, url_base64, filename) uri = proxy_url("proxy/preview", sig_base64, url_base64, filename)
UriHelper.append_uri_params(uri, preview_params) UriHelper.modify_uri_params(uri, preview_params)
end end
def verify_request_path_and_url( def verify_request_path_and_url(

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.Helpers.MediaHelper alias Pleroma.Helpers.MediaHelper
alias Pleroma.Helpers.UriHelper
alias Pleroma.ReverseProxy alias Pleroma.ReverseProxy
alias Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy
alias Plug.Conn alias Plug.Conn
@ -74,14 +75,26 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end end
defp handle_preview( defp handle_preview(
"image/" <> _ = _content_type, "image/gif" = _content_type,
_content_length, _content_length,
%{params: %{"output_format" => "jpeg"}} = conn, %{params: %{"static" => static}} = conn,
media_proxy_url media_proxy_url
) do )
when static in ["true", true] do
handle_jpeg_preview(conn, media_proxy_url) handle_jpeg_preview(conn, media_proxy_url)
end end
defp handle_preview(
_content_type,
_content_length,
%{params: %{"static" => static}} = conn,
_media_proxy_url
)
when static in ["true", true] do
uri_without_static_param = UriHelper.modify_uri_params(current_url(conn), %{}, ["static"])
redirect(conn, external: uri_without_static_param)
end
defp handle_preview("image/gif" = _content_type, _content_length, conn, media_proxy_url) do defp handle_preview("image/gif" = _content_type, _content_length, conn, media_proxy_url) do
redirect(conn, external: media_proxy_url) redirect(conn, external: media_proxy_url)
end end

View File

@ -119,7 +119,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
redirect_uri = redirect_uri(conn, redirect_uri) redirect_uri = redirect_uri(conn, redirect_uri)
url_params = %{access_token: token.token} url_params = %{access_token: token.token}
url_params = Maps.put_if_present(url_params, :state, params["state"]) url_params = Maps.put_if_present(url_params, :state, params["state"])
url = UriHelper.append_uri_params(redirect_uri, url_params) url = UriHelper.modify_uri_params(redirect_uri, url_params)
redirect(conn, external: url) redirect(conn, external: url)
else else
conn conn
@ -161,7 +161,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
redirect_uri = redirect_uri(conn, redirect_uri) redirect_uri = redirect_uri(conn, redirect_uri)
url_params = %{code: auth.token} url_params = %{code: auth.token}
url_params = Maps.put_if_present(url_params, :state, auth_attrs["state"]) url_params = Maps.put_if_present(url_params, :state, auth_attrs["state"])
url = UriHelper.append_uri_params(redirect_uri, url_params) url = UriHelper.modify_uri_params(redirect_uri, url_params)
redirect(conn, external: url) redirect(conn, external: url)
else else
conn conn