2020-05-20 19:26:43 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.MediaHelper do
|
|
|
|
@moduledoc """
|
|
|
|
Handles common media-related operations.
|
|
|
|
"""
|
|
|
|
|
2020-09-05 15:16:35 +02:00
|
|
|
alias Pleroma.HTTP
|
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
def image_resize(url, options) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("convert"),
|
2020-08-26 23:40:13 +02:00
|
|
|
{:ok, args} <- prepare_image_resize_args(options),
|
2020-09-09 18:30:42 +02:00
|
|
|
{:ok, env} <- HTTP.get(url, [], pool: :media),
|
2020-08-28 21:14:28 +02:00
|
|
|
{:ok, fifo_path} <- mkfifo() do
|
2020-08-27 23:33:37 +02:00
|
|
|
args = List.flatten([fifo_path, args])
|
2020-08-26 16:12:34 +02:00
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:convert, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 16:17:24 +02:00
|
|
|
defp prepare_image_resize_args(
|
|
|
|
%{max_width: max_width, max_height: max_height, format: "png"} = options
|
|
|
|
) do
|
|
|
|
quality = options[:quality] || 85
|
|
|
|
resize = Enum.join([max_width, "x", max_height, ">"])
|
|
|
|
|
|
|
|
args = [
|
|
|
|
"-resize",
|
|
|
|
resize,
|
|
|
|
"-quality",
|
|
|
|
to_string(quality),
|
|
|
|
"png:-"
|
|
|
|
]
|
|
|
|
|
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
|
2020-08-26 23:40:13 +02:00
|
|
|
defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
|
2020-08-26 00:18:22 +02:00
|
|
|
quality = options[:quality] || 85
|
2020-08-26 16:12:34 +02:00
|
|
|
resize = Enum.join([max_width, "x", max_height, ">"])
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
args = [
|
2020-08-28 21:14:28 +02:00
|
|
|
"-interlace",
|
|
|
|
"Plane",
|
|
|
|
"-resize",
|
|
|
|
resize,
|
|
|
|
"-quality",
|
|
|
|
to_string(quality),
|
|
|
|
"jpg:-"
|
2020-08-26 16:12:34 +02:00
|
|
|
]
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
{:ok, args}
|
|
|
|
end
|
2020-08-21 07:59:08 +02:00
|
|
|
|
2020-08-26 23:40:13 +02:00
|
|
|
defp prepare_image_resize_args(_), do: {:error, :missing_options}
|
2020-05-20 19:26:43 +02:00
|
|
|
|
2020-09-17 16:13:40 +02:00
|
|
|
# Note: video thumbnail is intentionally not resized (always has original dimensions)
|
2020-08-27 19:31:55 +02:00
|
|
|
def video_framegrab(url) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
|
2020-09-09 18:30:42 +02:00
|
|
|
{:ok, env} <- HTTP.get(url, [], pool: :media),
|
2020-08-27 19:47:29 +02:00
|
|
|
{:ok, fifo_path} <- mkfifo(),
|
2020-08-27 19:31:55 +02:00
|
|
|
args = [
|
2020-08-27 20:10:40 +02:00
|
|
|
"-y",
|
2020-08-28 21:14:28 +02:00
|
|
|
"-i",
|
|
|
|
fifo_path,
|
|
|
|
"-vframes",
|
|
|
|
"1",
|
|
|
|
"-f",
|
|
|
|
"mjpeg",
|
|
|
|
"-loglevel",
|
|
|
|
"error",
|
|
|
|
"-"
|
2020-08-27 19:47:29 +02:00
|
|
|
] do
|
2020-08-27 19:31:55 +02:00
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:ffmpeg, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
defp run_fifo(fifo_path, env, executable, args) do
|
2020-08-28 21:14:28 +02:00
|
|
|
pid =
|
|
|
|
Port.open({:spawn_executable, executable}, [
|
|
|
|
:use_stdio,
|
|
|
|
:stream,
|
|
|
|
:exit_status,
|
|
|
|
:binary,
|
|
|
|
args: args
|
|
|
|
])
|
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
|
2020-08-28 21:14:28 +02:00
|
|
|
fix = Pleroma.Helpers.QtFastStart.fix(env.body)
|
|
|
|
true = Port.command(fifo, fix)
|
2020-08-26 16:12:34 +02:00
|
|
|
:erlang.port_close(fifo)
|
2020-08-21 19:02:57 +02:00
|
|
|
loop_recv(pid)
|
2020-08-26 16:12:34 +02:00
|
|
|
after
|
|
|
|
File.rm(fifo_path)
|
|
|
|
end
|
|
|
|
|
2020-08-30 16:32:22 +02:00
|
|
|
defp mkfifo do
|
2020-09-16 21:30:42 +02:00
|
|
|
path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
case System.cmd("mkfifo", [path]) do
|
|
|
|
{_, 0} ->
|
|
|
|
spawn(fifo_guard(path))
|
|
|
|
{:ok, path}
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
{_, err} ->
|
|
|
|
{:error, {:fifo_failed, err}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fifo_guard(path) do
|
|
|
|
pid = self()
|
2020-08-28 21:14:28 +02:00
|
|
|
|
|
|
|
fn ->
|
2020-08-26 16:12:34 +02:00
|
|
|
ref = Process.monitor(pid)
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-26 16:12:34 +02:00
|
|
|
receive do
|
|
|
|
{:DOWN, ^ref, :process, ^pid, _} ->
|
|
|
|
File.rm(path)
|
|
|
|
end
|
|
|
|
end
|
2020-08-21 19:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp loop_recv(pid) do
|
|
|
|
loop_recv(pid, <<>>)
|
|
|
|
end
|
2020-08-18 17:23:27 +02:00
|
|
|
|
2020-08-21 19:02:57 +02:00
|
|
|
defp loop_recv(pid, acc) do
|
2020-08-18 17:23:27 +02:00
|
|
|
receive do
|
|
|
|
{^pid, {:data, data}} ->
|
2020-08-21 19:02:57 +02:00
|
|
|
loop_recv(pid, acc <> data)
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-21 19:02:57 +02:00
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
2020-08-28 21:14:28 +02:00
|
|
|
|
2020-08-21 19:02:57 +02:00
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 17:23:27 +02:00
|
|
|
{:error, status}
|
2020-08-28 21:14:28 +02:00
|
|
|
after
|
|
|
|
5000 ->
|
|
|
|
:erlang.port_close(pid)
|
|
|
|
{:error, :timeout}
|
2020-05-20 19:26:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|