mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Migrate ReadRemoteFileOperation to kotlin. Second step to keep git history
This commit is contained in:
		
							parent
							
								
									2b79175b5a
								
							
						
					
					
						commit
						17821e5760
					
				| @ -53,7 +53,7 @@ class PropfindMethod( | |||||||
|             depth = depth, |             depth = depth, | ||||||
|             reqProp = propertiesToRequest, |             reqProp = propertiesToRequest, | ||||||
|             listOfHeaders = super.getRequestHeadersAsHashMap(), |             listOfHeaders = super.getRequestHeadersAsHashMap(), | ||||||
|             callback = { response: Response, hrefRelation: HrefRelation? -> |             callback = { response: Response, hrefRelation: HrefRelation -> | ||||||
|                 when (hrefRelation) { |                 when (hrefRelation) { | ||||||
|                     HrefRelation.MEMBER -> members.add(response) |                     HrefRelation.MEMBER -> members.add(response) | ||||||
|                     HrefRelation.SELF -> this.root = response |                     HrefRelation.SELF -> this.root = response | ||||||
|  | |||||||
| @ -23,21 +23,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.OwnCloudClient | ||||||
| import com.owncloud.android.lib.common.accounts.AccountUtils; | import com.owncloud.android.lib.common.accounts.AccountUtils | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants; | import com.owncloud.android.lib.common.http.HttpConstants.HTTP_MULTI_STATUS | ||||||
| import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; | import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK | ||||||
| import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; | import com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0 | ||||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | import com.owncloud.android.lib.common.http.methods.webdav.DavUtils | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | import com.owncloud.android.lib.common.network.WebdavUtils | ||||||
| import timber.log.Timber; | import com.owncloud.android.lib.common.operations.RemoteOperation | ||||||
| 
 | import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||||
| import java.net.URL; | import com.owncloud.android.lib.common.utils.isOneOf | ||||||
| import java.util.concurrent.TimeUnit; | import timber.log.Timber | ||||||
| 
 | import java.net.URL | ||||||
| import static com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0; | import java.util.concurrent.TimeUnit | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Remote operation performing the read a file from the ownCloud server. |  * Remote operation performing the read a file from the ownCloud server. | ||||||
| @ -47,21 +46,7 @@ import static com.owncloud.android.lib.common.operations.RemoteOperationResult.R | |||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| public class ReadRemoteFileOperation extends RemoteOperation<RemoteFile> { | class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFile>() { | ||||||
| 
 |  | ||||||
|     private static final int SYNC_READ_TIMEOUT = 40000; |  | ||||||
|     private static final int SYNC_CONNECTION_TIMEOUT = 5000; |  | ||||||
| 
 |  | ||||||
|     private String mRemotePath; |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Constructor |  | ||||||
|      * |  | ||||||
|      * @param remotePath Remote path of the file. |  | ||||||
|      */ |  | ||||||
|     public ReadRemoteFileOperation(String remotePath) { |  | ||||||
|         mRemotePath = remotePath; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Performs the read operation. |      * Performs the read operation. | ||||||
| @ -69,41 +54,44 @@ public class ReadRemoteFileOperation extends RemoteOperation<RemoteFile> { | |||||||
|      * @param client Client object to communicate with the remote ownCloud server. |      * @param client Client object to communicate with the remote ownCloud server. | ||||||
|      */ |      */ | ||||||
|     @Override |     @Override | ||||||
|     protected RemoteOperationResult<RemoteFile> run(OwnCloudClient client) { |     override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteFile> { | ||||||
|         PropfindMethod propfind; |  | ||||||
|         RemoteOperationResult<RemoteFile> result; |  | ||||||
| 
 |  | ||||||
|         /// take the duty of check the server for the current state of the file there |  | ||||||
|         try { |         try { | ||||||
|             // remote request |             val propFind = PropfindMethod( | ||||||
|             propfind = new PropfindMethod( |                 url = URL("${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)}"), | ||||||
|                     new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), |                 depth = DEPTH_0, | ||||||
|                     DEPTH_0, |                 propertiesToRequest = DavUtils.allPropset | ||||||
|                     DavUtils.getAllPropset()); |             ).apply { | ||||||
| 
 |                 setReadTimeout(SYNC_READ_TIMEOUT, TimeUnit.SECONDS) | ||||||
|             propfind.setReadTimeout(SYNC_READ_TIMEOUT, TimeUnit.SECONDS); |                 setConnectionTimeout(SYNC_CONNECTION_TIMEOUT, TimeUnit.SECONDS) | ||||||
|             propfind.setConnectionTimeout(SYNC_CONNECTION_TIMEOUT, TimeUnit.SECONDS); |  | ||||||
|             final int status = client.executeHttpMethod(propfind); |  | ||||||
| 
 |  | ||||||
|             if (status == HttpConstants.HTTP_MULTI_STATUS |  | ||||||
|                     || status == HttpConstants.HTTP_OK) { |  | ||||||
| 
 |  | ||||||
|                 final RemoteFile file = RemoteFile.Companion.getRemoteFileFromDav(propfind.getRoot(), |  | ||||||
|                         AccountUtils.getUserId(mAccount, mContext), mAccount.name); |  | ||||||
| 
 |  | ||||||
|                 result = new RemoteOperationResult<>(OK); |  | ||||||
|                 result.setData(file); |  | ||||||
| 
 |  | ||||||
|             } else { |  | ||||||
|                 result = new RemoteOperationResult<>(propfind); |  | ||||||
|                 client.exhaustResponse(propfind.getResponseBodyAsStream()); |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |             val status = client.executeHttpMethod(propFind) | ||||||
|             result = new RemoteOperationResult<>(e); |             Timber.i("Read remote file $remotePath with status ${propFind.statusCode}") | ||||||
|             Timber.e(e, "Synchronizing  file %s", mRemotePath); |  | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         return result; |             return if (isSuccess(status)) { | ||||||
|  |                 // TODO: Remove that !! | ||||||
|  |                 val remoteFile = RemoteFile.getRemoteFileFromDav( | ||||||
|  |                     propFind.root!!, | ||||||
|  |                     AccountUtils.getUserId(mAccount, mContext), mAccount.name | ||||||
|  |                 ) | ||||||
|  | 
 | ||||||
|  |                 RemoteOperationResult<RemoteFile>(RemoteOperationResult.ResultCode.OK).apply { | ||||||
|  |                     data = remoteFile | ||||||
|  |                 } | ||||||
|  |             } else { | ||||||
|  |                 RemoteOperationResult<RemoteFile>(propFind).also { | ||||||
|  |                     client.exhaustResponse(propFind.getResponseBodyAsStream()) | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } catch (exception: Exception) { | ||||||
|  |             return RemoteOperationResult(exception) | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | 
 | ||||||
|  |     private fun isSuccess(status: Int) = status.isOneOf(HTTP_MULTI_STATUS, HTTP_OK) | ||||||
|  | 
 | ||||||
|  |     companion object { | ||||||
|  |         private const val SYNC_READ_TIMEOUT = 40_000L | ||||||
|  |         private const val SYNC_CONNECTION_TIMEOUT = 5_000L | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user