X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fhelpers%2Fmedia_helper.ex;h=738adfcaa9d446e71dafc5aa7ab2b169dbf934dd;hb=ff00b354fa5067c898e860e275748dd757cb04cd;hp=5ac75b326b146fca0a6ccd3b65611577547bd971;hpb=dfceb03cf47374fdeab60784476b2e266208a4bb;p=akkoma
diff --git a/lib/pleroma/helpers/media_helper.ex b/lib/pleroma/helpers/media_helper.ex
index 5ac75b326..738adfcaa 100644
--- a/lib/pleroma/helpers/media_helper.ex
+++ b/lib/pleroma/helpers/media_helper.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Helpers.MediaHelper do
@@ -7,13 +7,24 @@ defmodule Pleroma.Helpers.MediaHelper do
Handles common media-related operations.
"""
- @tmp_base "/tmp/pleroma-media_preview-pipe"
+ alias Pleroma.HTTP
+
+ require Logger
+
+ def missing_dependencies do
+ Enum.reduce([imagemagick: "convert", ffmpeg: "ffmpeg"], [], fn {sym, executable}, acc ->
+ if Pleroma.Utils.command_available?(executable) do
+ acc
+ else
+ [sym | acc]
+ end
+ end)
+ end
def image_resize(url, options) do
with executable when is_binary(executable) <- System.find_executable("convert"),
{:ok, args} <- prepare_image_resize_args(options),
- url = Pleroma.Web.MediaProxy.url(url),
- {:ok, env} <- Pleroma.HTTP.get(url),
+ {:ok, env} <- HTTP.get(url, [], pool: :media),
{:ok, fifo_path} <- mkfifo() do
args = List.flatten([fifo_path, args])
run_fifo(fifo_path, env, executable, args)
@@ -23,6 +34,23 @@ defmodule Pleroma.Helpers.MediaHelper do
end
end
+ defp prepare_image_resize_args(
+ %{max_width: max_width, max_height: max_height, format: "png"} = options
+ ) do
+ quality = options[:quality] || 85
+ resize = Enum.join([max_width, "x", max_height, ">"])
+
+ args = [
+ "-resize",
+ resize,
+ "-quality",
+ to_string(quality),
+ "png:-"
+ ]
+
+ {:ok, args}
+ end
+
defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
quality = options[:quality] || 85
resize = Enum.join([max_width, "x", max_height, ">"])
@@ -42,10 +70,10 @@ defmodule Pleroma.Helpers.MediaHelper do
defp prepare_image_resize_args(_), do: {:error, :missing_options}
+ # Note: video thumbnail is intentionally not resized (always has original dimensions)
def video_framegrab(url) do
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
- url = Pleroma.Web.MediaProxy.url(url),
- {:ok, env} <- Pleroma.HTTP.get(url),
+ {:ok, env} <- HTTP.get(url, [], pool: :media),
{:ok, fifo_path} <- mkfifo(),
args = [
"-y",
@@ -85,8 +113,8 @@ defmodule Pleroma.Helpers.MediaHelper do
File.rm(fifo_path)
end
- defp mkfifo() do
- path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
+ defp mkfifo do
+ path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
case System.cmd("mkfifo", [path]) do
{_, 0} ->