Keep args construction within video/image scopes instead of mangling down in fifo...
[akkoma] / lib / pleroma / helpers / media_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Helpers.MediaHelper do
6 @moduledoc """
7 Handles common media-related operations.
8 """
9
10 @tmp_base "/tmp/pleroma-media_preview-pipe"
11
12 def image_resize(url, options) do
13 with executable when is_binary(executable) <- System.find_executable("convert"),
14 {:ok, args} <- prepare_image_resize_args(options),
15 url = Pleroma.Web.MediaProxy.url(url),
16 {:ok, env} <- Pleroma.HTTP.get(url),
17 {:ok, fifo_path} <- mkfifo()
18 do
19 args = List.flatten([fifo_path, args])
20 run_fifo(fifo_path, env, executable, args)
21 else
22 nil -> {:error, {:convert, :command_not_found}}
23 {:error, _} = error -> error
24 end
25 end
26
27 defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
28 quality = options[:quality] || 85
29 resize = Enum.join([max_width, "x", max_height, ">"])
30 args = [
31 "-interlace", "Plane",
32 "-resize", resize,
33 "-quality", to_string(quality),
34 "jpg:-"
35 ]
36 {:ok, args}
37 end
38
39 defp prepare_image_resize_args(_), do: {:error, :missing_options}
40
41 def video_framegrab(url) do
42 with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
43 url = Pleroma.Web.MediaProxy.url(url),
44 {:ok, env} <- Pleroma.HTTP.get(url),
45 {:ok, fifo_path} <- mkfifo(),
46 args = [
47 "-y",
48 "-i", fifo_path,
49 "-vframes", "1",
50 "-f", "mjpeg",
51 "-loglevel", "error",
52 "pipe:"
53 ] do
54 run_fifo(fifo_path, env, executable, args)
55 else
56 nil -> {:error, {:ffmpeg, :command_not_found}}
57 {:error, _} = error -> error
58 end
59 end
60
61 defp run_fifo(fifo_path, env, executable, args) do
62 pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args])
63 fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
64 true = Port.command(fifo, env.body)
65 :erlang.port_close(fifo)
66 loop_recv(pid)
67 after
68 File.rm(fifo_path)
69 end
70
71 defp mkfifo() do
72 path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
73 case System.cmd("mkfifo", [path]) do
74 {_, 0} ->
75 spawn(fifo_guard(path))
76 {:ok, path}
77 {_, err} ->
78 {:error, {:fifo_failed, err}}
79 end
80 end
81
82 defp fifo_guard(path) do
83 pid = self()
84 fn() ->
85 ref = Process.monitor(pid)
86 receive do
87 {:DOWN, ^ref, :process, ^pid, _} ->
88 File.rm(path)
89 end
90 end
91 end
92
93 defp loop_recv(pid) do
94 loop_recv(pid, <<>>)
95 end
96
97 defp loop_recv(pid, acc) do
98 receive do
99 {^pid, {:data, data}} ->
100 loop_recv(pid, acc <> data)
101 {^pid, {:exit_status, 0}} ->
102 {:ok, acc}
103 {^pid, {:exit_status, status}} ->
104 {:error, status}
105 after
106 5000 ->
107 :erlang.port_close(pid)
108 {:error, :timeout}
109 end
110 end
111 end