Эх сурвалжийг харах

Refactor current logic

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
59683a25e1

+ 17 - 17
app/src/androidTest/java/com/nextcloud/utils/FileNameValidatorTests.kt

@@ -34,41 +34,41 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testInvalidCharacter() {
-        val result = FileNameValidator.isValid("file<name", capability, targetContext)
+        val result = FileNameValidator.checkFileName("file<name", capability, targetContext)
         assertEquals("File name contains invalid characters: <", result)
     }
 
     @Test
     fun testReservedName() {
-        val result = FileNameValidator.isValid("CON", capability, targetContext)
+        val result = FileNameValidator.checkFileName("CON", capability, targetContext)
         assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
     }
 
     @Test
     fun testEndsWithSpaceOrPeriod() {
-        val result = FileNameValidator.isValid("filename ", capability, targetContext)
+        val result = FileNameValidator.checkFileName("filename ", capability, targetContext)
         assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)
 
-        val result2 = FileNameValidator.isValid("filename.", capability, targetContext)
+        val result2 = FileNameValidator.checkFileName("filename.", capability, targetContext)
         assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)
     }
 
     @Test
     fun testEmptyFileName() {
-        val result = FileNameValidator.isValid("", capability, targetContext)
+        val result = FileNameValidator.checkFileName("", capability, targetContext)
         assertEquals(targetContext.getString(R.string.filename_empty), result)
     }
 
     @Test
     fun testFileAlreadyExists() {
         val existingFiles = mutableSetOf("existingFile")
-        val result = FileNameValidator.isValid("existingFile", capability, targetContext, existingFiles)
+        val result = FileNameValidator.checkFileName("existingFile", capability, targetContext, existingFiles)
         assertEquals(targetContext.getString(R.string.file_already_exists), result)
     }
 
     @Test
     fun testValidFileName() {
-        val result = FileNameValidator.isValid("validFileName", capability, targetContext)
+        val result = FileNameValidator.checkFileName("validFileName", capability, targetContext)
         assertNull(result)
     }
 
@@ -90,7 +90,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertTrue(result)
     }
 
@@ -99,7 +99,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "CON"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -108,7 +108,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -117,7 +117,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "invalid<Folder"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -126,7 +126,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "file|2.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -135,7 +135,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "folderWithSpace "
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -144,7 +144,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -153,7 +153,7 @@ class FileNameValidatorTests : AbstractIT() {
         val folderPath = "validFolder\\secondValidFolder\\CON"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.")
 
-        val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
         assertFalse(result)
     }
 
@@ -161,7 +161,7 @@ class FileNameValidatorTests : AbstractIT() {
     fun testOnlyFolderPath() {
         val folderPath = "/A1/Aaaww/W/C2/"
 
-        val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
         assertTrue(result)
     }
 
@@ -169,7 +169,7 @@ class FileNameValidatorTests : AbstractIT() {
     fun testOnlyFolderPathWithOneReservedName() {
         val folderPath = "/A1/Aaaww/CON/W/C2/"
 
-        val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
+        val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
         assertFalse(result)
     }
 }

+ 24 - 25
app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt

@@ -25,35 +25,35 @@ object FileNameValidator {
     )
 
     @Suppress("ReturnCount")
-    fun isValid(
-        name: String,
+    fun checkFileName(
+        filename: String,
         capability: OCCapability,
         context: Context,
-        fileNames: MutableSet<String>? = null
+        existedFileNames: MutableSet<String>? = null
     ): String? {
-        if (TextUtils.isEmpty(name)) {
+        if (TextUtils.isEmpty(filename)) {
             return context.getString(R.string.filename_empty)
         }
 
-        if (isFileNameAlreadyExist(name, fileNames ?: mutableSetOf())) {
+        if (isFileNameAlreadyExist(filename, existedFileNames ?: mutableSetOf())) {
             return context.getString(R.string.file_already_exists)
         }
 
-        if (name.endsWith(" ") || name.endsWith(".")) {
+        if (filename.endsWith(" ") || filename.endsWith(".")) {
             return context.getString(R.string.file_name_validator_error_ends_with_space_period)
         }
 
-        checkInvalidCharacters(name, capability, context)?.let {
+        checkInvalidCharacters(filename, capability, context)?.let {
             return it
         }
 
         if (capability.forbiddenFilenames.isTrue &&
             (
-                reservedWindowsNames.contains(name.uppercase()) ||
-                    reservedWindowsNames.contains(name.removeFileExtension().uppercase())
+                reservedWindowsNames.contains(filename.uppercase()) ||
+                    reservedWindowsNames.contains(filename.removeFileExtension().uppercase())
                 )
         ) {
-            return context.getString(R.string.file_name_validator_error_reserved_names, name.substringBefore("."))
+            return context.getString(R.string.file_name_validator_error_reserved_names, filename.substringBefore("."))
         }
 
         if (capability.forbiddenFilenameExtension.isTrue) {
@@ -63,23 +63,22 @@ object FileNameValidator {
         return null
     }
 
-    @Suppress("ReturnCount")
-    fun checkPath(folderPath: String, filePaths: List<String>, capability: OCCapability, context: Context): Boolean {
-        val folderPaths = folderPath.split("/", "\\").filter { it.isNotEmpty() }
-
-        for (item in folderPaths) {
-            if (isValid(item, capability, context) != null) {
-                return false
-            }
-        }
+    fun checkFolderAndFilePaths(
+        folderPath: String,
+        filePaths: List<String>,
+        capability: OCCapability,
+        context: Context
+    ): Boolean {
+        return checkFolderPath(folderPath, capability, context) && checkFilePaths(filePaths, capability, context)
+    }
 
-        for (item in filePaths) {
-            if (isValid(item, capability, context) != null) {
-                return false
-            }
-        }
+    fun checkFilePaths(filePaths: List<String>, capability: OCCapability, context: Context): Boolean {
+        return filePaths.all { checkFileName(it, capability, context) == null }
+    }
 
-        return true
+    fun checkFolderPath(folderPath: String, capability: OCCapability, context: Context): Boolean {
+        return folderPath.split("[/\\\\]".toRegex())
+            .none { it.isNotEmpty() && checkFileName(it, capability, context) != null }
     }
 
     @Suppress("ReturnCount")

+ 7 - 3
app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt

@@ -455,9 +455,7 @@ open class FolderPickerActivity :
         }
 
         targetFilePaths?.let { filePaths ->
-
-            val isPathValid = FileNameValidator.checkPath(file.remotePath, filePaths, capabilities, this)
-            if (!isPathValid) {
+            if (!isFolderAndFilePathsValid(filePaths)) {
                 DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move)
                 Handler(Looper.getMainLooper()).postDelayed({
                     setResult(RESULT_CANCELED, resultData)
@@ -477,6 +475,12 @@ open class FolderPickerActivity :
         finish()
     }
 
+    private fun isFolderAndFilePathsValid(filePaths: List<String>): Boolean {
+        val isFolderPathValid = FileNameValidator.checkFolderPath(file.remotePath, capabilities, this)
+        val isFilePathsValid = FileNameValidator.checkFilePaths(filePaths, capabilities, this)
+        return isFilePathsValid && isFolderPathValid
+    }
+
     override fun onRemoteOperationFinish(operation: RemoteOperation<*>?, result: RemoteOperationResult<*>) {
         super.onRemoteOperationFinish(operation, result)
         if (operation is CreateFolderOperation) {

+ 1 - 1
app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

@@ -669,7 +669,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
             }
             mUploadPath = stringBuilder.toString();
 
-            boolean isPathValid = FileNameValidator.INSTANCE.checkPath(mUploadPath, new ArrayList<>(), getCapabilities(), this);
+            boolean isPathValid = FileNameValidator.INSTANCE.checkFolderPath(mUploadPath, getCapabilities(), this);
             if (!isPathValid) {
                 DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move);
                 return;

+ 1 - 1
app/src/main/java/com/owncloud/android/ui/activity/UploadFilesActivity.java

@@ -662,7 +662,7 @@ public class UploadFilesActivity extends DrawerActivity implements LocalFileList
     private String checkFileNameBeforeUpload(String[] selectedFilePaths) {
         for (String filePath : selectedFilePaths) {
             File file = new File(filePath);
-            String filenameErrorMessage = FileNameValidator.INSTANCE.isValid(file.getName(), getCapabilities(), this, null);
+            String filenameErrorMessage = FileNameValidator.INSTANCE.checkFileName(file.getName(), getCapabilities(), this, null);
 
             if (filenameErrorMessage != null) {
                 return filenameErrorMessage;

+ 2 - 2
app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt

@@ -224,7 +224,7 @@ class ChooseTemplateDialogFragment : DialogFragment(), View.OnClickListener, Tem
         val path = parentFolder?.remotePath + name
         val selectedTemplate = adapter?.selectedTemplate
 
-        val errorMessage = FileNameValidator.isValid(name, getOCCapability(), requireContext())
+        val errorMessage = FileNameValidator.checkFileName(name, getOCCapability(), requireContext())
 
         when {
             selectedTemplate == null -> {
@@ -256,7 +256,7 @@ class ChooseTemplateDialogFragment : DialogFragment(), View.OnClickListener, Tem
             DOT + selectedTemplate.extension,
             ignoreCase = true
         )
-        val fileNameValidatorResult = FileNameValidator.isValid(name, getOCCapability(), requireContext())
+        val fileNameValidatorResult = FileNameValidator.checkFileName(name, getOCCapability(), requireContext())
 
         val errorMessage = when {
             isNameJustExtension -> null

+ 2 - 2
app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt

@@ -125,7 +125,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
         val newFileName = binding.userInput.text?.toString()?.trim() ?: ""
 
         val fileNameValidatorResult: String? =
-            FileNameValidator.isValid(newFileName, getOCCapability(), requireContext(), fileNames)
+            FileNameValidator.checkFileName(newFileName, getOCCapability(), requireContext(), fileNames)
 
         val errorMessage = when {
             newFileName.isEmpty() -> null
@@ -163,7 +163,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
             val newFolderName = (getDialog()?.findViewById<View>(R.id.user_input) as TextView)
                 .text.toString().trim { it <= ' ' }
 
-            val errorMessage: String? = FileNameValidator.isValid(newFolderName, getOCCapability(), requireContext())
+            val errorMessage: String? = FileNameValidator.checkFileName(newFolderName, getOCCapability(), requireContext())
 
             if (errorMessage != null) {
                 DisplayUtils.showSnackMessage(requireActivity(), errorMessage)

+ 3 - 3
app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.kt

@@ -25,7 +25,7 @@ import com.nextcloud.client.account.CurrentAccountProvider
 import com.nextcloud.client.di.Injectable
 import com.nextcloud.utils.extensions.getParcelableArgument
 import com.nextcloud.utils.fileNameValidator.FileNameValidator.isFileHidden
-import com.nextcloud.utils.fileNameValidator.FileNameValidator.isValid
+import com.nextcloud.utils.fileNameValidator.FileNameValidator.checkFileName
 import com.owncloud.android.R
 import com.owncloud.android.databinding.EditBoxDialogBinding
 import com.owncloud.android.datamodel.FileDataStorageManager
@@ -138,7 +138,7 @@ class RenameFileDialogFragment : DialogFragment(), DialogInterface.OnClickListen
                 newFileName = binding.userInput.text.toString().trim { it <= ' ' }
             }
 
-            val errorMessage = isValid(newFileName, oCCapability, requireContext(), null)
+            val errorMessage = checkFileName(newFileName, oCCapability, requireContext(), null)
             if (errorMessage != null) {
                 DisplayUtils.showSnackMessage(requireActivity(), errorMessage)
                 return
@@ -163,7 +163,7 @@ class RenameFileDialogFragment : DialogFragment(), DialogInterface.OnClickListen
             newFileName = binding.userInput.text.toString().trim { it <= ' ' }
         }
 
-        val errorMessage = isValid(newFileName, oCCapability, requireContext(), fileNames)
+        val errorMessage = checkFileName(newFileName, oCCapability, requireContext(), fileNames)
 
         if (isFileHidden(newFileName)) {
             binding.userInputContainer.error = getText(R.string.hidden_file_name_warning)