mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-11-04 12:28:25 +00:00 
			
		
		
		
	Migrate CopyRemoteFileOperation to kotlin. Second step to keep git history
This commit is contained in:
		
							parent
							
								
									33d6141613
								
							
						
					
					
						commit
						5449eb7480
					
				@ -21,110 +21,88 @@
 | 
				
			|||||||
 *   THE SOFTWARE.
 | 
					 *   THE SOFTWARE.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					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.OwnCloudClient;
 | 
					import com.owncloud.android.lib.common.http.methods.webdav.CopyMethod
 | 
				
			||||||
import com.owncloud.android.lib.common.http.HttpConstants;
 | 
					import com.owncloud.android.lib.common.network.WebdavUtils
 | 
				
			||||||
import com.owncloud.android.lib.common.http.methods.webdav.CopyMethod;
 | 
					import com.owncloud.android.lib.common.operations.RemoteOperation
 | 
				
			||||||
import com.owncloud.android.lib.common.network.WebdavUtils;
 | 
					import com.owncloud.android.lib.common.operations.RemoteOperationResult
 | 
				
			||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
 | 
					import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
 | 
				
			||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 | 
					import timber.log.Timber
 | 
				
			||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
 | 
					import java.net.URL
 | 
				
			||||||
import timber.log.Timber;
 | 
					import java.util.concurrent.TimeUnit
 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.net.URL;
 | 
					 | 
				
			||||||
import java.util.concurrent.TimeUnit;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Remote operation moving a remote file or folder in the ownCloud server to a different folder
 | 
					 * Remote operation copying a remote file or folder in the ownCloud server to a different folder
 | 
				
			||||||
 * in the same account.
 | 
					 * in the same account.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Allows renaming the moving file/folder at the same time.
 | 
					 * Allows renaming the copying file/folder at the same time.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author David A. Velasco
 | 
					 * @author David A. Velasco
 | 
				
			||||||
 * @author Christian Schabesberger
 | 
					 * @author Christian Schabesberger
 | 
				
			||||||
 * @author David González V.
 | 
					 * @author David González V.
 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
public class CopyRemoteFileOperation extends RemoteOperation<String> {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private static final int COPY_READ_TIMEOUT = 600000;
 | 
					 | 
				
			||||||
    private static final int COPY_CONNECTION_TIMEOUT = 5000;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private String mSrcRemotePath;
 | 
					 | 
				
			||||||
    private String mTargetRemotePath;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private boolean mOverwrite;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Constructor.
 | 
					 | 
				
			||||||
     * <p/>
 | 
					 | 
				
			||||||
     * TODO Paths should finish in "/" in the case of folders. ?
 | 
					 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param srcRemotePath    Remote path of the file/folder to move.
 | 
					 * @param srcRemotePath    Remote path of the file/folder to move.
 | 
				
