|
| 1 | +/* ownCloud Android Library is available under MIT license |
| 2 | + * |
| 3 | + * Copyright (C) 2020 ownCloud GmbH. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | +package com.owncloud.android.lib.resources.users |
| 26 | + |
| 27 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 28 | +import com.owncloud.android.lib.common.http.HttpConstants |
| 29 | +import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod |
| 30 | +import com.owncloud.android.lib.common.network.WebdavUtils |
| 31 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 32 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 33 | +import com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR |
| 34 | +import timber.log.Timber |
| 35 | +import java.io.IOException |
| 36 | +import java.io.InputStream |
| 37 | +import java.net.URL |
| 38 | + |
| 39 | +/** |
| 40 | + * Gets avatar about the user logged in, if available |
| 41 | + * |
| 42 | + * @author David A. Velasco |
| 43 | + * @author David González Verdugo |
| 44 | + */ |
| 45 | +class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOperation<RemoteAvatarData>() { |
| 46 | + override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteAvatarData> { |
| 47 | + var inputStream: InputStream? = null |
| 48 | + var result: RemoteOperationResult<RemoteAvatarData> |
| 49 | + |
| 50 | + try { |
| 51 | + val endPoint = |
| 52 | + client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + PATH_SEPARATOR + avatarDimension |
| 53 | + Timber.d("avatar URI: %s", endPoint) |
| 54 | + |
| 55 | + val getMethod = GetMethod(URL(endPoint)) |
| 56 | + |
| 57 | + val status = client.executeHttpMethod(getMethod) |
| 58 | + |
| 59 | + if (isSuccess(status)) { |
| 60 | + // find out size of file to read |
| 61 | + val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER).toInt() |
| 62 | + |
| 63 | + // find out MIME-type! |
| 64 | + val mimeType = getMethod.getResponseHeader(HttpConstants.CONTENT_TYPE_HEADER) |
| 65 | + |
| 66 | + if (mimeType == null || !mimeType.startsWith("image")) { |
| 67 | + Timber.w("Not an image, failing with no avatar") |
| 68 | + return RemoteOperationResult(RemoteOperationResult.ResultCode.FILE_NOT_FOUND) |
| 69 | + } |
| 70 | + |
| 71 | + /// download will be performed to a buffer |
| 72 | + inputStream = getMethod.responseBodyAsStream |
| 73 | + val bytesArray = inputStream.readBytes() |
| 74 | + |
| 75 | + // TODO check total bytes transferred? |
| 76 | + Timber.d("Avatar size: Bytes received ${bytesArray.size} of $contentLength") |
| 77 | + |
| 78 | + // find out etag |
| 79 | + val etag = WebdavUtils.getEtagFromResponse(getMethod) |
| 80 | + if (etag.isEmpty()) { |
| 81 | + Timber.w("Could not read Etag from avatar") |
| 82 | + } |
| 83 | + |
| 84 | + // Result |
| 85 | + result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) |
| 86 | + result.setData(RemoteAvatarData(bytesArray, mimeType, etag)) |
| 87 | + |
| 88 | + } else { |
| 89 | + result = RemoteOperationResult(getMethod) |
| 90 | + client.exhaustResponse(getMethod.responseBodyAsStream) |
| 91 | + } |
| 92 | + |
| 93 | + } catch (e: Exception) { |
| 94 | + result = RemoteOperationResult(e) |
| 95 | + Timber.e(e, "Exception while getting OC user avatar") |
| 96 | + |
| 97 | + } finally { |
| 98 | + try { |
| 99 | + client.exhaustResponse(inputStream) |
| 100 | + inputStream?.close() |
| 101 | + } catch (i: IOException) { |
| 102 | + Timber.e(i, "Unexpected exception closing input stream") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return result |
| 107 | + } |
| 108 | + |
| 109 | + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK |
| 110 | + |
| 111 | + companion object { |
| 112 | + private const val NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/" |
| 113 | + } |
| 114 | +} |
0 commit comments