Remove newline for linter
[akkoma] / lib / pleroma / helpers / media_helper.ex
index ecd234558fb04f3a472de3b45bab13668efb7119..89dd4204bdd6e7c9f508c03f111472a69cf6c5e6 100644 (file)
@@ -7,19 +7,35 @@ defmodule Pleroma.Helpers.MediaHelper do
   Handles common media-related operations.
   """
 
-  def ffmpeg_resize_remote(uri, %{max_width: max_width, max_height: max_height}) do
+  def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do
+    quality = options[:quality] || 1
+
     cmd = ~s"""
-    curl -L "#{uri}" |
-    ffmpeg -i pipe:0 -f lavfi -i color=c=white \
+    ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
       -filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
         force_original_aspect_ratio=decrease [scaled]; \
         [1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
-      -f image2 -vcodec mjpeg -frames:v 1 pipe:1 | \
-    cat
+      -loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 -q:v #{quality} pipe:1
     """
 
-    with {:ok, [stdout: stdout_list]} <- Pleroma.Exec.cmd(cmd) do
-      {:ok, Enum.join(stdout_list)}
+    pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
+    loop_recv(pid)
+  end
+
+  defp loop_recv(pid) do
+    loop_recv(pid, <<>>)
+  end
+
+  defp loop_recv(pid, acc) do
+    receive do
+      {^pid, {:data, data}} ->
+        loop_recv(pid, acc <> data)
+
+      {^pid, {:exit_status, 0}} ->
+        {:ok, acc}
+
+      {^pid, {:exit_status, status}} ->
+        {:error, status}
     end
   end
 end