Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix - AutoRename Edge Cases #13968

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
43 changes: 36 additions & 7 deletions app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ object AutoRename {

forbiddenFilenameExtensions.find { it == StringConstants.DOT }?.let { forbiddenExtension ->
pathSegments.replaceAll { segment ->
replacePathSegment(forbiddenExtension, segment)
replaceDots(forbiddenExtension, segment)
}
}

forbiddenFilenameExtensions
.filter { it != StringConstants.SPACE && it != StringConstants.DOT }
.forEach { forbiddenExtension ->
pathSegments.replaceAll { segment ->
replacePathSegment(forbiddenExtension, segment)
replaceFileExtensions(forbiddenExtension, segment)
}
}

pathSegments.replaceAll { segment ->
lowercaseFileExtension(segment)
}
}
}

Expand All @@ -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)
}
Expand Down
Loading