[#1026] Filter.AnonymizeFilename added ability to retain file extension with custom...
authorMaksim <parallel588@gmail.com>
Sun, 30 Jun 2019 07:28:35 +0000 (07:28 +0000)
committerrinpatch <rinpatch@sdf.org>
Sun, 30 Jun 2019 07:28:35 +0000 (07:28 +0000)
CHANGELOG.md
docs/config.md
lib/pleroma/config.ex
lib/pleroma/upload/filter/anonymize_filename.ex
test/upload/filter/anonymize_filename_test.exs [new file with mode: 0644]

index e30ab803cd5912b9a8f1d1e2dd5d0f3ff4057ffc..96473b1b8d527fabe3546ff6e943351268d49b20 100644 (file)
@@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 ### Fixed
 - Not being able to pin unlisted posts
 
+### Changed
+- Configuration: Filter.AnonymizeFilename added ability to retain file extension with custom text
+
 ## [1.0.0] - 2019-06-29
 ### Security
 - Mastodon API: Fix display names not being sanitized
index feef43ba9c75564988dccc88930c41a73f695cba..8afccb228d7ca9991fd4998d365a2b71102eada8 100644 (file)
@@ -36,7 +36,7 @@ No specific configuration.
 This filter replaces the filename (not the path) of an upload. For complete obfuscation, add
 `Pleroma.Upload.Filter.Dedupe` before AnonymizeFilename.
 
-* `text`: Text to replace filenames in links. If empty, `{random}.extension` will be used.
+* `text`: Text to replace filenames in links. If empty, `{random}.extension` will be used. You can get the original filename extension by using `{extension}`, for example `custom-file-name.{extension}`.
 
 ## Pleroma.Emails.Mailer
 * `adapter`: one of the mail adapters listed in [Swoosh readme](https://github.com/swoosh/swoosh#adapters), or `Swoosh.Adapters.Local` for in-memory mailbox.
index 71a47b9fb99615db9e837d9edb08216e47811fe6..fcc0397101368e26d3eb22c5734160eaa017a205 100644 (file)
@@ -38,7 +38,7 @@ defmodule Pleroma.Config do
 
   def put([parent_key | keys], value) do
     parent =
-      Application.get_env(:pleroma, parent_key)
+      Application.get_env(:pleroma, parent_key, [])
       |> put_in(keys, value)
 
     Application.put_env(:pleroma, parent_key, parent)
index 5ca53a79ba46ac7609a24e2e00ffc74c47bcec0d..a8516811ce666bb53d4d2c556892d0c6ca626cc0 100644 (file)
@@ -10,10 +10,19 @@ defmodule Pleroma.Upload.Filter.AnonymizeFilename do
   """
   @behaviour Pleroma.Upload.Filter
 
-  def filter(upload) do
-    extension = List.last(String.split(upload.name, "."))
-    name = Pleroma.Config.get([__MODULE__, :text], random(extension))
-    {:ok, %Pleroma.Upload{upload | name: name}}
+  alias Pleroma.Config
+  alias Pleroma.Upload
+
+  def filter(%Upload{name: name} = upload) do
+    extension = List.last(String.split(name, "."))
+    name = predefined_name(extension) || random(extension)
+    {:ok, %Upload{upload | name: name}}
+  end
+
+  @spec predefined_name(String.t()) :: String.t() | nil
+  defp predefined_name(extension) do
+    with name when not is_nil(name) <- Config.get([__MODULE__, :text]),
+         do: String.replace(name, "{extension}", extension)
   end
 
   defp random(extension) do
diff --git a/test/upload/filter/anonymize_filename_test.exs b/test/upload/filter/anonymize_filename_test.exs
new file mode 100644 (file)
index 0000000..02241cf
--- /dev/null
@@ -0,0 +1,40 @@
+defmodule Pleroma.Upload.Filter.AnonymizeFilenameTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.Config
+  alias Pleroma.Upload
+
+  setup do
+    custom_filename = Config.get([Upload.Filter.AnonymizeFilename, :text])
+
+    on_exit(fn ->
+      Config.put([Upload.Filter.AnonymizeFilename, :text], custom_filename)
+    end)
+
+    upload_file = %Upload{
+      name: "an… image.jpg",
+      content_type: "image/jpg",
+      path: Path.absname("test/fixtures/image_tmp.jpg")
+    }
+
+    %{upload_file: upload_file}
+  end
+
+  test "it replaces filename on pre-defined text", %{upload_file: upload_file} do
+    Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.png")
+    {:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
+    assert name == "custom-file.png"
+  end
+
+  test "it replaces filename on pre-defined text expression", %{upload_file: upload_file} do
+    Config.put([Upload.Filter.AnonymizeFilename, :text], "custom-file.{extension}")
+    {:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
+    assert name == "custom-file.jpg"
+  end
+
+  test "it replaces filename on random text", %{upload_file: upload_file} do
+    {:ok, %Upload{name: name}} = Upload.Filter.AnonymizeFilename.filter(upload_file)
+    assert <<_::bytes-size(14)>> <> ".jpg" = name
+    refute name == "an… image.jpg"
+  end
+end