mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Migrate GetRemoteUserAvatarOperation to kotlin
This commit is contained in:
		
							parent
							
								
									8a88fbd938
								
							
						
					
					
						commit
						6f74907af6
					
				| @ -1,185 +0,0 @@ | |||||||
| /* ownCloud Android Library is available under MIT license |  | ||||||
|  * |  | ||||||
|  *   Copyright (C) 2020 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 |  | ||||||
|  *   in the Software without restriction, including without limitation the rights |  | ||||||
|  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |  | ||||||
|  *   copies of the Software, and to permit persons to whom the Software is |  | ||||||
|  *   furnished to do so, subject to the following conditions: |  | ||||||
|  * |  | ||||||
|  *   The above copyright notice and this permission notice shall be included in |  | ||||||
|  *   all copies or substantial portions of the Software. |  | ||||||
|  * |  | ||||||
|  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |  | ||||||
|  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |  | ||||||
|  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |  | ||||||
|  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |  | ||||||
|  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |  | ||||||
|  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |  | ||||||
|  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |  | ||||||
|  *   THE SOFTWARE. |  | ||||||
|  * |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| package com.owncloud.android.lib.resources.users; |  | ||||||
| 
 |  | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; |  | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants; |  | ||||||
| import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod; |  | ||||||
| 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 timber.log.Timber; |  | ||||||
| 
 |  | ||||||
| import java.io.BufferedInputStream; |  | ||||||
| import java.io.ByteArrayOutputStream; |  | ||||||
| import java.io.IOException; |  | ||||||
| import java.io.InputStream; |  | ||||||
| import java.net.URL; |  | ||||||
| 
 |  | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Gets avatar about the user logged in, if available |  | ||||||
|  * |  | ||||||
|  * @author David A. Velasco |  | ||||||
|  * @author David González Verdugo |  | ||||||
|  */ |  | ||||||
| public class GetRemoteUserAvatarOperation extends RemoteOperation<GetRemoteUserAvatarOperation.ResultData> { |  | ||||||
| 
 |  | ||||||
|     private static final String NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"; |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Desired size in pixels of the squared image |  | ||||||
|      */ |  | ||||||
|     private int mDimension; |  | ||||||
| 
 |  | ||||||
|     public GetRemoteUserAvatarOperation(int dimension) { |  | ||||||
|         mDimension = dimension; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Override |  | ||||||
|     protected RemoteOperationResult<ResultData> run(OwnCloudClient client) { |  | ||||||
|         GetMethod getMethod = null; |  | ||||||
|         RemoteOperationResult<ResultData> result; |  | ||||||
|         InputStream inputStream = null; |  | ||||||
|         BufferedInputStream bis = null; |  | ||||||
|         ByteArrayOutputStream bos = null; |  | ||||||
| 
 |  | ||||||
|         try { |  | ||||||
|             final String url = client.getBaseUri() + NON_OFFICIAL_AVATAR_PATH + client.getCredentials().getUsername() + "/" + mDimension; |  | ||||||
|             Timber.d("avatar URI: %s", url); |  | ||||||
| 
 |  | ||||||
|             getMethod = new GetMethod(new URL(url)); |  | ||||||
| 
 |  | ||||||
|             int status = client.executeHttpMethod(getMethod); |  | ||||||
| 
 |  | ||||||
|             if (isSuccess(status)) { |  | ||||||
|                 // find out size of file to read |  | ||||||
|                 int totalToTransfer = 0; |  | ||||||
|                 String contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER); |  | ||||||
| 
 |  | ||||||
|                 if (contentLength != null && contentLength.length() > 0) { |  | ||||||
|                     totalToTransfer = Integer.parseInt(contentLength); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 // find out MIME-type! |  | ||||||
|                 String mimeType; |  | ||||||
|                 String contentType = getMethod.getResponseHeader(HttpConstants.CONTENT_TYPE_HEADER); |  | ||||||
| 
 |  | ||||||
|                 if (contentType == null || !contentType.startsWith("image")) { |  | ||||||
|                     Timber.w("Not an image, failing with no avatar"); |  | ||||||
|                     result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.FILE_NOT_FOUND); |  | ||||||
|                     return result; |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 mimeType = contentType; |  | ||||||
| 
 |  | ||||||
|                 /// download will be performed to a buffer |  | ||||||
|                 inputStream = getMethod.getResponseBodyAsStream(); |  | ||||||
|                 bis = new BufferedInputStream(inputStream); |  | ||||||
|                 bos = new ByteArrayOutputStream(totalToTransfer); |  | ||||||
| 
 |  | ||||||
|                 byte[] bytes = new byte[4096]; |  | ||||||
|                 int readResult; |  | ||||||
|                 while ((readResult = bis.read(bytes)) != -1) { |  | ||||||
|                     bos.write(bytes, 0, readResult); |  | ||||||
|                 } |  | ||||||
|                 // TODO check total bytes transferred? |  | ||||||
| 
 |  | ||||||
|                 // find out etag |  | ||||||
|                 String etag = WebdavUtils.getEtagFromResponse(getMethod); |  | ||||||
|                 if (etag.length() == 0) { |  | ||||||
|                     Timber.w("Could not read Etag from avatar"); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 // Result |  | ||||||
|                 result = new RemoteOperationResult<>(OK); |  | ||||||
|                 result.setData(new ResultData(bos.toByteArray(), mimeType, etag)); |  | ||||||
| 
 |  | ||||||
|             } else { |  | ||||||
|                 result = new RemoteOperationResult<>(getMethod); |  | ||||||
|                 client.exhaustResponse(getMethod.getResponseBodyAsStream()); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|         } catch (Exception e) { |  | ||||||
|             result = new RemoteOperationResult<>(e); |  | ||||||
|             Timber.e(e, "Exception while getting OC user avatar"); |  | ||||||
| 
 |  | ||||||
|         } finally { |  | ||||||
|             if (getMethod != null) { |  | ||||||
|                 try { |  | ||||||
|                     if (inputStream != null) { |  | ||||||
|                         client.exhaustResponse(inputStream); |  | ||||||
|                         if (bis != null) { |  | ||||||
|                             bis.close(); |  | ||||||
|                         } else { |  | ||||||
|                             inputStream.close(); |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } catch (IOException i) { |  | ||||||
|                     Timber.e(i, "Unexpected exception closing input stream"); |  | ||||||
|                 } |  | ||||||
|                 try { |  | ||||||
|                     if (bos != null) { |  | ||||||
|                         bos.close(); |  | ||||||
|                     } |  | ||||||
|                 } catch (IOException o) { |  | ||||||
|                     Timber.e(o, "Unexpected exception closing output stream"); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         return result; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     private boolean isSuccess(int status) { |  | ||||||
|         return (status == HttpConstants.HTTP_OK); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public static class ResultData { |  | ||||||
|         private String mEtag; |  | ||||||
|         private String mMimeType; |  | ||||||
|         private byte[] mAvatarData; |  | ||||||
| 
 |  | ||||||
|         ResultData(byte[] avatarData, String mimeType, String etag) { |  | ||||||
|             mAvatarData = avatarData; |  | ||||||
|             mMimeType = (mimeType == null) ? "" : mimeType; |  | ||||||
|             mEtag = (etag == null) ? "" : etag; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public String getEtag() { |  | ||||||
|             return mEtag; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public String getMimeType() { |  | ||||||
|             return mMimeType; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public byte[] getAvatarData() { |  | ||||||
|             return mAvatarData; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -0,0 +1,109 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  * | ||||||
|  |  *   Copyright (C) 2020 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 | ||||||
|  |  *   in the Software without restriction, including without limitation the rights | ||||||
|  |  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  *   copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  *   furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  *   The above copyright notice and this permission notice shall be included in | ||||||
|  |  *   all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  |  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  |  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||
|  |  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||||
|  |  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||||
|  |  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||
|  |  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  *   THE SOFTWARE. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | package com.owncloud.android.lib.resources.users | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.common.OwnCloudClient | ||||||
|  | import com.owncloud.android.lib.common.http.HttpConstants | ||||||
|  | import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod | ||||||
|  | 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 timber.log.Timber | ||||||
|  | import java.io.IOException | ||||||
|  | import java.io.InputStream | ||||||
|  | import java.net.URL | ||||||
|  | 
 | ||||||
|  | class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : | ||||||
|  |     RemoteOperation<RemoteAvatarData>() { | ||||||
|  |     override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteAvatarData> { | ||||||
|  |         lateinit var inputStream: InputStream | ||||||
|  |         lateinit var getMethod: GetMethod | ||||||
|  | 
 | ||||||
|  |         lateinit var result: RemoteOperationResult<RemoteAvatarData> | ||||||
|  |         try { | ||||||
|  |             val endPoint = | ||||||
|  |                 client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + "/" + avatarDimension | ||||||
|  |             Timber.d("avatar URI: %s", endPoint) | ||||||
|  | 
 | ||||||
|  |             getMethod = GetMethod(URL(endPoint)) | ||||||
|  | 
 | ||||||
|  |             val status = client.executeHttpMethod(getMethod) | ||||||
|  | 
 | ||||||
|  |             if (isSuccess(status)) { | ||||||
|  |                 // find out size of file to read | ||||||
|  |                 val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER).toInt() | ||||||
|  | 
 | ||||||
|  |                 // find out MIME-type! | ||||||
|  |                 val mimeType = getMethod.getResponseHeader(HttpConstants.CONTENT_TYPE_HEADER) | ||||||
|  | 
 | ||||||
|  |                 if (mimeType == null || !mimeType.startsWith("image")) { | ||||||
|  |                     Timber.w("Not an image, failing with no avatar") | ||||||
|  |                     return RemoteOperationResult(RemoteOperationResult.ResultCode.FILE_NOT_FOUND) | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 /// download will be performed to a buffer | ||||||
|  |                 inputStream = getMethod.responseBodyAsStream | ||||||
|  |                 val bytesArray = inputStream.readBytes() | ||||||
|  | 
 | ||||||
|  |                 // TODO check total bytes transferred? | ||||||
|  |                 Timber.d("Avatar size: Bytes received ${bytesArray.size} of $contentLength") | ||||||
|  | 
 | ||||||
|  |                 // find out etag | ||||||
|  |                 val etag = WebdavUtils.getEtagFromResponse(getMethod); | ||||||
|  |                 if (etag.isEmpty()) { | ||||||
|  |                     Timber.w("Could not read Etag from avatar") | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 // Result | ||||||
|  |                 result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) | ||||||
|  |                 result.setData(RemoteAvatarData(bytesArray, mimeType, etag)) | ||||||
|  | 
 | ||||||
|  |             } else { | ||||||
|  |                 result = RemoteOperationResult(getMethod) | ||||||
|  |                 client.exhaustResponse(getMethod.responseBodyAsStream) | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |         } catch (e: Exception) { | ||||||
|  |             result = RemoteOperationResult(e) | ||||||
|  |             Timber.e(e, "Exception while getting OC user avatar"); | ||||||
|  | 
 | ||||||
|  |         } finally { | ||||||
|  |             try { | ||||||
|  |                 client.exhaustResponse(inputStream); | ||||||
|  |                 inputStream.close() | ||||||
|  |             } catch (i: IOException) { | ||||||
|  |                 Timber.e(i, "Unexpected exception closing input stream"); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return result | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK | ||||||
|  | 
 | ||||||
|  |     companion object { | ||||||
|  |         private const val NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/" | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  * | ||||||
|  |  *   Copyright (C) 2020 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 | ||||||
|  |  *   in the Software without restriction, including without limitation the rights | ||||||
|  |  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  *   copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  *   furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  *   The above copyright notice and this permission notice shall be included in | ||||||
|  |  *   all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  |  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  |  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||
|  |  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||||
|  |  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||||
|  |  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||
|  |  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  *   THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.owncloud.android.lib.resources.users | ||||||
|  | 
 | ||||||
|  | data class RemoteAvatarData( | ||||||
|  |     val avatarData: ByteArray = byteArrayOf(), | ||||||
|  |     val mimeType: String = "", | ||||||
|  |     val eTag: String = "" | ||||||
|  | ) | ||||||
| @ -27,9 +27,11 @@ package com.owncloud.android.lib.resources.users.services | |||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||||
| import com.owncloud.android.lib.resources.Service | import com.owncloud.android.lib.resources.Service | ||||||
| import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation | import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation | ||||||
|  | import com.owncloud.android.lib.resources.users.RemoteAvatarData | ||||||
| import com.owncloud.android.lib.resources.users.RemoteUserInfo | import com.owncloud.android.lib.resources.users.RemoteUserInfo | ||||||
| 
 | 
 | ||||||
| interface UserService: Service { | interface UserService : Service { | ||||||
|     fun getUserInfo() : RemoteOperationResult<RemoteUserInfo> |     fun getUserInfo(): RemoteOperationResult<RemoteUserInfo> | ||||||
|     fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota> |     fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota> | ||||||
|  |     fun getUserAvatar(dimension: Int): RemoteOperationResult<RemoteAvatarData> | ||||||
| } | } | ||||||
|  | |||||||
| @ -21,16 +21,22 @@ package com.owncloud.android.lib.resources.users.services.implementation | |||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient | import com.owncloud.android.lib.common.OwnCloudClient | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||||
|  | import com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation | ||||||
| import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation | import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation | ||||||
| import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation | import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation | ||||||
|  | import com.owncloud.android.lib.resources.users.RemoteAvatarData | ||||||
| import com.owncloud.android.lib.resources.users.RemoteUserInfo | import com.owncloud.android.lib.resources.users.RemoteUserInfo | ||||||
| import com.owncloud.android.lib.resources.users.services.UserService | import com.owncloud.android.lib.resources.users.services.UserService | ||||||
| 
 | 
 | ||||||
| class OCUserService(override val client: OwnCloudClient) : | class OCUserService(override val client: OwnCloudClient, private val avatarDimension: Int) : | ||||||
|     UserService { |     UserService { | ||||||
|     override fun getUserInfo(): RemoteOperationResult<RemoteUserInfo> = |     override fun getUserInfo(): RemoteOperationResult<RemoteUserInfo> = | ||||||
|         GetRemoteUserInfoOperation().execute(client) |         GetRemoteUserInfoOperation().execute(client) | ||||||
| 
 | 
 | ||||||
|     override fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota> = |     override fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota> = | ||||||
|         GetRemoteUserQuotaOperation().execute(client) |         GetRemoteUserQuotaOperation().execute(client) | ||||||
|  | 
 | ||||||
|  |     override fun getUserAvatar(dimension: Int): RemoteOperationResult<RemoteAvatarData> = | ||||||
|  |         GetRemoteUserAvatarOperation(avatarDimension).execute(client) | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user