[#2497] Media preview proxy: misc. improvements (`static` param support, dynamic...
[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 alias Pleroma.HTTP
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 {:ok, env} <- HTTP.get(url, [], pool: :media),
16 {:ok, fifo_path} <- mkfifo() do
17 args = List.flatten([fifo_path, args])
18 run_fifo(fifo_path, env, executable, args)
19 else
20 nil -> {:error, {:convert, :command_not_found}}
21 {:error, _} = error -> error
22 end
23 end
24
25 defp prepare_image_resize_args(
26 %{max_width: max_width, max_height: max_height, format: "png"} = options
27 ) do
28 quality = options[:quality] || 85
29 resize = Enum.join([max_width, "x", max_height, ">"])
30
31 args = [
32 "-resize",
33 resize,
34 "-quality",
35 to_string(quality),
36 "png:-"
37 ]
38
39 {:ok, args}
40 end
41
42 defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
43 quality = options[:quality] || 85
44 resize = Enum.join([max_width, "x", max_height, ">"])
45
46 args = [
47 "-interlace",
48 "Plane",
49 "-resize",
50 resize,
51 "-quality",
52 to_string(quality),
53 "jpg:-"
54 ]
55
56 {:ok, args}
57 end
58
59 defp prepare_image_resize_args(_), do: {:error, :missing_options}
60
61 def video_framegrab(url) do
62 with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
63 {:ok, env} <- HTTP.get(url, [], pool: :media),
64 {:ok, fifo_path} <- mkfifo(),
65 args = [
66 "-y",
67 "-i",
68 fifo_path,
69 "-vframes",
70 "1",
71 "-f",
72 "mjpeg",
73 "-loglevel",
74 "error",
75 "-"
76 ] do
77 run_fifo(fifo_path, env, executable, args)
78 else
79 nil -> {:error, {:ffmpeg, :command_not_found}}
80 {:error, _} = error -> error
81 end
82 end
83
84 defp run_fifo(fifo_path, env, executable, args) do
85 pid =
86 Port.open({:spawn_executable, executable}, [
87 :use_stdio,
88 :stream,
89 :exit_status,
90 :binary,
91 args: args
92 ])
93
94 fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
95 fix = Pleroma.Helpers.QtFastStart.fix(env.body)
96 true = Port.command(fifo, fix)
97 :erlang.port_close(fifo)
98 loop_recv(pid)
99 after
100 File.rm(fifo_path)
101 end
102
103 defp mkfifo do
104 path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
105
106 case System.cmd("mkfifo", [path]) do
107 {_, 0} ->
108 spawn(fifo_guard(path))
109 {:ok, path}
110
111 {_, err} ->
112 {:error, {:fifo_failed, err}}
113 end
114 end
115
116 defp fifo_guard(path) do
117 pid = self()
118
119 fn ->
120 ref = Process.monitor(pid)
121
122 receive do
123 {:DOWN, ^ref, :process, ^pid, _} ->
124 File.rm(path)
125 end
126 end
127 end
128
129 defp loop_recv(pid) do
130 loop_recv(pid, <<>>)
131 end
132
133 defp loop_recv(pid, acc) do
134 receive do
135 {^pid, {:data, data}} ->
136 loop_recv(pid, acc <> data)
137
138 {^pid, {:exit_status, 0}} ->
139 {:ok, acc}
140
141 {^pid, {:exit_status, status}} ->
142 {:error, status}
143 after
144 5000 ->
145 :erlang.port_close(pid)
146 {:error, :timeout}
147 end
148 end
149 end