logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: 551721e41a0bd98bb840baca48415a781cc463a7
parent cd316d7269a6cac1e9edb732b202343001b82399
Author: Ilja <ilja@ilja.space>
Date:   Sun, 20 Feb 2022 12:59:42 +0100

Rename the new module

Diffstat:

Mdocs/configuration/cheatsheet.md4++--
Mdocs/installation/optional/media_graphics_packages.md2+-
Mlib/mix/tasks/pleroma/instance.ex18+++++++++---------
Mlib/pleroma/application_requirements.ex2+-
Rlib/pleroma/upload/filter/exiftool.ex -> lib/pleroma/upload/filter/exiftool/exiftool.ex0
Alib/pleroma/upload/filter/exiftool/read_description.ex47+++++++++++++++++++++++++++++++++++++++++++++++
Dlib/pleroma/upload/filter/exiftool_read_data.ex47-----------------------------------------------
Mtest/mix/tasks/pleroma/instance_test.exs4++--
Rtest/pleroma/upload/filter/exiftool_test.exs -> test/pleroma/upload/filter/exiftool/exiftool_test.exs0
Atest/pleroma/upload/filter/exiftool/read_description_test.exs106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dtest/pleroma/upload/filter/exiftool_read_data_test.exs106-------------------------------------------------------------------------------
11 files changed, 168 insertions(+), 168 deletions(-)

diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md @@ -633,9 +633,9 @@ This filter only strips the GPS and location metadata with Exiftool leaving colo No specific configuration. -#### Pleroma.Upload.Filter.ExiftoolReadData +#### Pleroma.Upload.Filter.Exiftool.ReadDescription -This filter only reads metadata with Exiftool so clients can prefill the media description field. +This filter reads the ImageDescription and iptc:Caption-Abstract fields with Exiftool so clients can prefill the media description field. No specific configuration. diff --git a/docs/installation/optional/media_graphics_packages.md b/docs/installation/optional/media_graphics_packages.md @@ -30,4 +30,4 @@ It is required for the following Pleroma features: It is required for the following Pleroma features: * `Pleroma.Upload.Filters.Exiftool` upload filter (related config: `Plaroma.Upload/filters` in `config/config.exs`) - * `Pleroma.Upload.Filters.ExiftoolReadData` upload filter (related config: `Plaroma.Upload/filters` in `config/config.exs`) + * `Pleroma.Upload.Filters.Exiftool.ReadDescription` upload filter (related config: `Plaroma.Upload/filters` in `config/config.exs`) diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex @@ -35,7 +35,7 @@ defmodule Mix.Tasks.Pleroma.Instance do listen_ip: :string, listen_port: :string, strip_uploads: :string, - read_uploads_data: :string, + read_uploads_description: :string, anonymize_uploads: :string, dedupe_uploads: :string ], @@ -179,7 +179,7 @@ defmodule Mix.Tasks.Pleroma.Instance do strip_uploads_default ) === "y" - {read_uploads_data_message, read_uploads_data_default} = + {read_uploads_description_message, read_uploads_description_default} = if Pleroma.Utils.command_available?("exiftool") do {"Do you want to read data from uploaded files so clients can use it to prefill fields like image description? This requires exiftool, it was detected as installed. (y/n)", "y"} @@ -188,12 +188,12 @@ defmodule Mix.Tasks.Pleroma.Instance do "n"} end - read_uploads_data = + read_uploads_description = get_option( options, - :read_uploads_data, - read_uploads_data_message, - read_uploads_data_default + :read_uploads_description, + read_uploads_description_message, + read_uploads_description_default ) === "y" anonymize_uploads = @@ -248,7 +248,7 @@ defmodule Mix.Tasks.Pleroma.Instance do upload_filters: upload_filters(%{ strip: strip_uploads, - read_data: read_uploads_data, + read_description: read_uploads_description, anonymize: anonymize_uploads, dedupe: dedupe_uploads }) @@ -323,8 +323,8 @@ defmodule Mix.Tasks.Pleroma.Instance do end enabled_filters = - if filters.read_data do - enabled_filters ++ [Pleroma.Upload.Filter.ExiftoolReadData] + if filters.read_description do + enabled_filters ++ [Pleroma.Upload.Filter.Exiftool.ReadDescription] else enabled_filters end diff --git a/lib/pleroma/application_requirements.ex b/lib/pleroma/application_requirements.ex @@ -165,7 +165,7 @@ defmodule Pleroma.ApplicationRequirements do defp check_system_commands!(:ok) do filter_commands_statuses = [ check_filter(Pleroma.Upload.Filter.Exiftool, "exiftool"), - check_filter(Pleroma.Upload.Filter.ExiftoolReadData, "exiftool"), + check_filter(Pleroma.Upload.Filter.Exiftool.ReadDescription, "exiftool"), check_filter(Pleroma.Upload.Filter.Mogrify, "mogrify"), check_filter(Pleroma.Upload.Filter.Mogrifun, "mogrify"), check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "mogrify"), diff --git a/lib/pleroma/upload/filter/exiftool.ex b/lib/pleroma/upload/filter/exiftool/exiftool.ex diff --git a/lib/pleroma/upload/filter/exiftool/read_description.ex b/lib/pleroma/upload/filter/exiftool/read_description.ex @@ -0,0 +1,47 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Upload.Filter.Exiftool.ReadDescription do + @moduledoc """ + Gets the description from the related EXIF tags and provides them in the response if no description is provided yet. + It will first check ImageDescription, when that's too long or empty, it will check iptc:Caption-Abstract. + When the description is too long (see `:instance, :description_limit`), an empty string is returned. + """ + @behaviour Pleroma.Upload.Filter + + @spec filter(Pleroma.Upload.t()) :: {:ok, any()} | {:error, String.t()} + + def filter(%Pleroma.Upload{description: description}) + when is_binary(description), + do: {:ok, :noop} + + def filter(%Pleroma.Upload{tempfile: file} = upload), + do: {:ok, :filtered, upload |> Map.put(:description, read_description_from_exif_data(file))} + + def filter(_, _), do: {:ok, :noop} + + defp read_description_from_exif_data(file) do + nil + |> read_when_empty(file, "-ImageDescription") + |> read_when_empty(file, "-iptc:Caption-Abstract") + end + + defp read_when_empty(current_description, _, _) when is_binary(current_description), + do: current_description + + defp read_when_empty(_, file, tag) do + try do + {tag_content, 0} = + System.cmd("exiftool", ["-b", "-s3", tag, file], stderr_to_stdout: true, parallelism: true) + + if tag_content != "" and + String.length(tag_content) <= + Pleroma.Config.get([:instance, :description_limit]), + do: tag_content, + else: nil + rescue + _ in ErlangError -> nil + end + end +end diff --git a/lib/pleroma/upload/filter/exiftool_read_data.ex b/lib/pleroma/upload/filter/exiftool_read_data.ex @@ -1,47 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Upload.Filter.ExiftoolReadData do - @moduledoc """ - Gets the description from the related EXIF tags and provides them in the response if no description is provided yet. - It will first check ImageDescription, when that's too long or empty, it will check iptc:Caption-Abstract. - When the description is too long (see `:instance, :description_limit`), an empty string is returned. - """ - @behaviour Pleroma.Upload.Filter - - @spec filter(Pleroma.Upload.t()) :: {:ok, any()} | {:error, String.t()} - - def filter(%Pleroma.Upload{description: description}) - when is_binary(description), - do: {:ok, :noop} - - def filter(%Pleroma.Upload{tempfile: file} = upload), - do: {:ok, :filtered, upload |> Map.put(:description, read_description_from_exif_data(file))} - - def filter(_, _), do: {:ok, :noop} - - defp read_description_from_exif_data(file) do - nil - |> read_when_empty(file, "-ImageDescription") - |> read_when_empty(file, "-iptc:Caption-Abstract") - end - - defp read_when_empty(current_description, _, _) when is_binary(current_description), - do: current_description - - defp read_when_empty(_, file, tag) do - try do - {tag_content, 0} = - System.cmd("exiftool", ["-b", "-s3", tag, file], stderr_to_stdout: true, parallelism: true) - - if tag_content != "" and - String.length(tag_content) <= - Pleroma.Config.get([:instance, :description_limit]), - do: tag_content, - else: nil - rescue - _ in ErlangError -> nil - end - end -end diff --git a/test/mix/tasks/pleroma/instance_test.exs b/test/mix/tasks/pleroma/instance_test.exs @@ -69,7 +69,7 @@ defmodule Mix.Tasks.Pleroma.InstanceTest do "./test/../test/instance/static/", "--strip-uploads", "y", - "--read-uploads-data", + "--read-uploads-description", "y", "--dedupe-uploads", "n", @@ -95,7 +95,7 @@ defmodule Mix.Tasks.Pleroma.InstanceTest do assert generated_config =~ "http: [ip: {127, 0, 0, 1}, port: 4000]" assert generated_config =~ - "filters: [Pleroma.Upload.Filter.Exiftool, Pleroma.Upload.Filter.ExiftoolReadData]" + "filters: [Pleroma.Upload.Filter.Exiftool, Pleroma.Upload.Filter.Exiftool.ReadDescription]" assert File.read!(tmp_path() <> "setup.psql") == generated_setup_psql() assert File.exists?(Path.expand("./test/instance/static/robots.txt")) diff --git a/test/pleroma/upload/filter/exiftool_test.exs b/test/pleroma/upload/filter/exiftool/exiftool_test.exs diff --git a/test/pleroma/upload/filter/exiftool/read_description_test.exs b/test/pleroma/upload/filter/exiftool/read_description_test.exs @@ -0,0 +1,106 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Upload.Filter.Exiftool.ReadDescriptionTest do + use Pleroma.DataCase, async: true + alias Pleroma.Upload.Filter + + @uploads %Pleroma.Upload{ + name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", + content_type: "image/jpeg", + path: + Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), + tempfile: + Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg"), + description: nil + } + + test "keeps description when not empty" do + uploads = %Pleroma.Upload{ + name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", + content_type: "image/jpeg", + path: + Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), + tempfile: + Path.absname( + "test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg" + ), + description: "Eight different owls" + } + + assert Filter.Exiftool.ReadDescription.filter(uploads) == + {:ok, :noop} + end + + test "otherwise returns ImageDescription when present" do + uploads_after = %Pleroma.Upload{ + name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", + content_type: "image/jpeg", + path: + Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), + tempfile: + Path.absname( + "test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg" + ), + description: "Pictures of eight different owls" + } + + assert Filter.Exiftool.ReadDescription.filter(@uploads) == + {:ok, :filtered, uploads_after} + end + + test "otherwise returns iptc:Caption-Abstract when present" do + upload = %Pleroma.Upload{ + name: "portrait_of_owls_caption-abstract.jpg", + content_type: "image/jpeg", + path: Path.absname("test/fixtures/portrait_of_owls_caption-abstract.jpg"), + tempfile: Path.absname("test/fixtures/portrait_of_owls_caption-abstract_tmp.jpg"), + description: nil + } + + upload_after = %Pleroma.Upload{ + name: "portrait_of_owls_caption-abstract.jpg", + content_type: "image/jpeg", + path: Path.absname("test/fixtures/portrait_of_owls_caption-abstract.jpg"), + tempfile: Path.absname("test/fixtures/portrait_of_owls_caption-abstract_tmp.jpg"), + description: "Pictures of eight different owls - iptc" + } + + assert Filter.Exiftool.ReadDescription.filter(upload) == + {:ok, :filtered, upload_after} + end + + test "otherwise returns nil" do + uploads = %Pleroma.Upload{ + name: "portrait_of_owls_no_description-abstract.jpg", + content_type: "image/jpeg", + path: Path.absname("test/fixtures/portrait_of_owls_no_description.jpg"), + tempfile: Path.absname("test/fixtures/portrait_of_owls_no_description_tmp.jpg"), + description: nil + } + + assert Filter.Exiftool.ReadDescription.filter(uploads) == + {:ok, :filtered, uploads} + end + + test "Return nil when image description from EXIF data exceeds the maximum length" do + clear_config([:instance, :description_limit], 5) + + assert Filter.Exiftool.ReadDescription.filter(@uploads) == + {:ok, :filtered, @uploads} + end + + test "Return nil when image description from EXIF data can't be read" do + uploads = %Pleroma.Upload{ + name: "non-existant.jpg", + content_type: "image/jpeg", + path: Path.absname("test/fixtures/non-existant.jpg"), + tempfile: Path.absname("test/fixtures/non-existant_tmp.jpg"), + description: nil + } + + assert Filter.Exiftool.ReadDescription.filter(uploads) == + {:ok, :filtered, uploads} + end +end diff --git a/test/pleroma/upload/filter/exiftool_read_data_test.exs b/test/pleroma/upload/filter/exiftool_read_data_test.exs @@ -1,106 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Upload.Filter.ExiftoolReadDataTest do - use Pleroma.DataCase, async: true - alias Pleroma.Upload.Filter - - @uploads %Pleroma.Upload{ - name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", - content_type: "image/jpeg", - path: - Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), - tempfile: - Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg"), - description: nil - } - - test "keeps description when not empty" do - uploads = %Pleroma.Upload{ - name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", - content_type: "image/jpeg", - path: - Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), - tempfile: - Path.absname( - "test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg" - ), - description: "Eight different owls" - } - - assert Filter.ExiftoolReadData.filter(uploads) == - {:ok, :noop} - end - - test "otherwise returns ImageDescription when present" do - uploads_after = %Pleroma.Upload{ - name: "portrait_of_owls_imagedescription_and_caption-abstract.jpg", - content_type: "image/jpeg", - path: - Path.absname("test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract.jpg"), - tempfile: - Path.absname( - "test/fixtures/portrait_of_owls_imagedescription_and_caption-abstract_tmp.jpg" - ), - description: "Pictures of eight different owls" - } - - assert Filter.ExiftoolReadData.filter(@uploads) == - {:ok, :filtered, uploads_after} - end - - test "otherwise returns iptc:Caption-Abstract when present" do - upload = %Pleroma.Upload{ - name: "portrait_of_owls_caption-abstract.jpg", - content_type: "image/jpeg", - path: Path.absname("test/fixtures/portrait_of_owls_caption-abstract.jpg"), - tempfile: Path.absname("test/fixtures/portrait_of_owls_caption-abstract_tmp.jpg"), - description: nil - } - - upload_after = %Pleroma.Upload{ - name: "portrait_of_owls_caption-abstract.jpg", - content_type: "image/jpeg", - path: Path.absname("test/fixtures/portrait_of_owls_caption-abstract.jpg"), - tempfile: Path.absname("test/fixtures/portrait_of_owls_caption-abstract_tmp.jpg"), - description: "Pictures of eight different owls - iptc" - } - - assert Filter.ExiftoolReadData.filter(upload) == - {:ok, :filtered, upload_after} - end - - test "otherwise returns nil" do - uploads = %Pleroma.Upload{ - name: "portrait_of_owls_no_description-abstract.jpg", - content_type: "image/jpeg", - path: Path.absname("test/fixtures/portrait_of_owls_no_description.jpg"), - tempfile: Path.absname("test/fixtures/portrait_of_owls_no_description_tmp.jpg"), - description: nil - } - - assert Filter.ExiftoolReadData.filter(uploads) == - {:ok, :filtered, uploads} - end - - test "Return nil when image description from EXIF data exceeds the maximum length" do - clear_config([:instance, :description_limit], 5) - - assert Filter.ExiftoolReadData.filter(@uploads) == - {:ok, :filtered, @uploads} - end - - test "Return nil when image description from EXIF data can't be read" do - uploads = %Pleroma.Upload{ - name: "non-existant.jpg", - content_type: "image/jpeg", - path: Path.absname("test/fixtures/non-existant.jpg"), - tempfile: Path.absname("test/fixtures/non-existant_tmp.jpg"), - description: nil - } - - assert Filter.ExiftoolReadData.filter(uploads) == - {:ok, :filtered, uploads} - end -end