Jelajahi Sumber

Merge pull request #12942 from nextcloud/bugfix/delete-file-v1

Bugfix E2E File Deletion For V1
Alper Öztürk 1 tahun lalu
induk
melakukan
56d9327bbb

+ 0 - 132
app/src/main/java/com/owncloud/android/operations/RemoveRemoteEncryptedFileOperation.java

@@ -1,132 +0,0 @@
-/*
- * Nextcloud - Android Client
- *
- * SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me>
- * SPDX-FileCopyrightText: 2017 Nextcloud GmbH
- * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
- */
-
-package com.owncloud.android.operations;
-
-import android.content.Context;
-
-import com.nextcloud.client.account.User;
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFolderMetadataFile;
-import com.owncloud.android.lib.common.OwnCloudClient;
-import com.owncloud.android.lib.common.operations.RemoteOperation;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult;
-import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.utils.EncryptionUtils;
-import com.owncloud.android.utils.EncryptionUtilsV2;
-
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.NameValuePair;
-import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
-
-import kotlin.Pair;
-
-/**
- * Remote operation performing the removal of a remote encrypted file or folder
- */
-public class RemoveRemoteEncryptedFileOperation extends RemoteOperation<Void> {
-    private static final String TAG = RemoveRemoteEncryptedFileOperation.class.getSimpleName();
-    private static final int REMOVE_READ_TIMEOUT = 30000;
-    private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
-    private final String remotePath;
-    private final OCFile parentFolder;
-    private final User user;
-    private final String fileName;
-    private final Context context;
-    private final boolean isFolder;
-
-    /**
-     * Constructor
-     *
-     * @param remotePath   RemotePath of the remote file or folder to remove from the server
-     * @param parentFolder parent folder
-     */
-    RemoveRemoteEncryptedFileOperation(String remotePath,
-                                       User user,
-                                       Context context,
-                                       String fileName,
-                                       OCFile parentFolder,
-                                       boolean isFolder) {
-        this.remotePath = remotePath;
-        this.user = user;
-        this.fileName = fileName;
-        this.context = context;
-        this.parentFolder = parentFolder;
-        this.isFolder = isFolder;
-    }
-
-    /**
-     * Performs the remove operation.
-     */
-    @Override
-    protected RemoteOperationResult<Void> run(OwnCloudClient client) {
-        RemoteOperationResult<Void> result;
-        DeleteMethod delete = null;
-        String token = null;
-
-        try {
-            // Lock folder
-            token = EncryptionUtils.lockFolder(parentFolder, client);
-
-            EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2();
-            Pair<Boolean, DecryptedFolderMetadataFile> pair = encryptionUtilsV2.retrieveMetadata(parentFolder, client, user, context);
-            boolean metadataExists = pair.getFirst();
-            DecryptedFolderMetadataFile metadata = pair.getSecond();
-
-            // delete file remote
-            delete = new DeleteMethod(client.getFilesDavUri(remotePath));
-            delete.setQueryString(new NameValuePair[]{new NameValuePair(E2E_TOKEN, token)});
-            int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
-
-            delete.getResponseBodyAsString();   // exhaust the response, although not interesting
-            result = new RemoteOperationResult<>(delete.succeeded() || status == HttpStatus.SC_NOT_FOUND, delete);
-            Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
-
-            if (isFolder) {
-                encryptionUtilsV2.removeFolderFromMetadata(fileName, metadata);
-            } else {
-                encryptionUtilsV2.removeFileFromMetadata(fileName, metadata);
-            }
-
-            // upload metadata
-            encryptionUtilsV2.serializeAndUploadMetadata(parentFolder,
-                                                         metadata,
-                                                         token,
-                                                         client,
-                                                         metadataExists,
-                                                         context,
-                                                         user,
-                                                         new FileDataStorageManager(user, context.getContentResolver()));
-
-            // return success
-            return result;
-        } catch (Exception e) {
-            result = new RemoteOperationResult<>(e);
-            Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
-
-        } finally {
-            if (delete != null) {
-                delete.releaseConnection();
-            }
-
-            // unlock file
-            if (token != null) {
-                RemoteOperationResult<Void> unlockFileOperationResult = EncryptionUtils.unlockFolder(parentFolder,
-                                                                                                     client,
-                                                                                                     token);
-
-                if (!unlockFileOperationResult.isSuccess()) {
-                    Log_OC.e(TAG, "Failed to unlock " + parentFolder.getLocalId());
-                }
-            }
-        }
-
-        return result;
-    }
-}

