[#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.
- 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
- **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
@tmp_base "/tmp/pleroma-media_preview-pipe"
def image_resize(url, options) do
with executable when is_binary(executable) <- System.find_executable("convert"),
{:ok, args} <- prepare_image_resize_args(options),
@ -103,7 +101,7 @@ defp run_fifo(fifo_path, env, executable, args) do
end
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
{_, 0} ->

View file

@ -3,14 +3,17 @@
# SPDX-License-Identifier: AGPL-3.0-only
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)
appended_params = for {k, v} <- appended_params, into: %{}, do: {to_string(k), v}
existing_params = URI.query_decoder(uri.query || "") |> Enum.into(%{})
updated_params_keys = Enum.uniq(Map.keys(existing_params) ++ Map.keys(appended_params))
existing_params = URI.query_decoder(uri.query || "") |> Map.new()
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 =
for k <- updated_params_keys, do: {k, appended_params[k] || existing_params[k]}
existing_params
|> Map.merge(overridden_params)
|> Map.drop(deleted_params)
uri
|> Map.put(:query, URI.encode_query(updated_params))

View file

@ -182,9 +182,9 @@ defp do_render("show.json", %{user: user} = opts) do
display_name = user.name || user.nickname
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_static = User.banner_url(user) |> MediaProxy.preview_url(output_format: "jpeg")
header_static = User.banner_url(user) |> MediaProxy.preview_url(static: true)
following_count =
if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do

View file

@ -157,7 +157,7 @@ def build_url(sig_base64, url_base64, filename \\ nil) do
def build_preview_url(sig_base64, url_base64, filename \\ nil, preview_params \\ []) do
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
def verify_request_path_and_url(

View file

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

View file

@ -119,7 +119,7 @@ defp handle_existing_authorization(
redirect_uri = redirect_uri(conn, redirect_uri)
url_params = %{access_token: token.token}
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)
else
conn
@ -161,7 +161,7 @@ def after_create_authorization(%Plug.Conn{} = conn, %Authorization{} = auth, %{
redirect_uri = redirect_uri(conn, redirect_uri)
url_params = %{code: auth.token}
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)
else
conn