From 896d159034f4f55640d4d745a3a62bc02c5393da Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 18 Sep 2024 09:40:17 +0200 Subject: [PATCH] fix trimming issue Signed-off-by: alperozturk --- .../com/nextcloud/utils/AutoRenameTests.kt | 25 +++++++++++ .../nextcloud/utils/autoRename/AutoRename.kt | 41 +++++++------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt b/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt index 2a1de22b54..59ad1a02f2 100644 --- a/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt +++ b/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt @@ -46,4 +46,29 @@ class AutoRenameTests: AbstractOnServerIT() { val expectedFilename = "file_" assert(result == expectedFilename) } + + + @Test + fun testRenameWhenMultipleInvalidCharactersShouldReturnValidFilename() { + val filename = "file|name?<>.txt" + val result = AutoRename.rename(filename, capability) + val expectedFilename = "file_name___.txt" + assert(result == expectedFilename) { "Expected $expectedFilename but got $result" } + } + + @Test + fun testRenameWhenFilenameStartsAndEndsWithInvalidExtensionsShouldReturnValidFilename() { + val filename = " .file.part " + val result = AutoRename.rename(filename, capability) + val expectedFilename = "_file_part" + assert(result == expectedFilename) { "Expected $expectedFilename but got $result" } + } + + @Test + fun testRenameWhenNonPrintableCharactersArePresentShouldRemoveThem() { + val filename = "file\u0001name.txt" + val result = AutoRename.rename(filename, capability) + val expectedFilename = "filename.txt" + assert(result == expectedFilename) { "Expected $expectedFilename but got $result" } + } } diff --git a/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt b/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt index 151d01ef54..e3e48d61e8 100644 --- a/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt +++ b/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt @@ -18,54 +18,43 @@ object AutoRename { private const val REPLACEMENT = "_" fun rename(filename: String, capability: OCCapability): String { + var result = filename + capability.run { forbiddenFilenameCharactersJson?.let { val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters() forbiddenFilenameCharacters.forEach { - if (filename.lowercase().contains(it)) { - val result = filename.replace(it, REPLACEMENT) - return if (shouldRemoveNonPrintableUnicodeCharacters()) { - removeNonPrintableUnicodeCharacters(result) - } else { - result - } + if (result.lowercase().contains(it)) { + result = result.replace(it, REPLACEMENT) } } } forbiddenFilenameExtensionJson?.let { - for (forbiddenExtension in forbiddenFilenameExtension()) { - val result = if (forbiddenExtension == StringConstants.SPACE && - filename.endsWith(forbiddenExtension, ignoreCase = true)) { - filename.trimEnd() - } else if (forbiddenExtension == StringConstants.SPACE && - filename.startsWith(forbiddenExtension, ignoreCase = true)) { - filename.trimStart() - } else if (filename.endsWith(forbiddenExtension, ignoreCase = true) || - filename.startsWith(forbiddenExtension, ignoreCase = true)) { - filename.replace(forbiddenExtension, REPLACEMENT) - } else { - filename + forbiddenFilenameExtension().any { forbiddenExtension -> + if (forbiddenExtension == StringConstants.SPACE) { + result = result.trimStart().trimEnd() } - return if (shouldRemoveNonPrintableUnicodeCharacters()) { - removeNonPrintableUnicodeCharacters(result) - } else { - result + if (result.endsWith(forbiddenExtension, ignoreCase = true) || + result.startsWith(forbiddenExtension, ignoreCase = true)) { + result = result.replace(forbiddenExtension, REPLACEMENT) } + + false } } } return if (capability.shouldRemoveNonPrintableUnicodeCharacters()) { - removeNonPrintableUnicodeCharacters(filename) + removeNonPrintableUnicodeCharacters(result) } else { - filename + result } } - fun removeNonPrintableUnicodeCharacters(filename: String): String { + private fun removeNonPrintableUnicodeCharacters(filename: String): String { val regex = "\\p{C}" val pattern = Pattern.compile(regex) val matcher = pattern.matcher(filename)