diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 99918cce5c0d..f5803f10fc09 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -48,6 +48,8 @@ import com.nextcloud.model.ShareeEntry; import com.nextcloud.utils.date.DateFormatPattern; import com.nextcloud.utils.extensions.DateExtensionsKt; +import com.nextcloud.utils.extensions.FileExtensionsKt; +import com.nextcloud.utils.extensions.StringExtensionsKt; import com.owncloud.android.MainApp; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import com.owncloud.android.lib.common.network.WebdavEntry; @@ -913,25 +915,31 @@ public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLoc } private boolean removeLocalCopyIfNeeded(OCFile ocFile, boolean removeLocalCopy, boolean removeDBData) { - String localPath = ocFile.getStoragePath(); - if (!removeLocalCopy) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false"); + Log_OC.w(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false"); return true; } - if (!ocFile.isDown()) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: file not downloaded -> skip"); + String localPath = ocFile.getStoragePath(); + final var file = FileExtensionsKt.toFile(localPath); + if (file == null) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: file exists -> skip"); return true; } - if (localPath == null) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: localPath is null -> skip"); + if (ocFile.isFolder()) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: file is folder -> skip"); return true; } - Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath); + String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile); + if (!localPath.equalsIgnoreCase(expectedPath)) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: Path mismatch! Expected " + expectedPath + + " but found " + localPath + ". Skipping deletion to prevent data loss."); + return true; + } + Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath); boolean success = new File(localPath).delete(); Log_OC.d(TAG, "removeLocalCopyIfNeeded: file deletion result=" + success); diff --git a/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java b/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java index c31e21a6a48d..4d1d7c1019ec 100644 --- a/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java @@ -173,6 +173,8 @@ public static String getSavePath(String accountName) { * Get local path where OCFile file is to be stored after upload. That is, * corresponding local path (in local owncloud storage) to remote uploaded * file. + *

+ * e.g. /storage/emulated/0/Android/media/com.nextcloud.client/nextcloud/admin@example.cloud/folder/file.txt */ public static String getDefaultSavePathFor(String accountName, OCFile file) { return getSavePath(accountName) + file.getDecryptedRemotePath();