Skip to content

Commit 3defe27

Browse files
committed
fix(file-delete): local copy
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent c52ae6f commit 3defe27

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import com.nextcloud.model.ShareeEntry;
4949
import com.nextcloud.utils.date.DateFormatPattern;
5050
import com.nextcloud.utils.extensions.DateExtensionsKt;
51+
import com.nextcloud.utils.extensions.FileExtensionsKt;
52+
import com.nextcloud.utils.extensions.StringExtensionsKt;
5153
import com.owncloud.android.MainApp;
5254
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
5355
import com.owncloud.android.lib.common.network.WebdavEntry;
@@ -913,25 +915,31 @@ public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLoc
913915
}
914916

915917
private boolean removeLocalCopyIfNeeded(OCFile ocFile, boolean removeLocalCopy, boolean removeDBData) {
916-
String localPath = ocFile.getStoragePath();
917-
918918
if (!removeLocalCopy) {
919-
Log_OC.d(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false");
919+
Log_OC.w(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false");
920920
return true;
921921
}
922922

923-
if (!ocFile.isDown()) {
924-
Log_OC.d(TAG, "removeLocalCopyIfNeeded: file not downloaded -> skip");
923+
String localPath = ocFile.getStoragePath();
924+
final var file = FileExtensionsKt.toFile(localPath);
925+
if (file == null) {
926+
Log_OC.w(TAG, "removeLocalCopyIfNeeded: file exists -> skip");
925927
return true;
926928
}
927929

928-
if (localPath == null) {
929-
Log_OC.d(TAG, "removeLocalCopyIfNeeded: localPath is null -> skip");
930+
if (ocFile.isFolder()) {
931+
Log_OC.w(TAG, "removeLocalCopyIfNeeded: file is folder -> skip");
930932
return true;
931933
}
932934

933-
Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath);
935+
String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile);
936+
if (!localPath.equalsIgnoreCase(expectedPath)) {
937+
Log_OC.w(TAG, "removeLocalCopyIfNeeded: Path mismatch! Expected " + expectedPath
938+
+ " but found " + localPath + ". Skipping deletion to prevent data loss.");
939+
return true;
940+
}
934941

942+
Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath);
935943
boolean success = new File(localPath).delete();
936944
Log_OC.d(TAG, "removeLocalCopyIfNeeded: file deletion result=" + success);
937945

app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public static String getSavePath(String accountName) {
173173
* Get local path where OCFile file is to be stored after upload. That is,
174174
* corresponding local path (in local owncloud storage) to remote uploaded
175175
* file.
176+
* <p>
177+
* e.g. /storage/emulated/0/Android/media/com.nextcloud.client/nextcloud/admin@example.cloud/folder/file.txt
176178
*/
177179
public static String getDefaultSavePathFor(String accountName, OCFile file) {
178180
return getSavePath(accountName) + file.getDecryptedRemotePath();

0 commit comments

Comments
 (0)