diff --git a/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt b/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt index 57abe6362b..d3b21d4bb2 100644 --- a/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt +++ b/app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt @@ -167,4 +167,12 @@ class AutoRenameTests : AbstractOnServerIT() { val expectedFolderName = "/COm02/2569.webp" assert(result == expectedFolderName) { "Expected $expectedFolderName but got $result" } } + + @Test + fun testValidFilename() { + val filename = ".file.TXT" + val result = AutoRename.rename(filename, capability) + val expectedFilename = "_file.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 33a63bbe92..5f3260a3c2 100644 --- a/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt +++ b/app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt @@ -59,7 +59,7 @@ object AutoRename { forbiddenFilenameExtensions.find { it == StringConstants.DOT }?.let { forbiddenExtension -> pathSegments.replaceAll { segment -> - replacePathSegment(forbiddenExtension, segment) + replaceDots(forbiddenExtension, segment) } } @@ -67,9 +67,13 @@ object AutoRename { .filter { it != StringConstants.SPACE && it != StringConstants.DOT } .forEach { forbiddenExtension -> pathSegments.replaceAll { segment -> - replacePathSegment(forbiddenExtension, segment) + replaceFileExtensions(forbiddenExtension, segment) } } + + pathSegments.replaceAll { segment -> + lowercaseFileExtension(segment) + } } } @@ -82,16 +86,41 @@ object AutoRename { } } - private fun replacePathSegment(forbiddenExtension: String, segment: String): String { - return if (segment.endsWith(forbiddenExtension, ignoreCase = true) || - segment.startsWith(forbiddenExtension, ignoreCase = true) - ) { - segment.replace(forbiddenExtension, REPLACEMENT) + private fun lowercaseFileExtension(input: String): String { + val lastDotIndex = input.lastIndexOf('.') + + return if (lastDotIndex > 0) { + val base = input.substring(0, lastDotIndex) + val extension = input.substring(lastDotIndex + 1).lowercase() // Convert extension to lowercase + + "$base.$extension" + } else { + input + } + } + + private fun replaceDots(forbiddenExtension: String, segment: String): String { + return if (isSegmentContainsForbiddenExtension(forbiddenExtension, segment)) { + segment.replaceFirst(forbiddenExtension, REPLACEMENT) } else { segment } } + private fun replaceFileExtensions(forbiddenExtension: String, segment: String): String { + return if (isSegmentContainsForbiddenExtension(forbiddenExtension, segment)) { + val newExtension = forbiddenExtension.replace(StringConstants.DOT, REPLACEMENT, ignoreCase = true) + segment.replace(forbiddenExtension, newExtension.lowercase(), ignoreCase = true) + } else { + segment + } + } + + private fun isSegmentContainsForbiddenExtension(forbiddenExtension: String, segment: String): Boolean { + return segment.endsWith(forbiddenExtension, ignoreCase = true) || + segment.startsWith(forbiddenExtension, ignoreCase = true) + } + private fun convertToUTF8(filename: String): String { return String(filename.toByteArray(), Charsets.UTF_8) }