Skip to content

Commit

Permalink
removeNonPrintableUnicodeCharacters
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 17, 2024
1 parent d441cc3 commit 780c810
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
64 changes: 25 additions & 39 deletions app/src/main/java/com/nextcloud/utils/autoRename/AutoRename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ package com.nextcloud.utils.autoRename
import com.nextcloud.utils.extensions.StringConstants
import com.nextcloud.utils.extensions.forbiddenFilenameCharacters
import com.nextcloud.utils.extensions.forbiddenFilenameExtension
import com.owncloud.android.lib.common.utils.Log_OC
import com.nextcloud.utils.extensions.shouldRemoveNonPrintableUnicodeCharacters
import com.owncloud.android.lib.resources.status.OCCapability
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException
import java.util.regex.Pattern

object AutoRename {
private const val REPLACEMENT = "_"
private const val TAG = "AutoRename"
private val scope = CoroutineScope(Dispatchers.IO)

fun rename(filename: String, capability: OCCapability): String {
capability.run {
Expand All @@ -30,14 +24,19 @@ object AutoRename {

forbiddenFilenameCharacters.forEach {
if (filename.lowercase().contains(it)) {
return filename.replace(it, REPLACEMENT)
val result = filename.replace(it, REPLACEMENT)
return if (shouldRemoveNonPrintableUnicodeCharacters()) {
removeNonPrintableUnicodeCharacters(result)
} else {
result
}
}
}
}

forbiddenFilenameExtensionJson?.let {
for (forbiddenExtension in forbiddenFilenameExtension()) {
return if (forbiddenExtension == StringConstants.SPACE &&
val result = if (forbiddenExtension == StringConstants.SPACE &&
filename.endsWith(forbiddenExtension, ignoreCase = true)) {
filename.trimEnd()
} else if (forbiddenExtension == StringConstants.SPACE &&
Expand All @@ -49,40 +48,27 @@ object AutoRename {
} else {
filename
}
}
}
}

return filename
}

fun renameFiles(paths: Array<String>, capability: OCCapability, onComplete: (Array<String>) -> Unit) {
scope.launch {
val result = paths.map { path ->
val file = File(path)

if (file.exists()) {
val newFilename = rename(file.name, capability)
val newFile = File(file.parentFile, newFilename)

if (file.renameTo(newFile)) {
try {
newFile.path
} catch (e: IOException) {
Log_OC.e(TAG, "Exception caught during renameFiles(): $e")
path
}
return if (shouldRemoveNonPrintableUnicodeCharacters()) {
removeNonPrintableUnicodeCharacters(result)
} else {
path
result
}
} else {
path
}
}.toTypedArray()

launch(Dispatchers.Main) {
onComplete(result)
}
}

return if (capability.shouldRemoveNonPrintableUnicodeCharacters()) {
removeNonPrintableUnicodeCharacters(filename)
} else {
filename
}
}

fun removeNonPrintableUnicodeCharacters(filename: String): String {
val regex = "\\p{C}"
val pattern = Pattern.compile(regex)
val matcher = pattern.matcher(filename)
return matcher.replaceAll("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ fun OCCapability.forbiddenFilenameExtension(): List<String> = jsonToList(forbidd

fun OCCapability.forbiddenFilenameBaseNames(): List<String> = jsonToList(forbiddenFilenameBaseNamesJson)

fun OCCapability.shouldRemoveNonPrintableUnicodeCharacters(): Boolean {
return forbiddenFilenames().isNotEmpty() ||
forbiddenFilenameCharacters().isNotEmpty() ||
forbiddenFilenameExtension().isNotEmpty() ||
forbiddenFilenameBaseNames().isNotEmpty()
}

@Suppress("ReturnCount")
private fun jsonToList(json: String?): List<String> {
if (json == null) return emptyList()
Expand Down

0 comments on commit 780c810

Please sign in to comment.