From 27774fca7863f0e997290f2748786ef773e0f51b Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 10 Jul 2024 14:57:21 +0200 Subject: [PATCH] Check file names for upload content from other apps Signed-off-by: alperozturk --- .../ui/activity/FolderPickerActivity.kt | 2 +- .../ReceiveExternalFilesActivity.java | 2 +- .../android/ui/helpers/UriUploader.kt | 21 ++++++++++++++++++- app/src/main/res/values/strings.xml | 3 ++- 4 files changed, 24 insertions(+), 4 deletions(-) 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 d422814b52..834de55733 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 @@ -456,7 +456,7 @@ open class FolderPickerActivity : targetFilePaths?.let { filePaths -> if (!isFolderAndFilePathsValid(filePaths)) { - DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move) + DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters) Handler(Looper.getMainLooper()).postDelayed({ setResult(RESULT_CANCELED, resultData) finish() diff --git a/app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java index c71724ca02..b5dae3bbb1 100755 --- a/app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java @@ -671,7 +671,7 @@ public class ReceiveExternalFilesActivity extends FileActivity boolean isPathValid = FileNameValidator.INSTANCE.checkFolderPath(mUploadPath, getCapabilities(), this); if (!isPathValid) { - DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move); + DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters); return; } diff --git a/app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt b/app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt index 3db05a6805..e58b8b5155 100644 --- a/app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt +++ b/app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt @@ -15,6 +15,7 @@ import android.net.Uri import android.os.Parcelable import com.nextcloud.client.account.User import com.nextcloud.client.jobs.upload.FileUploadHelper +import com.nextcloud.utils.fileNameValidator.FileNameValidator import com.owncloud.android.R import com.owncloud.android.files.services.NameCollisionPolicy import com.owncloud.android.lib.common.utils.Log_OC @@ -23,6 +24,7 @@ import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener import com.owncloud.android.ui.fragment.TaskRetainerFragment +import com.owncloud.android.utils.DisplayUtils import com.owncloud.android.utils.UriUtils.getDisplayNameForUri /** @@ -70,9 +72,26 @@ class UriUploader( Log_OC.e(TAG, "Sensitive URI detected, aborting upload.") code = UriUploaderResultCode.ERROR_SENSITIVE_PATH } else { - val uris = mUrisToUpload.filterNotNull() + var isInvalidPathMessageDisplayed = false + val uris = mUrisToUpload + .filterNotNull() .map { it as Uri } .map { Pair(it, getRemotePathForUri(it)) } + .filter { (_, filename) -> + val isValid = (FileNameValidator.checkFileName( + filename.removePrefix("/"), + mActivity.capabilities, + mActivity, null + ) == null) + if (!isValid && !isInvalidPathMessageDisplayed) { + isInvalidPathMessageDisplayed = true + DisplayUtils.showSnackMessage( + mActivity, + R.string.file_name_validator_upload_content_error + ) + } + isValid + } val fileUris = uris .filter { it.first.scheme == ContentResolver.SCHEME_FILE } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e79e65de1b..b8bbe7a445 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1220,7 +1220,8 @@ Permissions are required to open search result otherwise it will redirected to web… - Folder path contains reserved names or invalid character + Some contents cannot able to uploaded due to contains reserved names or invalid character. + Folder path contains reserved names or invalid character File name contains invalid characters: %s %s is a reserved name File name ends with a space or a period