mirror of
https://github.com/nextcloud/android.git
synced 2024-11-21 20:55:31 +03:00
Merge pull request #13968 from nextcloud/bugfix/auto-rename-edge-cases
BugFix - AutoRename Edge Cases
This commit is contained in:
commit
5cce0ebfd4
2 changed files with 44 additions and 7 deletions
|
@ -167,4 +167,12 @@ class AutoRenameTests : AbstractOnServerIT() {
|
||||||
val expectedFolderName = "/COm02/2569.webp"
|
val expectedFolderName = "/COm02/2569.webp"
|
||||||
assert(result == expectedFolderName) { "Expected $expectedFolderName but got $result" }
|
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" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ object AutoRename {
|
||||||
|
|
||||||
forbiddenFilenameExtensions.find { it == StringConstants.DOT }?.let { forbiddenExtension ->
|
forbiddenFilenameExtensions.find { it == StringConstants.DOT }?.let { forbiddenExtension ->
|
||||||
pathSegments.replaceAll { segment ->
|
pathSegments.replaceAll { segment ->
|
||||||
replacePathSegment(forbiddenExtension, segment)
|
replaceDots(forbiddenExtension, segment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,9 +67,13 @@ object AutoRename {
|
||||||
.filter { it != StringConstants.SPACE && it != StringConstants.DOT }
|
.filter { it != StringConstants.SPACE && it != StringConstants.DOT }
|
||||||
.forEach { forbiddenExtension ->
|
.forEach { forbiddenExtension ->
|
||||||
pathSegments.replaceAll { segment ->
|
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 {
|
private fun lowercaseFileExtension(input: String): String {
|
||||||
return if (segment.endsWith(forbiddenExtension, ignoreCase = true) ||
|
val lastDotIndex = input.lastIndexOf('.')
|
||||||
segment.startsWith(forbiddenExtension, ignoreCase = true)
|
|
||||||
) {
|
return if (lastDotIndex > 0) {
|
||||||
segment.replace(forbiddenExtension, REPLACEMENT)
|
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 {
|
} else {
|
||||||
segment
|
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 {
|
private fun convertToUTF8(filename: String): String {
|
||||||
return String(filename.toByteArray(), Charsets.UTF_8)
|
return String(filename.toByteArray(), Charsets.UTF_8)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue