From 2c75099a9532ef048c78efe95384e6ac24c9ae3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Fri, 21 May 2021 14:01:25 +0200 Subject: [PATCH] Refactor copy remote file operation. Make overwrite false as default to avoid some potential errors --- .../common/http/methods/webdav/CopyMethod.kt | 2 +- .../files/CopyRemoteFileOperation.kt | 54 ++++++++++--------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index d10718e0..217f3ca9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -36,7 +36,7 @@ import java.net.URL class CopyMethod( val url: URL, private val destinationUrl: String, - private val forceOverride: Boolean + private val forceOverride: Boolean = false ) : DavMethod(url) { @Throws(Exception::class) public override fun onDavExecute(davResource: DavOCResource): Int { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.kt index acb650d5..c1b77f11 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2020 ownCloud GmbH. + * Copyright (C) 2022 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 @@ -30,6 +30,7 @@ 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.net.URL import java.util.concurrent.TimeUnit @@ -44,13 +45,12 @@ import java.util.concurrent.TimeUnit * @author Christian Schabesberger * @author David González V. * - * @param srcRemotePath Remote path of the file/folder to move. - * @param targetRemotePath Remove path desired for the file/folder after moving it. + * @param srcRemotePath Remote path of the file/folder to copy. + * @param targetRemotePath Remote path desired for the file/folder to copy it. */ class CopyRemoteFileOperation( private val srcRemotePath: String, private val targetRemotePath: String, - private val overwrite: Boolean ) : RemoteOperation() { /** * Performs the rename operation. @@ -72,37 +72,43 @@ class CopyRemoteFileOperation( val copyMethod = CopyMethod( URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(srcRemotePath)), client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(targetRemotePath), - overwrite ).apply { - setReadTimeout(COPY_READ_TIMEOUT.toLong(), TimeUnit.SECONDS) - setConnectionTimeout(COPY_CONNECTION_TIMEOUT.toLong(), TimeUnit.SECONDS) + setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS) + setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) } val status = client.executeHttpMethod(copyMethod) + when { + isSuccess(status) -> { + val fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID) + result = RemoteOperationResult(ResultCode.OK) + result.setData(fileRemoteId) + } + isPreconditionFailed(status) -> { + result = RemoteOperationResult(ResultCode.INVALID_OVERWRITE) + client.exhaustResponse(copyMethod.getResponseBodyAsStream()) - if (status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) { - val fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID) - result = RemoteOperationResult(ResultCode.OK) - result.setData(fileRemoteId) - } else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !overwrite) { - result = RemoteOperationResult(ResultCode.INVALID_OVERWRITE) - client.exhaustResponse(copyMethod.getResponseBodyAsStream()) - - /// for other errors that could be explicitly handled, check first: - /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4 - } else { - result = RemoteOperationResult(copyMethod) - client.exhaustResponse(copyMethod.getResponseBodyAsStream()) + /// for other errors that could be explicitly handled, check first: + /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4 + } + else -> { + result = RemoteOperationResult(copyMethod) + client.exhaustResponse(copyMethod.getResponseBodyAsStream()) + } } - Timber.i("Copy " + srcRemotePath + " to " + targetRemotePath + ": " + result.logMessage) + Timber.i("Copy $srcRemotePath to $targetRemotePath: ${result.logMessage}") } catch (e: Exception) { result = RemoteOperationResult(e) - Timber.e(e, "Copy " + srcRemotePath + " to " + targetRemotePath + ": " + result.logMessage) + Timber.e(e, "Copy $srcRemotePath to $targetRemotePath: ${result.logMessage}") } return result } + private fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT) + + private fun isPreconditionFailed(status: Int) = status == HttpConstants.HTTP_PRECONDITION_FAILED + companion object { - private const val COPY_READ_TIMEOUT = 600_000 - private const val COPY_CONNECTION_TIMEOUT = 5_000 + private const val COPY_READ_TIMEOUT = 10L + private const val COPY_CONNECTION_TIMEOUT = 6L } }