Fix folderPaths check

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-07-10 12:22:43 +02:00 committed by Alper Öztürk
parent 15f9fcce25
commit c74254918a
3 changed files with 29 additions and 3 deletions

View file

@ -41,7 +41,7 @@ class FileNameValidatorTests : AbstractIT() {
@Test
fun testReservedName() {
val result = FileNameValidator.isValid("CON", capability, targetContext)
assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names), result)
assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
}
@Test
@ -156,4 +156,20 @@ class FileNameValidatorTests : AbstractIT() {
val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
assertFalse(result)
}
@Test
fun testOnlyFolderPath() {
val folderPath = "/A1/Aaaww/W/C2/"
val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
assertTrue(result)
}
@Test
fun testOnlyFolderPathWithOneReservedName() {
val folderPath = "/A1/Aaaww/CON/W/C2/"
val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
assertFalse(result)
}
}

View file

@ -65,7 +65,7 @@ object FileNameValidator {
@Suppress("ReturnCount")
fun checkPath(folderPath: String, filePaths: List<String>, capability: OCCapability, context: Context): Boolean {
val folderPaths = folderPath.split("/", "\\")
val folderPaths = folderPath.split("/", "\\").filter { it.isNotEmpty() }
for (item in folderPaths) {
if (isValid(item, capability, context) != null) {

View file

@ -55,6 +55,7 @@ import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.utils.extensions.BundleExtensionsKt;
import com.nextcloud.utils.extensions.FileExtensionsKt;
import com.nextcloud.utils.extensions.IntentExtensionsKt;
import com.nextcloud.utils.fileNameValidator.FileNameValidator;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.databinding.ReceiveExternalFilesBinding;
@ -661,8 +662,17 @@ public class ReceiveExternalFilesActivity extends FileActivity
if (id == R.id.uploader_choose_folder) {
mUploadPath = ""; // first element in mParents is root dir, represented by "";
// init mUploadPath with "/" results in a "//" prefix
StringBuilder stringBuilder = new StringBuilder();
for (String p : mParents) {
mUploadPath += p + OCFile.PATH_SEPARATOR;
stringBuilder.append(p).append(OCFile.PATH_SEPARATOR);
}
mUploadPath = stringBuilder.toString();
boolean isPathValid = FileNameValidator.INSTANCE.checkPath(mUploadPath, new ArrayList<>(), getCapabilities(), this);
if (!isPathValid) {
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move);
return;
}
if (mUploadFromTmpFile) {