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