mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Migrate MoveRemoteFileOperation to kotlin. Second step to keep git history
This commit is contained in:
		
							parent
							
								
									0d4d73eee0
								
							
						
					
					
						commit
						8ea70d77fa
					
				| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* ownCloud Android Library is available under MIT license | ||||||
|  *   Copyright (C) 2020 ownCloud GmbH. |  *   Copyright (C) 2021 ownCloud GmbH. | ||||||
|  * |  * | ||||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   of this software and associated documentation files (the "Software"), to deal | ||||||
| @ -21,126 +21,109 @@ | |||||||
|  *   THE SOFTWARE. |  *   THE SOFTWARE. | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  | package com.owncloud.android.lib.resources.files | ||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.resources.files; | import android.net.Uri | ||||||
| 
 | import com.owncloud.android.lib.common.OwnCloudClient | ||||||
| import android.net.Uri; | import com.owncloud.android.lib.common.http.HttpConstants | ||||||
| 
 | import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.network.WebdavUtils | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants; | import com.owncloud.android.lib.common.operations.RemoteOperation | ||||||
| import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod; | import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | import com.owncloud.android.lib.common.utils.isOneOf | ||||||
| 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 moving a remote file or folder in the ownCloud server to a different folder | ||||||
|  * in the same account. |  * in the same account. | ||||||
|  * <p> |  * | ||||||
|  * Allows renaming the moving file/folder at the same time. |  * Allows renaming the moving file/folder at the same time. | ||||||
|  * |  * | ||||||
|  * @author David A. Velasco |  * @author David A. Velasco | ||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  |  * @author Abel García de Prada | ||||||
|  */ |  */ | ||||||
| public class MoveRemoteFileOperation extends RemoteOperation { | open class MoveRemoteFileOperation( | ||||||
| 
 |     private val sourceRemotePath: String, | ||||||
|     private static final int MOVE_READ_TIMEOUT = 600000; |     private val targetRemotePath: String, | ||||||
|     private static final int MOVE_CONNECTION_TIMEOUT = 5000; |     private val forceOverwrite: Boolean | ||||||
| 
 | ) : RemoteOperation<Unit>() { | ||||||
|     private String mSrcRemotePath; |  | ||||||
|     private String mTargetRemotePath; |  | ||||||
|     private boolean mOverwrite; |  | ||||||
| 
 |  | ||||||
|     protected boolean moveChunkedFile = false; |  | ||||||
|     protected String mFileLastModifTimestamp; |  | ||||||
|     protected long mFileLength; |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Constructor. |  | ||||||
|      * <p> |  | ||||||
|      * TODO Paths should finish in "/" in the case of folders. ? |  | ||||||
|      * |  | ||||||
|      * @param srcRemotePath    Remote path of the file/folder to move. |  | ||||||
|      * @param targetRemotePath Remote path desired for the file/folder after moving it. |  | ||||||
|      */ |  | ||||||
|     public MoveRemoteFileOperation(String srcRemotePath, |  | ||||||
|                                    String targetRemotePath, |  | ||||||
|                                    boolean overwrite) { |  | ||||||
| 
 |  | ||||||
|         mSrcRemotePath = srcRemotePath; |  | ||||||
|         mTargetRemotePath = targetRemotePath; |  | ||||||
|         mOverwrite = overwrite; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 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<Unit> { | ||||||
|     protected RemoteOperationResult run(OwnCloudClient client) { |         if (targetRemotePath == sourceRemotePath) { | ||||||
|         if (mTargetRemotePath.equals(mSrcRemotePath)) { |  | ||||||
|             // nothing to do! |             // nothing to do! | ||||||
|             return new RemoteOperationResult<>(ResultCode.OK); |             return RemoteOperationResult(ResultCode.OK) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (mTargetRemotePath.startsWith(mSrcRemotePath)) { |         if (targetRemotePath.startsWith(sourceRemotePath)) { | ||||||
|             return new RemoteOperationResult<>(ResultCode.INVALID_MOVE_INTO_DESCENDANT); |             return RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// perform remote operation |         /// perform remote operation | ||||||
|         RemoteOperationResult result; |         var result: RemoteOperationResult<Unit> | ||||||
|         try { |         try { | ||||||
|             // After finishing a chunked upload, we have to move the resulting file from uploads folder to files one, |             // After finishing a chunked upload, we have to move the resulting file from uploads folder to files one, | ||||||
|             // so this uri has to be customizable |             // so this uri has to be customizable | ||||||
|             Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); |             val srcWebDavUri = getSrcWebDavUriForClient(client) | ||||||
| 
 |             val moveMethod = MoveMethod( | ||||||
|             final MoveMethod move = new MoveMethod( |                 url = URL(srcWebDavUri.toString() + WebdavUtils.encodePath(sourceRemotePath)), | ||||||
|                     new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), |                 destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(targetRemotePath), | ||||||
|                     client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), |                 forceOverride = forceOverwrite | ||||||
|                     mOverwrite); |             ).apply { | ||||||
| 
 |                 addRequestHeaders(this) | ||||||
|             if (moveChunkedFile) { |                 setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS) | ||||||
|                 move.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); |                 setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS) | ||||||
|                 move.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(mFileLength)); |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             move.setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS); |             val status = client.executeHttpMethod(moveMethod) | ||||||
|             move.setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); |  | ||||||
| 
 | 
 | ||||||
|             final int status = client.executeHttpMethod(move); |  | ||||||
|             /// process response |  | ||||||
|             if (isSuccess(status)) { |             if (isSuccess(status)) { | ||||||
|                 result = new RemoteOperationResult<>(ResultCode.OK); |                 result = RemoteOperationResult<Unit>(ResultCode.OK) | ||||||
|             } else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !mOverwrite) { |             } else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !forceOverwrite) { | ||||||
| 
 |                 result = RemoteOperationResult<Unit>(ResultCode.INVALID_OVERWRITE) | ||||||
|                 result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); |                 client.exhaustResponse(moveMethod.getResponseBodyAsStream()) | ||||||
|                 client.exhaustResponse(move.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 = new RemoteOperationResult<>(move); |                 result = RemoteOperationResult<Unit>(moveMethod) | ||||||
|                 client.exhaustResponse(move.getResponseBodyAsStream()); |                 client.exhaustResponse(moveMethod.getResponseBodyAsStream()) | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             Timber.i("Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage()); |             Timber.i("Move $sourceRemotePath to $targetRemotePath: ${result.logMessage}") | ||||||
|  |         } catch (e: Exception) { | ||||||
|  |             result = RemoteOperationResult<Unit>(e) | ||||||
|  |             Timber.e(e, "Move $sourceRemotePath to $targetRemotePath: ${result.logMessage}") | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |  | ||||||
|             result = new RemoteOperationResult<>(e); |  | ||||||
|             Timber.e(e, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage()); |  | ||||||
|         } |         } | ||||||
| 
 |         return result | ||||||
|         return result; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected boolean isSuccess(int status) { |     /** | ||||||
|         return status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT; |      * For standard moves, we will use [OwnCloudClient.getUserFilesWebDavUri]. | ||||||
|  |      * In case we need a different source Uri, override this method. | ||||||
|  |      */ | ||||||
|  |     open fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.userFilesWebDavUri | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * For standard moves, we won't need any special headers. | ||||||
|  |      * In case new headers are needed, override this method | ||||||
|  |      */ | ||||||
|  |     open fun addRequestHeaders(moveMethod: MoveMethod) { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT) | ||||||
|  | 
 | ||||||
|  |     companion object { | ||||||
|  |         private const val MOVE_READ_TIMEOUT = 10L | ||||||
|  |         private const val MOVE_CONNECTION_TIMEOUT = 6L | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* ownCloud Android Library is available under MIT license | ||||||
|  *   Copyright (C) 2020 ownCloud GmbH. |  *   Copyright (C) 2021 ownCloud GmbH. | ||||||
|  * |  * | ||||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   of this software and associated documentation files (the "Software"), to deal | ||||||
| @ -21,30 +21,40 @@ | |||||||
|  *   THE SOFTWARE. |  *   THE SOFTWARE. | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  | package com.owncloud.android.lib.resources.files.chunks | ||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.resources.files.chunks; | import android.net.Uri | ||||||
| 
 | import com.owncloud.android.lib.common.OwnCloudClient | ||||||
| import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation; | import com.owncloud.android.lib.common.http.HttpConstants | ||||||
|  | import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod | ||||||
|  | import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Remote operation to move the file built from chunks after uploading it |  * Remote operation to move the file built from chunks after uploading it | ||||||
|  * |  * | ||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  |  * @author Abel García de Prada | ||||||
|  */ |  */ | ||||||
| public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation { | class MoveRemoteChunksFileOperation( | ||||||
|  |     sourceRemotePath: String, | ||||||
|  |     targetRemotePath: String, | ||||||
|  |     overwrite: Boolean, | ||||||
|  |     private val fileLastModificationTimestamp: String, | ||||||
|  |     private val fileLength: Long | ||||||
|  | ) : MoveRemoteFileOperation( | ||||||
|  |     sourceRemotePath = sourceRemotePath, | ||||||
|  |     targetRemotePath = targetRemotePath, | ||||||
|  |     forceOverwrite = overwrite | ||||||
|  | ) { | ||||||
| 
 | 
 | ||||||
|     /** |     override fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.uploadsWebDavUri | ||||||
|      * Constructor. | 
 | ||||||
|      * |     override fun addRequestHeaders(moveMethod: MoveMethod) { | ||||||
|      * @param srcRemotePath    Remote path of the file/folder to move. |         super.addRequestHeaders(moveMethod) | ||||||
|      * @param targetRemotePath Remove path desired for the file/folder after moving it. | 
 | ||||||
|      * @param overwrite |         moveMethod.apply { | ||||||
|      */ |             addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, fileLastModificationTimestamp) | ||||||
|     public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, |             addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, fileLength.toString()) | ||||||
|                                          String fileLastModifTimestamp, long fileLength) { |         } | ||||||
|         super(srcRemotePath, targetRemotePath, overwrite); |  | ||||||
|         moveChunkedFile = true; |  | ||||||
|         mFileLastModifTimestamp = fileLastModifTimestamp; |  | ||||||
|         mFileLength = fileLength; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user