Prechádzať zdrojové kódy

Use encrypted remote path for file conflict resolver dialog

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 rok pred
rodič
commit
82c1c25a2f

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

@@ -66,11 +66,14 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.annotation.VisibleForTesting;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import kotlin.Pair;
 
 public class FileDataStorageManager {
     private static final String TAG = FileDataStorageManager.class.getSimpleName();
@@ -2297,6 +2300,45 @@ public class FileDataStorageManager {
         }
     }
 
+    public String getFolderName(String path) {
+        return "/" + path.split("/")[1] + "/";
+    }
+
+    public String getEncryptedRemotePath(String decryptedRemotePath) {
+        String folderName = getFolderName(decryptedRemotePath);
+
+        if (folderName == null) {
+            throw new NullPointerException("folderName cannot be null");
+        }
+
+        OCFile folder = getFileByDecryptedRemotePath(folderName);
+        List<OCFile> files = getAllFilesRecursivelyInsideFolder(folder);
+        List<Pair<String, String>> decryptedFileNamesAndEncryptedRemotePaths = getDecryptedFileNamesAndEncryptedRemotePaths(files);
+
+        String decryptedFileName = decryptedRemotePath.substring( decryptedRemotePath.lastIndexOf('/') + 1);
+
+        for (Pair<String, String> item : decryptedFileNamesAndEncryptedRemotePaths) {
+            if (item.getFirst().equals(decryptedFileName)) {
+                return item.getSecond();
+            }
+        }
+
+        return null;
+    }
+
+    private List<Pair<String, String>> getDecryptedFileNamesAndEncryptedRemotePaths(List<OCFile> fileList) {
+        List<Pair<String, String>> result = new ArrayList<>();
+
+        for (OCFile file : fileList) {
+            if (file.isEncrypted()) {
+                Pair<String, String> fileNameAndEncryptedRemotePath = new Pair<>(file.getDecryptedFileName(), file.getRemotePath());
+                result.add(fileNameAndEncryptedRemotePath);
+            }
+        }
+
+        return result;
+    }
+
     public void removeLocalFiles(User user, FileDataStorageManager storageManager) {
         File tempDir = new File(FileStorageUtils.getTemporalPath(user.getAccountName()));
         File saveDir = new File(FileStorageUtils.getSavePath(user.getAccountName()));

+ 1 - 0
app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java

@@ -625,6 +625,7 @@ public class RefreshFolderOperation extends RemoteOperation {
         }
     }
 
+    // TODO write test for decryptedRemotePath existence...
     public static void updateFileNameForEncryptedFile(FileDataStorageManager storageManager,
                                                       @NonNull DecryptedFolderMetadataFile metadata,
                                                       OCFile updatedFile) {

+ 11 - 2
app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt

@@ -21,6 +21,7 @@ import com.nextcloud.client.jobs.upload.UploadNotificationManager
 import com.nextcloud.model.HTTPStatusCodes
 import com.nextcloud.utils.extensions.getParcelableArgument
 import com.owncloud.android.R
+import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.UploadsStorageManager
 import com.owncloud.android.db.OCUpload
@@ -42,6 +43,10 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
     @Inject
     var uploadsStorageManager: UploadsStorageManager? = null
 
+    @JvmField
+    @Inject
+    var fileStorageManager: FileDataStorageManager? = null
+
     private var conflictUploadId: Long = 0
     private var existingFile: OCFile? = null
     private var newFile: OCFile? = null
@@ -159,8 +164,12 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
             return
         }
         if (existingFile == null) {
-            // fetch info of existing file from server
-            val operation = ReadFileRemoteOperation(newFile!!.remotePath)
+            var remotePath = newFile!!.remotePath
+            if (newFile?.isEncrypted == true) {
+                remotePath = fileStorageManager?.getEncryptedRemotePath(newFile!!.remotePath)
+            }
+
+            val operation = ReadFileRemoteOperation(remotePath)
 
             @Suppress("TooGenericExceptionCaught")
             Thread {

+ 14 - 7
app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -54,7 +54,6 @@ import com.nextcloud.client.jobs.download.FileDownloadHelper;
 import com.nextcloud.client.jobs.download.FileDownloadWorker;
 import com.nextcloud.client.jobs.upload.FileUploadHelper;
 import com.nextcloud.client.jobs.upload.FileUploadWorker;
-import com.nextcloud.client.jobs.upload.UploadNotificationManager;
 import com.nextcloud.client.media.PlayerServiceConnection;
 import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ConnectivityService;
@@ -135,6 +134,7 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 
 import javax.inject.Inject;
@@ -149,6 +149,7 @@ import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import kotlin.Pair;
 import kotlin.Unit;
 
 import static com.owncloud.android.datamodel.OCFile.PATH_SEPARATOR;
@@ -878,18 +879,24 @@ public class FileDisplayActivity extends FileActivity
         requestUploadOfFilesFromFileSystem(basePath, filePaths, resultCode);
     }
 
+    private String[] getRemotePaths(String directory, String[] filePaths, String localBasePath) {
+        String[] remotePaths = new String[filePaths.length];
+        for (int j = 0; j < remotePaths.length; j++) {
+            String relativePath = StringUtils.removePrefix(filePaths[j], localBasePath);
+            remotePaths[j] = directory + relativePath;
+        }
+
+        return remotePaths;
+    }
+
     private void requestUploadOfFilesFromFileSystem(String localBasePath, String[] filePaths, int resultCode) {
         if (localBasePath != null && filePaths != null) {
             if (!localBasePath.endsWith("/")) {
                 localBasePath = localBasePath + "/";
             }
 
-            String[] remotePaths = new String[filePaths.length];
             String remotePathBase = getCurrentDir().getRemotePath();
-            for (int j = 0; j < remotePaths.length; j++) {
-                String relativePath = StringUtils.removePrefix(filePaths[j], localBasePath);
-                remotePaths[j] = remotePathBase + relativePath;
-            }
+            String[] decryptedRemotePaths = getRemotePaths(remotePathBase, filePaths, localBasePath);
 
             int behaviour = switch (resultCode) {
                 case UploadFilesActivity.RESULT_OK_AND_MOVE -> FileUploadWorker.LOCAL_BEHAVIOUR_MOVE;
@@ -899,7 +906,7 @@ public class FileDisplayActivity extends FileActivity
 
             FileUploadHelper.Companion.instance().uploadNewFiles(getUser().orElseThrow(RuntimeException::new),
                                                                  filePaths,
-                                                                 remotePaths,
+                                                                 decryptedRemotePaths,
                                                                  behaviour,
                                                                  true,
                                                                  UploadFileOperation.CREATED_BY_USER,