HTTP signatures respect allowlist federation
[akkoma] / lib / pleroma / helpers / media_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 require Logger
13
14 def missing_dependencies do
15 Enum.reduce([imagemagick: "convert", ffmpeg: "ffmpeg"], [], fn {sym, executable}, acc ->
16 if Pleroma.Utils.command_available?(executable) do
17 acc
18 else
19 [sym | acc]
20 end
21 end)
22 end
23
24 def image_resize(url, options) do
25 with executable when is_binary(executable) <- System.find_executable("convert"),
26 {:ok, args} <- prepare_image_resize_args(options),
27 {:ok, env} <- HTTP.get(url, [], []),
28 {:ok, fifo_path} <- mkfifo() do
29 args = List.flatten([fifo_path, args])
30 run_fifo(fifo_path, env, executable, args)
31 else
32 nil -> {:error, {:convert, :command_not_found}}
33 {:error, _} = error -> error
34 end
35 end
36
37 defp prepare_image_resize_args(
38 %{max_width: max_width, max_height: max_height, format: "png"} = options
39 ) do
40 quality = options[:quality] || 85
41 resize = Enum.join([max_width, "x", max_height, ">"])
42
43 args = [
44 "-resize",
45 resize,
46 "-quality",
47 to_string(quality),
48 "png:-"
49 ]
50
51 {:ok, args}
52 end
53
54 defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
55 quality = options[:quality] || 85
56 resize = Enum.join([max_width, "x", max_height, ">"])
57
58 args = [
59 "-interlace",
60 "Plane",
61 "-resize",
62 resize,
63 "-quality",
64 to_string(quality),
65 "jpg:-"
66 ]
67
68 {:ok, args}
69 end
70
71 defp prepare_image_resize_args(_), do: {:error, :missing_options}
72
73 # Note: video thumbnail is intentionally not resized (always has original dimensions)
74 def video_framegrab(url) do
75 with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
76 {:ok, env} <- HTTP.get(url, [], []),
77 {:ok, fifo_path} <- mkfifo(),
78 args = [
79 "-y",
80 "-i",
81 fifo_path,
82 "-vframes",
83 "1",
84 "-f",
85 "mjpeg",
86 "-loglevel",
87 "error",
88 "-"
89 ] do
90 run_fifo(fifo_path, env, executable, args)
91 else
92 nil -> {:error, {:ffmpeg, :command_not_found}}
93 {:error, _} = error -> error
94 end
95 end
96
97 defp run_fifo(fifo_path, env, executable, args) do
98 pid =
99 Port.open({:spawn_executable, executable}, [
100 :use_stdio,
101 :stream,
102 :exit_status,
103 :binary,
104 args: args
105 ])
106
107 fifo = File.open!(fifo_path, [:append, :binary])
108 fix = Pleroma.Helpers.QtFastStart.fix(env.body)
109 IO.binwrite(fifo, fix)
110 File.close(fifo)
111 loop_recv(pid)
112 after
113 File.rm(fifo_path)
114 end
115
116 defp mkfifo do
117 path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
118
119 case System.cmd("mkfifo", [path]) do
120 {_, 0} ->
121 spawn(fifo_guard(path))
122 {:ok, path}
123
124 {_, err} ->
125 {:error, {:fifo_failed, err}}
126 end
127 end
128
129 defp fifo_guard(path) do
130 pid = self()
131
132 fn ->
133 ref = Process.monitor(pid)
134
135 receive do
136 {:DOWN, ^ref, :process, ^pid, _} ->
137 File.rm(path)
138 end
139 end
140 end
141
142 defp loop_recv(pid) do
143 loop_recv(pid, <<>>)
144 end
145
146 defp loop_recv(pid, acc) do
147 receive do
148 {^pid, {:data, data}} ->
149 loop_recv(pid, acc <> data)
150
151 {^pid, {:exit_status, 0}} ->
152 {:ok, acc}
153
154 {^pid, {:exit_status, status}} ->
155 {:error, status}
156 after
157 5000 ->
158 :erlang.port_close(pid)
159 {:error, :timeout}
160 end
161 end
162 end