Add auto rename object and tests

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-09-17 10:51:59 +02:00 committed by Alper Öztürk
parent 173f17f6bf
commit c06052a49b
2 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,49 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.utils
import com.nextcloud.utils.autoRename.AutoRename
import com.nextcloud.utils.extensions.forbiddenFilenameCharacters
import com.nextcloud.utils.extensions.forbiddenFilenameExtension
import com.owncloud.android.AbstractOnServerIT
import com.owncloud.android.lib.resources.status.OCCapability
import org.junit.Before
import org.junit.Test
class AutoRenameTests: AbstractOnServerIT() {
private var capability: OCCapability = fileDataStorageManager.getCapability(account.name)
@Before
fun setup() {
capability = capability.apply {
forbiddenFilenameExtensionJson = """[" ",".",".part",".part"]"""
forbiddenFilenameCharactersJson = """["<", ">", ":", "\\\\", "/", "|", "?", "*", "&"]"""
}
}
@Test
fun testRenameWhenInvalidCharacterIncludedShouldReturnValidFilename() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()
val filename = "file${randomInvalidChar}file.txt"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "file_file.txt"
assert(result == expectedFilename)
}
@Test
fun testRenameWhenInvalidFilenameExtensionIncludedShouldReturnValidFilename() {
val forbiddenFilenameExtension = capability.forbiddenFilenameExtension()
val randomInvalidFilenameExtension = forbiddenFilenameExtension.random()
val filename = "file${randomInvalidFilenameExtension}"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "file_"
assert(result == expectedFilename)
}
}

View file

@ -0,0 +1,43 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.utils.autoRename
import com.nextcloud.utils.extensions.forbiddenFilenameCharacters
import com.nextcloud.utils.extensions.forbiddenFilenameExtension
import com.owncloud.android.lib.resources.status.OCCapability
object AutoRename {
private const val REPLACEMENT = "_"
fun rename(filename: String, capability: OCCapability): String {
val result = filename.lowercase()
capability.run {
forbiddenFilenameCharactersJson?.let {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
forbiddenFilenameCharacters.forEach {
if (result.contains(it)) {
return result.replace(it, REPLACEMENT)
}
}
}
forbiddenFilenameExtensionJson?.let {
for (forbiddenExtension in forbiddenFilenameExtension()) {
if (filename.endsWith(forbiddenExtension, ignoreCase = true) ||
filename.startsWith(forbiddenExtension, ignoreCase = true)) {
return filename.replace(forbiddenExtension, REPLACEMENT)
}
}
}
}
return result
}
}