[#2497] Added video preview proxy. Switched from exexec to Port.

This commit is contained in:
Ivan Tashkinov 2020-08-18 18:23:27 +03:00
commit da116d81fb
8 changed files with 41 additions and 89 deletions

View file

@ -1,38 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Exec do
@moduledoc "Pleroma wrapper around Exexec commands."
alias Pleroma.Config
def ensure_started(options_overrides \\ %{}) do
options =
if Config.get([:exexec, :root_mode]) || System.get_env("USER") == "root" do
# Note: running as `root` is discouraged (yet Gitlab CI does that by default)
%{root: true, user: "root", limit_users: ["root"]}
else
%{}
end
options =
options
|> Map.merge(Config.get([:exexec, :options], %{}))
|> Map.merge(options_overrides)
with {:error, {:already_started, pid}} <- Exexec.start(options) do
{:ok, pid}
end
end
def run(cmd, options \\ %{}) do
ensure_started()
Exexec.run(cmd, options)
end
def cmd(cmd, options \\ %{}) do
options = Map.merge(%{sync: true, stdout: true}, options)
run(cmd, options)
end
end

View file

@ -7,19 +7,24 @@ defmodule Pleroma.Helpers.MediaHelper do
Handles common media-related operations.
"""
def ffmpeg_resize_remote(uri, %{max_width: max_width, max_height: max_height}) do
def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height}) do
cmd = ~s"""
curl -L "#{uri}" |
ffmpeg -i pipe:0 -f lavfi -i color=c=white \
ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
-filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
force_original_aspect_ratio=decrease [scaled]; \
[1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
-f image2 -vcodec mjpeg -frames:v 1 pipe:1 | \
cat
-loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 pipe:1
"""
with {:ok, [stdout: stdout_list]} <- Pleroma.Exec.cmd(cmd) do
{:ok, Enum.join(stdout_list)}
pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
receive do
{^pid, {:data, data}} ->
send(pid, {self(), :close})
{:ok, data}
{^pid, {:exit_status, status}} when status > 0 ->
{:error, status}
end
end
end

View file

@ -66,6 +66,35 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
end
end
defp handle_preview("image/" <> _ = _content_type, conn, url) do
handle_image_or_video_preview(conn, url)
end
defp handle_preview("video/" <> _ = _content_type, conn, url) do
handle_image_or_video_preview(conn, url)
end
defp handle_preview(content_type, conn, _url) do
send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
end
defp handle_image_or_video_preview(%{params: params} = conn, url) do
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
media_proxy_url <- MediaProxy.url(url),
{:ok, thumbnail_binary} <-
MediaHelper.ffmpeg_resize(
media_proxy_url,
%{max_width: thumbnail_max_width, max_height: thumbnail_max_height}
) do
conn
|> put_resp_header("content-type", "image/jpeg")
|> send_resp(200, thumbnail_binary)
else
_ ->
send_resp(conn, :failed_dependency, "Can't handle preview.")
end
end
defp thumbnail_max_dimensions(params) do
config = Config.get([:media_preview_proxy], [])
@ -86,27 +115,6 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
{thumbnail_max_width, thumbnail_max_height}
end
defp handle_preview("image/" <> _ = _content_type, %{params: params} = conn, url) do
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
media_proxy_url <- MediaProxy.url(url),
{:ok, thumbnail_binary} <-
MediaHelper.ffmpeg_resize_remote(
media_proxy_url,
%{max_width: thumbnail_max_width, max_height: thumbnail_max_height}
) do
conn
|> put_resp_header("content-type", "image/jpeg")
|> send_resp(200, thumbnail_binary)
else
_ ->
send_resp(conn, :failed_dependency, "Can't handle image preview.")
end
end
defp handle_preview(content_type, conn, _url) do
send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
end
defp preview_head_request_timeout do
Config.get([:media_preview_proxy, :proxy_opts, :head_request_max_read_duration]) ||
preview_timeout()