2020-05-20 17:26:43 +00: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-08-26 14:12:34 +00:00
|
|
|
@tmp_base "/tmp/pleroma-media_preview-pipe"
|
|
|
|
|
|
|
|
def image_resize(url, options) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("convert"),
|
2020-08-26 21:40:13 +00:00
|
|
|
{:ok, args} <- prepare_image_resize_args(options),
|
2020-08-26 14:12:34 +00:00
|
|
|
url = Pleroma.Web.MediaProxy.url(url),
|
|
|
|
{:ok, env} <- Pleroma.HTTP.get(url),
|
|
|
|
{:ok, fifo_path} <- mkfifo()
|
|
|
|
do
|
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:convert, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 21:40:13 +00:00
|
|
|
defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
|
2020-08-25 22:18:22 +00:00
|
|
|
quality = options[:quality] || 85
|
2020-08-26 14:12:34 +00:00
|
|
|
resize = Enum.join([max_width, "x", max_height, ">"])
|
|
|
|
args = [
|
2020-08-26 15:37:45 +00:00
|
|
|
"-interlace", "Plane",
|
2020-08-26 14:12:34 +00:00
|
|
|
"-resize", resize,
|
2020-08-26 22:43:25 +00:00
|
|
|
"-quality", to_string(quality),
|
|
|
|
"jpg:-"
|
2020-08-26 14:12:34 +00:00
|
|
|
]
|
|
|
|
{:ok, args}
|
|
|
|
end
|
2020-08-21 05:59:08 +00:00
|
|
|
|
2020-08-26 21:40:13 +00:00
|
|
|
defp prepare_image_resize_args(_), do: {:error, :missing_options}
|
2020-05-20 17:26:43 +00:00
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
defp run_fifo(fifo_path, env, executable, args) do
|
2020-08-26 22:43:25 +00:00
|
|
|
args = List.flatten([fifo_path, args])
|
2020-08-26 14:12:34 +00:00
|
|
|
pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args])
|
|
|
|
fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
|
|
|
|
true = Port.command(fifo, env.body)
|
|
|
|
:erlang.port_close(fifo)
|
2020-08-21 17:02:57 +00:00
|
|
|
loop_recv(pid)
|
2020-08-26 14:12:34 +00:00
|
|
|
after
|
|
|
|
File.rm(fifo_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp mkfifo() do
|
|
|
|
path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
|
|
|
|
case System.cmd("mkfifo", [path]) do
|
|
|
|
{_, 0} ->
|
|
|
|
spawn(fifo_guard(path))
|
|
|
|
{:ok, path}
|
|
|
|
{_, err} ->
|
|
|
|
{:error, {:fifo_failed, err}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fifo_guard(path) do
|
|
|
|
pid = self()
|
|
|
|
fn() ->
|
|
|
|
ref = Process.monitor(pid)
|
|
|
|
receive do
|
|
|
|
{:DOWN, ^ref, :process, ^pid, _} ->
|
|
|
|
File.rm(path)
|
|
|
|
end
|
|
|
|
end
|
2020-08-21 17:02:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp loop_recv(pid) do
|
|
|
|
loop_recv(pid, <<>>)
|
|
|
|
end
|
2020-08-18 15:23:27 +00:00
|
|
|
|
2020-08-21 17:02:57 +00:00
|
|
|
defp loop_recv(pid, acc) do
|
2020-08-18 15:23:27 +00:00
|
|
|
receive do
|
|
|
|
{^pid, {:data, data}} ->
|
2020-08-21 17:02:57 +00:00
|
|
|
loop_recv(pid, acc <> data)
|
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 15:23:27 +00:00
|
|
|
{:error, status}
|
2020-08-26 14:12:34 +00:00
|
|
|
after
|
|
|
|
5000 ->
|
|
|
|
:erlang.port_close(pid)
|
|
|
|
{:error, :timeout}
|
2020-05-20 17:26:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|