Procházet zdrojové kódy

Fix parent path finding

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk před 11 měsíci
rodič
revize
a9e3ef99d0

+ 5 - 5
app/src/androidTest/java/com/nextcloud/extensions/OfflineOperationExtensionTests.kt

@@ -9,7 +9,7 @@ package com.nextcloud.extensions
 
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import com.nextcloud.client.database.entity.OfflineOperationEntity
-import com.nextcloud.utils.extensions.getParentPathFromPath
+import com.nextcloud.utils.extensions.getTopParentPathFromPath
 import com.nextcloud.utils.extensions.updatePath
 import com.nextcloud.utils.extensions.updatePathsIfParentPathMatches
 import org.junit.Assert.assertEquals
@@ -59,28 +59,28 @@ class OfflineOperationExtensionTests {
     @Test
     fun testGetParentPathFromPath() {
         val entity = OfflineOperationEntity(path = "/abc/def/file/")
-        val parentPath = entity.getParentPathFromPath()
+        val parentPath = entity.getTopParentPathFromPath()
         assertEquals("/abc/", parentPath)
     }
 
     @Test
     fun testGetParentPathFromPathWithRootPath() {
         val entity = OfflineOperationEntity(path = "/")
-        val parentPath = entity.getParentPathFromPath()
+        val parentPath = entity.getTopParentPathFromPath()
         assertEquals("//", parentPath)
     }
 
     @Test
     fun testGetParentPathFromPathWithEmptyString() {
         val entity = OfflineOperationEntity(path = "")
-        val parentPath = entity.getParentPathFromPath()
+        val parentPath = entity.getTopParentPathFromPath()
         assertEquals("//", parentPath)
     }
 
     @Test
     fun testGetParentPathFromPathWithNullPath() {
         val entity = OfflineOperationEntity(path = null)
-        val parentPath = entity.getParentPathFromPath()
+        val parentPath = entity.getTopParentPathFromPath()
         assertNull(parentPath)
     }
 

+ 34 - 12
app/src/main/java/com/nextcloud/utils/extensions/OfflineOperationExtensions.kt

@@ -12,11 +12,11 @@ import com.nextcloud.client.database.entity.OfflineOperationEntity
 
 private const val DELIMITER = '/'
 
-fun OfflineOperationDao.deleteSubDirIfParentPathMatches(path: String) {
-    val topDir = path.getParentPathFromPath()
+fun OfflineOperationDao.deleteSubDirIfParentPathMatches(filename: String) {
+    val targetPath = DELIMITER + filename + DELIMITER
     getAll().forEach {
-        val entityTopDir = it.getParentPathFromPath()
-        if (entityTopDir == topDir) {
+        val entityParentPath = it.getParentPathFromPath()
+        if (entityParentPath == targetPath) {
             delete(it)
         }
     }
@@ -37,8 +37,8 @@ fun OfflineOperationDao.updatePathsIfParentPathMatches(oldPath: String?, newTopD
 
 fun OfflineOperationDao.updateNextOperationsParentPaths(currentOperation: OfflineOperationEntity) {
     getAll().forEach { nextOperation ->
-        val nextOperationParentPath = nextOperation.getParentPathFromPath()
-        val currentOperationParentPath = currentOperation.getParentPathFromPath()
+        val nextOperationParentPath = nextOperation.getTopParentPathFromPath()
+        val currentOperationParentPath = currentOperation.getTopParentPathFromPath()
         if (nextOperationParentPath == currentOperationParentPath) {
             nextOperation.parentPath = currentOperationParentPath
             update(nextOperation)
@@ -49,8 +49,8 @@ fun OfflineOperationDao.updateNextOperationsParentPaths(currentOperation: Offlin
 fun OfflineOperationEntity.updatePathsIfParentPathMatches(oldPath: String?, newTopDir: String?): String? {
     if (oldPath.isNullOrEmpty() || newTopDir.isNullOrEmpty()) return null
 
-    val topDir = getParentPathFromPath()
-    val oldTopDir = oldPath.getParentPathFromPath()
+    val topDir = getTopParentPathFromPath()
+    val oldTopDir = oldPath.getTopParentPathFromPath()
     return if (topDir == oldTopDir) {
         updatePath(newTopDir)
     } else {
@@ -73,10 +73,32 @@ fun OfflineOperationEntity.updatePath(newParentPath: String?): String? {
     return newParentPath + segments?.joinToString(separator = DELIMITER.toString()) + DELIMITER
 }
 
-fun OfflineOperationEntity.getParentPathFromPath(): String? = path?.getParentPathFromPath()
+fun OfflineOperationEntity.getTopParentPathFromPath(): String? = path?.getTopParentPathFromPath()
 
-private fun String?.getParentPathFromPath(): String {
-    val trimmedPath = this?.trim(DELIMITER)
-    val firstDir = trimmedPath?.split(DELIMITER)?.firstOrNull() ?: ""
+private fun String?.getTopParentPathFromPath(): String? {
+    if (this == null) return null
+    val trimmedPath = this.trim(DELIMITER)
+    val firstDir = trimmedPath.split(DELIMITER).firstOrNull() ?: return null
     return DELIMITER + firstDir + DELIMITER
 }
+
+private fun OfflineOperationEntity.getParentPathFromPath(): String? {
+    if (filename == null) return null
+    return path.getParentPathFromPath(filename!!)
+}
+
+private fun String?.getParentPathFromPath(filename: String): String? {
+    val pathParts = this?.trim(DELIMITER)?.split(DELIMITER) ?: return null
+    val targetIndex = pathParts.indexOf(filename)
+    val result = if (targetIndex >= 0) {
+        if (targetIndex == 0) filename else pathParts[targetIndex - 1]
+    } else {
+        null
+    }
+
+    return if (result != null) {
+        DELIMITER + result + DELIMITER
+    } else {
+        null
+    }
+}

+ 3 - 3
app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -156,7 +156,7 @@ public class FileDataStorageManager {
     }
 
     public void deleteOfflineOperation(OCFile file) {
-        OfflineOperationExtensionsKt.deleteSubDirIfParentPathMatches(offlineOperationDao, file.getDecryptedRemotePath());
+        OfflineOperationExtensionsKt.deleteSubDirIfParentPathMatches(offlineOperationDao, file.getFileName());
         offlineOperationDao.deleteByPath(file.getDecryptedRemotePath());
         removeFile(file, true, true);
     }
@@ -178,7 +178,7 @@ public class FileDataStorageManager {
         }
 
         OfflineOperationEntity entity = addCreateFolderOfflineOperation(newPath, newFolderName, uploadedParentPath, file.getParentId());
-        String newTopDir = OfflineOperationExtensionsKt.getParentPathFromPath(entity);
+        String newTopDir = OfflineOperationExtensionsKt.getTopParentPathFromPath(entity);
 
         if (newTopDir != null) {
             OfflineOperationExtensionsKt.updatePathsIfParentPathMatches(offlineOperationDao, oldPath, newTopDir, entity.getParentPath());
@@ -205,7 +205,7 @@ public class FileDataStorageManager {
         entity.setFilename(newFolderName);
         offlineOperationDao.update(entity);
 
-        String newTopDir = OfflineOperationExtensionsKt.getParentPathFromPath(entity);
+        String newTopDir = OfflineOperationExtensionsKt.getTopParentPathFromPath(entity);
 
         if (newTopDir != null && oldPath != null) {
             OfflineOperationExtensionsKt.updatePathsIfParentPathMatches(offlineOperationDao, oldPath, newTopDir, entity.getParentPath());