[#2497] Added video preview proxy. Switched from exexec to Port.
[akkoma] / lib / pleroma / web / media_proxy / media_proxy_controller.ex
index 583c177f2e609eee9b92d01bc114009723d121d5..8861398dd857a53d870a1a2220ac17de1e0a5077 100644 (file)
@@ -66,31 +66,23 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
     end
   end
 
-  defp thumbnail_max_dimensions(params) do
-    config = Config.get([:media_preview_proxy], [])
-
-    thumbnail_max_width =
-      if w = params["thumbnail_max_width"] do
-        String.to_integer(w)
-      else
-        Keyword.fetch!(config, :thumbnail_max_width)
-      end
+  defp handle_preview("image/" <> _ = _content_type, conn, url) do
+    handle_image_or_video_preview(conn, url)
+  end
 
-    thumbnail_max_height =
-      if h = params["thumbnail_max_height"] do
-        String.to_integer(h)
-      else
-        Keyword.fetch!(config, :thumbnail_max_height)
-      end
+  defp handle_preview("video/" <> _ = _content_type, conn, url) do
+    handle_image_or_video_preview(conn, url)
+  end
 
-    {thumbnail_max_width, thumbnail_max_height}
+  defp handle_preview(content_type, conn, _url) do
+    send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
   end
 
-  defp handle_preview("image/" <> _ = _content_type, %{params: params} = conn, url) do
+  defp handle_image_or_video_preview(%{params: params} = conn, url) do
     with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
          media_proxy_url <- MediaProxy.url(url),
          {:ok, thumbnail_binary} <-
-           MediaHelper.ffmpeg_resize_remote(
+           MediaHelper.ffmpeg_resize(
              media_proxy_url,
              %{max_width: thumbnail_max_width, max_height: thumbnail_max_height}
            ) do
@@ -99,12 +91,28 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
       |> send_resp(200, thumbnail_binary)
     else
       _ ->
-        send_resp(conn, :failed_dependency, "Can't handle image preview.")
+        send_resp(conn, :failed_dependency, "Can't handle preview.")
     end
   end
 
-  defp handle_preview(content_type, conn, _url) do
-    send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
+  defp thumbnail_max_dimensions(params) do
+    config = Config.get([:media_preview_proxy], [])
+
+    thumbnail_max_width =
+      if w = params["thumbnail_max_width"] do
+        String.to_integer(w)
+      else
+        Keyword.fetch!(config, :thumbnail_max_width)
+      end
+
+    thumbnail_max_height =
+      if h = params["thumbnail_max_height"] do
+        String.to_integer(h)
+      else
+        Keyword.fetch!(config, :thumbnail_max_height)
+      end
+
+    {thumbnail_max_width, thumbnail_max_height}
   end
 
   defp preview_head_request_timeout do