			||||||
 * @param targetRemotePath Remove path desired for the file/folder after moving it.
 | 
					 * @param targetRemotePath Remove path desired for the file/folder after moving it.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
    public CopyRemoteFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite
 | 
					class CopyRemoteFileOperation(
 | 
				
			||||||
    ) {
 | 
					    private val srcRemotePath: String,
 | 
				
			||||||
        mSrcRemotePath = srcRemotePath;
 | 
					    private val targetRemotePath: String,
 | 
				
			||||||
        mTargetRemotePath = targetRemotePath;
 | 
					    private val overwrite: Boolean
 | 
				
			||||||
        mOverwrite = overwrite;
 | 
					) : RemoteOperation<String?>() {
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Performs the rename operation.
 | 
					     * Performs the rename operation.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param client Client object to communicate with the remote ownCloud server.
 | 
					     * @param client Client object to communicate with the remote ownCloud server.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    override fun run(client: OwnCloudClient): RemoteOperationResult<String?> {
 | 
				
			||||||
    protected RemoteOperationResult<String> run(OwnCloudClient client) {
 | 
					        if (targetRemotePath == srcRemotePath) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (mTargetRemotePath.equals(mSrcRemotePath)) {
 | 
					 | 
				
			||||||
            // nothing to do!
 | 
					            // nothing to do!
 | 
				
			||||||
            return new RemoteOperationResult<>(ResultCode.OK);
 | 
					            return RemoteOperationResult(ResultCode.OK)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        if (targetRemotePath.startsWith(srcRemotePath)) {
 | 
				
			||||||
        if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
 | 
					            return RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT)
 | 
				
			||||||
            return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// perform remote operation
 | 
					        /// perform remote operation
 | 
				
			||||||
        RemoteOperationResult result;
 | 
					        var result: RemoteOperationResult<String?>
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            CopyMethod copyMethod =
 | 
					            val copyMethod = CopyMethod(
 | 
				
			||||||
                    new CopyMethod(
 | 
					                URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(srcRemotePath)),
 | 
				
			||||||
                            new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)),
 | 
					                client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(targetRemotePath),
 | 
				
			||||||
                    client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
 | 
					                overwrite
 | 
				
			||||||
                    mOverwrite);
 | 
					            ).apply {
 | 
				
			||||||
 | 
					                setReadTimeout(COPY_READ_TIMEOUT.toLong(), TimeUnit.SECONDS)
 | 
				
			||||||
            copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS);
 | 
					                setConnectionTimeout(COPY_CONNECTION_TIMEOUT.toLong(), TimeUnit.SECONDS)
 | 
				
			||||||
            copyMethod.setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
 | 
					            }
 | 
				
			||||||
 | 
					            val status = client.executeHttpMethod(copyMethod)
 | 
				
			||||||
            final int status = client.executeHttpMethod(copyMethod);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) {
 | 
					            if (status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) {
 | 
				
			||||||
                String fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID);
 | 
					                val fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID)
 | 
				
			||||||
                result = new RemoteOperationResult<>(ResultCode.OK);
 | 
					                result = RemoteOperationResult(ResultCode.OK)
 | 
				
			||||||
                result.setData(fileRemoteId);
 | 
					                result.setData(fileRemoteId)
 | 
				
			||||||
            } else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !mOverwrite) {
 | 
					            } else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !overwrite) {
 | 
				
			||||||
                result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
 | 
					                result = RemoteOperationResult(ResultCode.INVALID_OVERWRITE)
 | 
				
			||||||
                client.exhaustResponse(copyMethod.getResponseBodyAsStream());
 | 
					                client.exhaustResponse(copyMethod.getResponseBodyAsStream())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                /// for other errors that could be explicitly handled, check first:
 | 
					                /// for other errors that could be explicitly handled, check first:
 | 
				
			||||||
                /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
 | 
					                /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
 | 
					                result = RemoteOperationResult(copyMethod)
 | 
				
			||||||
                result = new RemoteOperationResult<>(copyMethod);
 | 
					                client.exhaustResponse(copyMethod.getResponseBodyAsStream())
 | 
				
			||||||
                client.exhaustResponse(copyMethod.getResponseBodyAsStream());
 | 
					            }
 | 
				
			||||||
 | 
					            Timber.i("Copy " + srcRemotePath + " to " + targetRemotePath + ": " + result.logMessage)
 | 
				
			||||||
 | 
					        } catch (e: Exception) {
 | 
				
			||||||
 | 
					            result = RemoteOperationResult(e)
 | 
				
			||||||
 | 
					            Timber.e(e, "Copy " + srcRemotePath + " to " + targetRemotePath + ": " + result.logMessage)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return result
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Timber.i("Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
 | 
					    companion object {
 | 
				
			||||||
 | 
					        private const val COPY_READ_TIMEOUT = 600_000
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        private const val COPY_CONNECTION_TIMEOUT = 5_000
 | 
				
			||||||
            result = new RemoteOperationResult<>(e);
 | 
					 | 
				
			||||||
            Timber.e(e, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return result;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user