diff options
author | Roman Chvanikov <chvanikoff@pm.me> | 2020-05-22 18:19:25 +0300 |
---|---|---|
committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-05-22 18:19:25 +0300 |
commit | cc82229ba70e054acfdea07a195c1b11961ea1bc (patch) | |
tree | 5ed800c47802126dade4e45b39f083e2e00a8301 | |
parent | 7b02bfca51f95f56a5d12724b80b16019507cce9 (diff) | |
download | pleroma-cc82229ba70e054acfdea07a195c1b11961ea1bc.tar.gz |
Add filename_display_max_length config
-rw-r--r-- | config/config.exs | 3 | ||||
-rw-r--r-- | lib/pleroma/web/common_api/utils.ex | 8 | ||||
-rw-r--r-- | test/web/common_api/common_api_utils_test.exs | 41 |
3 files changed, 39 insertions, 13 deletions
diff --git a/config/config.exs b/config/config.exs index 1b11b4fa9..7385fb6c3 100644 --- a/config/config.exs +++ b/config/config.exs @@ -71,7 +71,8 @@ config :pleroma, Pleroma.Upload, follow_redirect: true, pool: :upload ] - ] + ], + filename_display_max_length: 30 config :pleroma, Pleroma.Uploaders.Local, uploads: "uploads" diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index b9fa21648..7be9d8caa 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -396,10 +396,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do def to_masto_date(_), do: "" defp shortname(name) do - if String.length(name) < 30 do - name + with max_length when max_length > 0 <- + Pleroma.Config.get([Pleroma.Upload, :filename_display_max_length], 30), + true <- String.length(name) > max_length do + String.slice(name, 0..max_length) <> "…" else - String.slice(name, 0..30) <> "…" + _ -> name end end diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index d7d2d10d5..e67c10b93 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -14,18 +14,41 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do @public_address "https://www.w3.org/ns/activitystreams#Public" - test "it adds attachment links to a given text and attachment set" do - name = - "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png" + describe "add_attachments/2" do + setup do + name = + "Sakura Mana – Turned on by a Senior OL with a Temptating Tight Skirt-s Full Hipline and Panty Shot- Beautiful Thick Thighs- and Erotic Ass- -2015- -- Oppaitime 8-28-2017 6-50-33 PM.png" - attachment = %{ - "url" => [%{"href" => name}] - } + attachment = %{ + "url" => [%{"href" => URI.encode(name)}] + } - res = Utils.add_attachments("", [attachment]) + %{name: name, attachment: attachment} + end + + test "it adds attachment links to a given text and attachment set", %{ + name: name, + attachment: attachment + } do + len = 10 + clear_config([Pleroma.Upload, :filename_display_max_length], len) - assert res == - "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>" + expected = + "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>" + + assert Utils.add_attachments("", [attachment]) == expected + end + + test "doesn't truncate file name if config for truncate is set to 0", %{ + name: name, + attachment: attachment + } do + clear_config([Pleroma.Upload, :filename_display_max_length], 0) + + expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>" + + assert Utils.add_attachments("", [attachment]) == expected + end end describe "it confirms the password given is the current users password" do |