Skip to content

Commit ea84cbd

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

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
@@ -47,6 +47,8 @@
4747
import com.nextcloud.model.ShareeEntry;
4848
import com.nextcloud.utils.date.DateFormatPattern;
4949
import com.nextcloud.utils.extensions.DateExtensionsKt;
50+
import com.nextcloud.utils.extensions.FileExtensionsKt;
51+
import com.nextcloud.utils.extensions.StringExtensionsKt;
5052
import com.owncloud.android.MainApp;
5153
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
5254
import com.owncloud.android.lib.common.network.WebdavEntry;
@@ -912,25 +914,31 @@ public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLoc
912914
}
913915

914916
private boolean removeLocalCopyIfNeeded(OCFile ocFile, boolean removeLocalCopy, boolean removeDBData) {
915-
String localPath = ocFile.getStoragePath();
916-
917917
if (!removeLocalCopy) {
918-
Log_OC.d(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false");
918+
Log_OC.w(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false");
919919
return true;
920920
}
921921

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

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

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

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

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)