From b6a732cdfd2c9f121a205d15b0717302a49966af Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 3 Jul 2024 16:38:08 +0200 Subject: [PATCH] Add checks for move or copy Signed-off-by: alperozturk --- .../nextcloud/utils/FileNameValidatorTests.kt | 72 +++++++++++++++++++ .../utils/extensions/StringExtensions.kt | 9 +++ .../fileNameValidator/FileNameValidator.kt | 23 +++++- .../ui/activity/FolderPickerActivity.kt | 16 +++++ app/src/main/res/values/strings.xml | 2 + 5 files changed, 121 insertions(+), 1 deletion(-) diff --git a/app/src/androidTest/java/com/nextcloud/utils/FileNameValidatorTests.kt b/app/src/androidTest/java/com/nextcloud/utils/FileNameValidatorTests.kt index 3dbc613b0f..6f21ab79a2 100644 --- a/app/src/androidTest/java/com/nextcloud/utils/FileNameValidatorTests.kt +++ b/app/src/androidTest/java/com/nextcloud/utils/FileNameValidatorTests.kt @@ -84,4 +84,76 @@ class FileNameValidatorTests : AbstractIT() { assertTrue(FileNameValidator.isFileNameAlreadyExist("existingFile", existingFiles)) assertFalse(FileNameValidator.isFileNameAlreadyExist("newFile", existingFiles)) } + + @Test + fun testValidFolderAndFilePaths() { + val folderPath = "validFolder" + val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg") + + val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext) + assertTrue(result) + } + + @Test + fun testFolderPathWithReservedName() { + val folderPath = "CON" + val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg") + + val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext) + assertFalse(result) + } + + @Test + fun testFilePathWithReservedName() { + val folderPath = "validFolder" + val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg") + + val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext) + assertFalse(result) + } + + @Test + fun testFolderPathWithInvalidCharacter() { + val folderPath = "invalid, capability: OCCapability, context: Context): Boolean { + val folderPaths = folderPath.split("/", "\\") + + for (item in folderPaths) { + if (isValid(item, capability, context) != null) { + return false + } + } + + for (item in filePaths) { + if (isValid(item, capability, context) != null) { + return false + } + } + + return true + } + private fun checkInvalidCharacters(name: String, capability: OCCapability, context: Context): String? { if (capability.forbiddenFilenameCharacters.isFalse) return null diff --git a/app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt index ed9574f1fb..4dad09c582 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt @@ -15,6 +15,8 @@ import android.content.Intent import android.content.IntentFilter import android.content.res.Resources import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.os.Parcelable import android.view.ActionMode import android.view.Menu @@ -23,6 +25,7 @@ import android.view.View import androidx.activity.OnBackPressedCallback import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.nextcloud.client.di.Injectable +import com.nextcloud.utils.fileNameValidator.FileNameValidator import com.owncloud.android.R import com.owncloud.android.databinding.FilesFolderPickerBinding import com.owncloud.android.databinding.FilesPickerBinding @@ -441,6 +444,7 @@ open class FolderPickerActivity : } } + @Suppress("MagicNumber") private fun processOperation(action: String?) { val i = intent val resultData = Intent() @@ -451,6 +455,18 @@ open class FolderPickerActivity : } targetFilePaths?.let { filePaths -> + + val isPathValid = FileNameValidator.checkPath(file.remotePath, filePaths, capabilities, this) + if (!isPathValid) { + DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move) + Handler(Looper.getMainLooper()).postDelayed({ + setResult(RESULT_CANCELED, resultData) + finish() + + }, 1000L) + return + } + action?.let { action -> fileOperationsHelper.moveOrCopyFiles(action, filePaths, file) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9ac58e4a11..9fde5b47f4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1219,6 +1219,8 @@ Contact not found, you can always sync to update. Redirecting to web… Permissions are required to open search result otherwise it will redirected to web… + + Folder path contains reserved names or invalid character File name contains invalid characters: %s File name is a reserved name File name ends with a space or a period