logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

read_description.ex (1720B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Upload.Filter.Exiftool.ReadDescription do
  5. @moduledoc """
  6. Gets a valid description from the related EXIF tags and provides them in the response if no description is provided yet.
  7. It will first check ImageDescription, when that doesn't probide a valid description, it will check iptc:Caption-Abstract.
  8. A valid description means the fields are filled in and not too long (see `:instance, :description_limit`).
  9. """
  10. @behaviour Pleroma.Upload.Filter
  11. def filter(%Pleroma.Upload{description: description})
  12. when is_binary(description),
  13. do: {:ok, :noop}
  14. def filter(%Pleroma.Upload{tempfile: file} = upload),
  15. do: {:ok, :filtered, upload |> Map.put(:description, read_description_from_exif_data(file))}
  16. def filter(_, _), do: {:ok, :noop}
  17. defp read_description_from_exif_data(file) do
  18. nil
  19. |> read_when_empty(file, "-ImageDescription")
  20. |> read_when_empty(file, "-iptc:Caption-Abstract")
  21. end
  22. defp read_when_empty(current_description, _, _) when is_binary(current_description),
  23. do: current_description
  24. defp read_when_empty(_, file, tag) do
  25. try do
  26. {tag_content, 0} =
  27. System.cmd("exiftool", ["-b", "-s3", tag, file],
  28. stderr_to_stdout: false,
  29. parallelism: true
  30. )
  31. tag_content = String.trim(tag_content)
  32. if tag_content != "" and
  33. String.length(tag_content) <=
  34. Pleroma.Config.get([:instance, :description_limit]),
  35. do: tag_content,
  36. else: nil
  37. rescue
  38. _ in ErlangError -> nil
  39. end
  40. end
  41. end