Skip to content

Commit

Permalink
fix trimming issue
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 18, 2024
1 parent 780c810 commit deec144
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
25 changes: 25 additions & 0 deletions app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
41 changes: 15 additions & 26 deletions app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit deec144

Please sign in to comment.