mirror of
https://github.com/nextcloud/android.git
synced 2024-11-22 13:15:35 +03:00
Add auto rename object and tests
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
173f17f6bf
commit
c06052a49b
2 changed files with 92 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue