Przeglądaj źródła

checkFileName according to correct version and configuration

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 9 miesięcy temu
rodzic
commit
72183a57af

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

@@ -8,8 +8,9 @@
 package com.nextcloud.utils
 
 import com.nextcloud.utils.fileNameValidator.FileNameValidator
-import com.owncloud.android.AbstractIT
+import com.owncloud.android.AbstractOnServerIT
 import com.owncloud.android.R
+import com.owncloud.android.lib.resources.status.NextcloudVersion
 import com.owncloud.android.lib.resources.status.OCCapability
 import org.junit.Assert.assertEquals
 import org.junit.Assert.assertFalse
@@ -19,7 +20,7 @@ import org.junit.Before
 import org.junit.Test
 
 @Suppress("TooManyFunctions")
-class FileNameValidatorTests : AbstractIT() {
+class FileNameValidatorTests : AbstractOnServerIT() {
 
     private var capability: OCCapability = fileDataStorageManager.getCapability(account.name)
 
@@ -40,6 +41,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testInvalidCharacter() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val result = FileNameValidator.checkFileName("file<name", capability, targetContext)
         assertEquals(
             String.format(targetContext.getString(R.string.file_name_validator_error_invalid_character), "<"),
@@ -49,12 +52,16 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testReservedName() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val result = FileNameValidator.checkFileName("CON", capability, targetContext)
         assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
     }
 
     @Test
     fun testForbiddenFilenameExtension() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val result = FileNameValidator.checkFileName("my_fav_file.filepart", capability, targetContext)
         assertEquals(
             targetContext.getString(R.string.file_name_validator_error_forbidden_file_extensions, "filepart"),
@@ -79,7 +86,7 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFileAlreadyExists() {
-        val existingFiles = mutableSetOf("existingFile")
+        val existingFiles = setOf("existingFile")
         val result = FileNameValidator.checkFileName("existingFile", capability, targetContext, existingFiles)
         assertEquals(targetContext.getString(R.string.file_already_exists), result)
     }
@@ -98,7 +105,7 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testIsFileNameAlreadyExist() {
-        val existingFiles = mutableSetOf("existingFile")
+        val existingFiles = setOf("existingFile")
         assertTrue(FileNameValidator.isFileNameAlreadyExist("existingFile", existingFiles))
         assertFalse(FileNameValidator.isFileNameAlreadyExist("newFile", existingFiles))
     }
@@ -114,6 +121,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFolderPathWithReservedName() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "CON"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
@@ -123,6 +132,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFilePathWithReservedName() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg")
 
@@ -132,6 +143,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFolderPathWithInvalidCharacter() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "invalid<Folder"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
 
@@ -141,6 +154,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFilePathWithInvalidCharacter() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "validFolder"
         val filePaths = listOf("file1.txt", "file|2.doc", "file3.jpg")
 
@@ -168,6 +183,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testFilePathWithNestedFolder() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "validFolder\\secondValidFolder\\CON"
         val filePaths = listOf("file1.txt", "file2.doc", "file3.")
 
@@ -185,6 +202,8 @@ class FileNameValidatorTests : AbstractIT() {
 
     @Test
     fun testOnlyFolderPathWithOneReservedName() {
+        testOnlyOnServer(NextcloudVersion.nextcloud_30)
+
         val folderPath = "/A1/Aaaww/CON/W/C2/"
 
         val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)

+ 22 - 2
app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt

@@ -17,6 +17,7 @@ import com.nextcloud.utils.extensions.forbiddenFilenames
 import com.nextcloud.utils.extensions.removeFileExtension
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
+import com.owncloud.android.lib.resources.status.NextcloudVersion
 import com.owncloud.android.lib.resources.status.OCCapability
 
 object FileNameValidator {
@@ -36,7 +37,7 @@ object FileNameValidator {
         filename: String,
         capability: OCCapability,
         context: Context,
-        existedFileNames: MutableSet<String>? = null
+        existedFileNames: Set<String>? = null
     ): String? {
         if (TextUtils.isEmpty(filename)) {
             return context.getString(R.string.filename_empty)
@@ -48,10 +49,29 @@ object FileNameValidator {
             }
         }
 
+        return if (capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)) {
+            validateForNCThirtyOrAbove(filename, capability, context)
+        } else {
+            validateForBelowNCThirty(filename, context)
+        }
+    }
+
+    private fun validateForBelowNCThirty(
+        filename: String,
+        context: Context,
+    ): String? {
         if (filename.endsWith(StringConstants.SPACE) || filename.endsWith(StringConstants.DOT)) {
             return context.getString(R.string.file_name_validator_error_ends_with_space_period)
         }
 
+        return null
+    }
+
+    private fun validateForNCThirtyOrAbove(
+        filename: String,
+        capability: OCCapability,
+        context: Context,
+    ): String? {
         checkInvalidCharacters(filename, capability, context)?.let {
             return it
         }
@@ -153,5 +173,5 @@ object FileNameValidator {
 
     fun isFileHidden(name: String): Boolean = !TextUtils.isEmpty(name) && name[0] == '.'
 
-    fun isFileNameAlreadyExist(name: String, fileNames: MutableSet<String>): Boolean = fileNames.contains(name)
+    fun isFileNameAlreadyExist(name: String, fileNames: Set<String>): Boolean = fileNames.contains(name)
 }