From a6b33b39bca0ef38a5d405bac4df375800cf913c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Tue, 11 May 2021 10:34:00 +0200 Subject: [PATCH 1/5] Migrate RenameRemoteFileOperation to kotlin. First step to keep git history --- ...enameRemoteFileOperation.java => RenameRemoteFileOperation.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/{RenameRemoteFileOperation.java => RenameRemoteFileOperation.kt} (100%) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt similarity index 100% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt From 671ac935efc8e41ca29aa22c58a774a77810857c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Tue, 11 May 2021 11:24:27 +0200 Subject: [PATCH 2/5] Migrate RenameRemoteFileOperation to kotlin. Second step to keep git history --- .../files/RenameRemoteFileOperation.kt | 140 ++++++++---------- 1 file changed, 64 insertions(+), 76 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt index f27fdb532..8669aded6 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2019 ownCloud GmbH. + * Copyright (C) 2021 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,20 +22,20 @@ * */ -package com.owncloud.android.lib.resources.files; +package com.owncloud.android.lib.resources.files -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod; -import com.owncloud.android.lib.common.network.WebdavUtils; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; -import timber.log.Timber; - -import java.io.File; -import java.net.URL; -import java.util.concurrent.TimeUnit; +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod +import com.owncloud.android.lib.common.network.WebdavUtils +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode +import com.owncloud.android.lib.common.utils.isOneOf +import timber.log.Timber +import java.io.File +import java.net.URL +import java.util.concurrent.TimeUnit /** * Remote operation performing the rename of a remote file or folder in the ownCloud server. @@ -43,76 +43,58 @@ import java.util.concurrent.TimeUnit; * @author David A. Velasco * @author masensio */ -public class RenameRemoteFileOperation extends RemoteOperation { - - private static final int RENAME_READ_TIMEOUT = 600000; - private static final int RENAME_CONNECTION_TIMEOUT = 5000; +class RenameRemoteFileOperation( + private val oldName: String, + private val oldRemotePath: String, + private val newName: String, + isFolder: Boolean, +) : RemoteOperation() { - private String mOldName; - private String mOldRemotePath; - private String mNewName; - private String mNewRemotePath; - - /** - * Constructor - * - * @param oldName Old name of the file. - * @param oldRemotePath Old remote path of the file. - * @param newName New name to set as the name of file. - * @param isFolder 'true' for folder and 'false' for files - */ - public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, - boolean isFolder) { - mOldName = oldName; - mOldRemotePath = oldRemotePath; - mNewName = newName; + private var newRemotePath: String? = null - String parent = (new File(mOldRemotePath)).getParent(); - parent = (parent.endsWith(File.separator)) ? parent : parent + File.separator; - mNewRemotePath = parent + mNewName; + init { + var parent = (File(oldRemotePath)).parent!! + parent = if (parent.endsWith(File.separator)) parent else parent + File.separator + newRemotePath = parent + newName if (isFolder) { - mNewRemotePath += File.separator; + newRemotePath.plus(File.separator) } } - /** - * Performs the rename operation. - * - * @param client Client object to communicate with the remote ownCloud server. - */ - @Override - protected RemoteOperationResult run(OwnCloudClient client) { + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { - if (mNewName.equals(mOldName)) { - return new RemoteOperationResult<>(ResultCode.OK); + if (newName == oldName) { + return RemoteOperationResult(ResultCode.OK) } if (targetPathIsUsed(client)) { - return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); + return RemoteOperationResult(ResultCode.INVALID_OVERWRITE) } - final MoveMethod move = new MoveMethod(new URL(client.getUserFilesWebDavUri() + - WebdavUtils.encodePath(mOldRemotePath)), - client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); - - move.setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS); - move.setConnectionTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS); + val moveMethod: MoveMethod = MoveMethod( + url = URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(oldRemotePath)), + destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(newRemotePath), + forceOverride = false + ).apply { + setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS) + setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) + } + val status = client.executeHttpMethod(moveMethod) - final int status = client.executeHttpMethod(move); - final RemoteOperationResult result = - (status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) - ? new RemoteOperationResult<>(ResultCode.OK) - : new RemoteOperationResult<>(move); + result = if (isSuccess(status)) { + RemoteOperationResult(ResultCode.OK) + } else { + RemoteOperationResult(moveMethod) + } - Timber.i("Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage()); - client.exhaustResponse(move.getResponseBodyAsStream()); - return result; - } catch (Exception e) { - final RemoteOperationResult result = new RemoteOperationResult<>(e); - Timber.e(e, - "Rename " + mOldRemotePath + " to " + ((mNewRemotePath == null) ? mNewName : mNewRemotePath) + ":" + - " " + result.getLogMessage()); - return result; + Timber.i("Rename $oldRemotePath to $newRemotePath: ${result.logMessage}") + client.exhaustResponse(moveMethod.getResponseBodyAsStream()) + return result + } catch (exception: Exception) { + result = RemoteOperationResult(exception) + Timber.e(exception, "Rename $oldRemotePath to $newName: ${result.logMessage}") + return result } } @@ -121,10 +103,16 @@ public class RenameRemoteFileOperation extends RemoteOperation { * * @return 'True' if the target path is already used by an existing file. */ - private boolean targetPathIsUsed(OwnCloudClient client) { - CheckPathExistenceRemoteOperation checkPathExistenceRemoteOperation = - new CheckPathExistenceRemoteOperation(mNewRemotePath, false); - RemoteOperationResult exists = checkPathExistenceRemoteOperation.execute(client); - return exists.isSuccess(); + private fun targetPathIsUsed(client: OwnCloudClient): Boolean { + val checkPathExistenceRemoteOperation = CheckPathExistenceRemoteOperation(newRemotePath, false) + val exists = checkPathExistenceRemoteOperation.execute(client) + return exists.isSuccess + } + + private fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT) + + companion object { + private const val RENAME_READ_TIMEOUT = 10_000L + private const val RENAME_CONNECTION_TIMEOUT = 5_000L } -} \ No newline at end of file +} From 5a77bcb0b789bf4df71d9e3e04ecebfc7ea65411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Tue, 11 May 2021 11:28:35 +0200 Subject: [PATCH 3/5] Add rename file to FileService --- .../resources/files/services/FileService.kt | 7 +++++ .../services/implementation/OCFileService.kt | 26 ++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt index ee3a33630..76511895d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt @@ -56,4 +56,11 @@ interface FileService : Service { fun removeFile( remotePath: String ): RemoteOperationResult + + fun renameFile( + oldName: String, + oldRemotePath: String, + newName: String, + isFolder: Boolean, + ): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt index 38cfb47b0..b07d0db3c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt @@ -33,6 +33,7 @@ import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation import com.owncloud.android.lib.resources.files.RemoteFile import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation +import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation import com.owncloud.android.lib.resources.files.services.FileService class OCFileService(override val client: OwnCloudClient) : FileService { @@ -75,11 +76,30 @@ class OCFileService(override val client: OwnCloudClient) : FileService { targetRemotePath = targetRemotePath, ).execute(client) - override fun refreshFolder(remotePath: String): RemoteOperationResult> = + override fun refreshFolder( + remotePath: String + ): RemoteOperationResult> = ReadRemoteFolderOperation( remotePath = remotePath ).execute(client) - override fun removeFile(remotePath: String): RemoteOperationResult = - RemoveRemoteFileOperation(remotePath = remotePath).execute(client) + override fun removeFile( + remotePath: String + ): RemoteOperationResult = + RemoveRemoteFileOperation( + remotePath = remotePath + ).execute(client) + + override fun renameFile( + oldName: String, + oldRemotePath: String, + newName: String, + isFolder: Boolean + ): RemoteOperationResult = + RenameRemoteFileOperation( + oldName = oldName, + oldRemotePath = oldRemotePath, + newName = newName, + isFolder = isFolder + ).execute(client) } From f851adc28111a0de14085573f36de8ba8303d863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Thu, 20 May 2021 14:02:05 +0200 Subject: [PATCH 4/5] Remove nullability from a variable --- .../android/lib/resources/files/RenameRemoteFileOperation.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt index 8669aded6..c89e12e86 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt @@ -50,7 +50,7 @@ class RenameRemoteFileOperation( isFolder: Boolean, ) : RemoteOperation() { - private var newRemotePath: String? = null + private var newRemotePath: String init { var parent = (File(oldRemotePath)).parent!! @@ -75,7 +75,6 @@ class RenameRemoteFileOperation( val moveMethod: MoveMethod = MoveMethod( url = URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(oldRemotePath)), destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(newRemotePath), - forceOverride = false ).apply { setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS) setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) From f5cf272acdf09f05a5db792f836fcd22959af9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Mon, 24 May 2021 11:27:44 +0200 Subject: [PATCH 5/5] Apply code review suggestions --- .../lib/resources/files/RenameRemoteFileOperation.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt index c89e12e86..8f669c69c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RenameRemoteFileOperation.kt @@ -53,9 +53,11 @@ class RenameRemoteFileOperation( private var newRemotePath: String init { - var parent = (File(oldRemotePath)).parent!! - parent = if (parent.endsWith(File.separator)) parent else parent + File.separator - newRemotePath = parent + newName + var parent = (File(oldRemotePath)).parent ?: throw IllegalArgumentException() + if (!parent.endsWith(File.separator)) { + parent = parent.plus(File.separator) + } + newRemotePath = parent.plus(newName) if (isFolder) { newRemotePath.plus(File.separator) }