mirror of
https://github.com/nextcloud/android.git
synced 2024-11-25 06:35:48 +03:00
fix trimming issue
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
31f0d434ba
commit
896d159034
2 changed files with 40 additions and 26 deletions
|
@ -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" }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue