1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Upload.Filter.AnalyzeMetadata do
7 Extracts metadata about the upload, such as width/height
11 @behaviour Pleroma.Upload.Filter
13 @spec filter(Pleroma.Upload.t()) ::
14 {:ok, :filtered, Pleroma.Upload.t()} | {:ok, :noop} | {:error, String.t()}
15 def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _} = upload) do
24 |> Map.put(:width, image.width)
25 |> Map.put(:height, image.height)
26 |> Map.put(:blurhash, get_blurhash(file))
28 {:ok, :filtered, upload}
31 Logger.warn("#{__MODULE__}: #{inspect(e)}")
36 def filter(%Pleroma.Upload{tempfile: file, content_type: "video" <> _} = upload) do
38 result = media_dimensions(file)
42 |> Map.put(:width, result.width)
43 |> Map.put(:height, result.height)
45 {:ok, :filtered, upload}
48 Logger.warn("#{__MODULE__}: #{inspect(e)}")
53 def filter(_), do: {:ok, :noop}
55 defp get_blurhash(file) do
56 with {:ok, blurhash} <- :eblurhash.magick(file) do
63 defp media_dimensions(file) do
64 with executable when is_binary(executable) <- System.find_executable("ffprobe"),
69 "stream=width,height",
74 {result, 0} <- System.cmd(executable, args),
76 String.split(String.trim(result), "x") |> Enum.map(&String.to_integer(&1)) do
77 %{width: width, height: height}
79 nil -> {:error, {:ffprobe, :command_not_found}}
80 {:error, _} = error -> error