+ 198 - 0
app/src/main/java/com/owncloud/android/operations/RemoveRemoteEncryptedFileOperation.kt

@@ -0,0 +1,198 @@
+/*
+ * Nextcloud - Android Client
+ *
+ * SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me>
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH
+ * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
+ */
+package com.owncloud.android.operations
+
+import android.content.Context
+import androidx.core.util.component1
+import androidx.core.util.component2
+import com.nextcloud.client.account.User
+import com.owncloud.android.datamodel.ArbitraryDataProvider
+import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
+import com.owncloud.android.datamodel.FileDataStorageManager
+import com.owncloud.android.datamodel.OCFile
+import com.owncloud.android.lib.common.OwnCloudClient
+import com.owncloud.android.lib.common.operations.RemoteOperation
+import com.owncloud.android.lib.common.operations.RemoteOperationResult
+import com.owncloud.android.lib.common.utils.Log_OC
+import com.owncloud.android.lib.resources.status.E2EVersion
+import com.owncloud.android.utils.EncryptionUtils
+import com.owncloud.android.utils.EncryptionUtilsV2
+import com.owncloud.android.utils.theme.CapabilityUtils
+import org.apache.commons.httpclient.HttpStatus
+import org.apache.commons.httpclient.NameValuePair
+import org.apache.jackrabbit.webdav.client.methods.DeleteMethod
+
+/**
+ * Remote operation performing the removal of a remote encrypted file or folder
+ *
+ * Constructor
+ *
+ * @param remotePath   RemotePath of the remote file or folder to remove from the server
+ * @param parentFolder parent folder
+ */
+
+@Suppress("LongParameterList")
+class RemoveRemoteEncryptedFileOperation internal constructor(
+    private val remotePath: String,
+    private val user: User,
+    private val context: Context,
+    private val fileName: String,
+    private val parentFolder: OCFile,
+    private val isFolder: Boolean
+) : RemoteOperation<Void>() {
+
+    /**
+     * Performs the remove operation.
+     */
+    @Deprecated("Deprecated in Java")
+    @Suppress("TooGenericExceptionCaught")
+    override fun run(client: OwnCloudClient): RemoteOperationResult<Void> {
+        val result: RemoteOperationResult<Void>
+        var delete: DeleteMethod? = null
+        var token: String? = null
+        val e2eVersion = CapabilityUtils.getCapability(context).endToEndEncryptionApiVersion
+        val isE2EVersionAtLeast2 = e2eVersion >= E2EVersion.V2_0
+
+        try {
+            token = EncryptionUtils.lockFolder(parentFolder, client)
+
+            return if (isE2EVersionAtLeast2) {
+                val deleteResult = deleteForV2(client, token)
+                result = deleteResult.first
+                delete = deleteResult.second
+                result
+            } else {
+                val deleteResult = deleteForV1(client, token)
+                result = deleteResult.first
+                delete = deleteResult.second
+                result
+            }
+        } catch (e: Exception) {
+            result = RemoteOperationResult(e)
+            Log_OC.e(TAG, "Remove " + remotePath + ": " + result.logMessage, e)
+        } finally {
+            delete?.releaseConnection()
+            token?.let { unlockFile(client, it, isE2EVersionAtLeast2) }
+        }
+
+        return result
+    }
+
+    private fun unlockFile(client: OwnCloudClient, token: String, isE2EVersionAtLeast2: Boolean) {
+        val unlockFileOperationResult = if (isE2EVersionAtLeast2) {
+            EncryptionUtils.unlockFolder(parentFolder, client, token)
+        } else {
+            EncryptionUtils.unlockFolderV1(parentFolder, client, token)
+        }
+
+        if (!unlockFileOperationResult.isSuccess) {
+            Log_OC.e(TAG, "Failed to unlock " + parentFolder.localId)
+        }
+    }
+
+    private fun deleteRemoteFile(
+        client: OwnCloudClient,
+        token: String?
+    ): Pair<RemoteOperationResult<Void>, DeleteMethod> {
+        val delete = DeleteMethod(client.getFilesDavUri(remotePath)).apply {
+            setQueryString(arrayOf(NameValuePair(E2E_TOKEN, token)))
+        }
+
+        val status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT)
+        delete.getResponseBodyAsString() // exhaust the response, although not interesting
+
+        val result = RemoteOperationResult<Void>(delete.succeeded() || status == HttpStatus.SC_NOT_FOUND, delete)
+        Log_OC.i(TAG, "Remove " + remotePath + ": " + result.logMessage)
+
+        return Pair(result, delete)
+    }
+
+    private fun deleteForV1(client: OwnCloudClient, token: String?): Pair<RemoteOperationResult<Void>, DeleteMethod> {
+        @Suppress("DEPRECATION")
+        val arbitraryDataProvider: ArbitraryDataProvider = ArbitraryDataProviderImpl(context)
+        val privateKey = arbitraryDataProvider.getValue(user.accountName, EncryptionUtils.PRIVATE_KEY)
+        val publicKey = arbitraryDataProvider.getValue(user.accountName, EncryptionUtils.PUBLIC_KEY)
+
+        val (metadataExists, metadata) = EncryptionUtils.retrieveMetadataV1(
+            parentFolder,
+            client,
+            privateKey,
+            publicKey,
+            arbitraryDataProvider,
+            user
+        )
+
+        val (result, delete) = deleteRemoteFile(client, token)
+
+        if (!isFolder) {
+            EncryptionUtils.removeFileFromMetadata(fileName, metadata)
+        }
+
+        val encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(
+            metadata,
+            publicKey,
+            parentFolder.localId,
+            user,
+            arbitraryDataProvider
+        )
+
+        val serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata)
+
+        EncryptionUtils.uploadMetadata(
+            parentFolder,
+            serializedFolderMetadata,
+            token,
+            client,
+            metadataExists,
+            E2EVersion.V1_2,
+            "",
+            arbitraryDataProvider,
+            user
+        )
+
+        return Pair(result, delete)
+    }
+
+    private fun deleteForV2(client: OwnCloudClient, token: String?): Pair<RemoteOperationResult<Void>, DeleteMethod> {
+        val encryptionUtilsV2 = EncryptionUtilsV2()
+
+        val (metadataExists, metadata) = encryptionUtilsV2.retrieveMetadata(
+            parentFolder,
+            client,
+            user,
+            context
+        )
+
+        val (result, delete) = deleteRemoteFile(client, token)
+
+        if (isFolder) {
+            encryptionUtilsV2.removeFolderFromMetadata(fileName, metadata)
+        } else {
+            encryptionUtilsV2.removeFileFromMetadata(fileName, metadata)
+        }
+
+        encryptionUtilsV2.serializeAndUploadMetadata(
+            parentFolder,
+            metadata,
+            token!!,
+            client,
+            metadataExists,
+            context,
+            user,
+            FileDataStorageManager(user, context.contentResolver)
+        )
+
+        return Pair(result, delete)
+    }
+
+    companion object {
+        private val TAG = RemoveRemoteEncryptedFileOperation::class.java.getSimpleName()
+        private const val REMOVE_READ_TIMEOUT = 30000
+        private const val REMOVE_CONNECTION_TIMEOUT = 5000
+    }
+}

+ 4 - 0
app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java

@@ -169,6 +169,10 @@ public final class EncryptionUtils {
         }
     }
 
+    public static void removeFileFromMetadata(String fileName, DecryptedFolderMetadataFileV1 metadata) {
+        metadata.getFiles().remove(fileName);
+    }
+
     public static String serializeJSON(Object data) {
         return serializeJSON(data, false);
     }