From 85782e4818495a9a10cd61c4371c026c3c2e4a9f Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 29 Apr 2020 18:36:32 +0200 Subject: [PATCH 01/10] Migrate GetRemoteUserQuotaOperation to kotlin --- .../users/GetRemoteUserQuotaOperation.java | 185 ------------------ .../users/GetRemoteUserQuotaOperation.kt | 126 ++++++++++++ 2 files changed, 126 insertions(+), 185 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java deleted file mode 100644 index e4d834d8..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java +++ /dev/null @@ -1,185 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * - * Copyright (C) 2020 ownCloud Inc. - * Copyright (C) 2015 Bartosz Przybylski - * Copyright (C) 2014 Marcello Steiner - * - * 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 at.bitfire.dav4android.Property; -import at.bitfire.dav4android.property.QuotaAvailableBytes; -import at.bitfire.dav4android.property.QuotaUsedBytes; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; -import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; -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.net.URL; -import java.util.List; - -import static com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0; -import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; - -/** - * @author marcello - * @author David González Verdugo - */ -public class GetRemoteUserQuotaOperation extends RemoteOperation { - - private String mRemotePath; - - /** - * Constructor - * - * @param remotePath Remote path of the file. - */ - public GetRemoteUserQuotaOperation(String remotePath) { - mRemotePath = remotePath; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - - try { - PropfindMethod propfindMethod = new PropfindMethod( - new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), - DEPTH_0, - DavUtils.getQuotaPropSet()); - - int status = client.executeHttpMethod(propfindMethod); - - if (isSuccess(status)) { - RemoteQuota remoteQuota = readData(propfindMethod.getRoot().getProperties()); - - result = new RemoteOperationResult<>(OK); - - // Add data to the result - if (result.isSuccess()) { - result.setData(remoteQuota); - } - - } else { // synchronization failed - result = new RemoteOperationResult<>(propfindMethod); - } - - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - - } finally { - if (result.isSuccess()) { - Timber.i("Get quota from " + mRemotePath + ": " + result.getLogMessage()); - } else { - if (result.isException()) { - Timber.e(result.getException(), "Get quota from " + mRemotePath + ": " + result.getLogMessage()); - } else { - Timber.e("Get quota from " + mRemotePath + ": " + result.getLogMessage()); - } - } - } - - return result; - } - - private boolean isSuccess(int status) { - return status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_OK; - } - - /** - * Read the data retrieved from the server about the quota - * - * @param properties WebDAV properties containing quota data - * @return new {@link RemoteQuota} instance representing the data read from the server - */ - private RemoteQuota readData(List properties) { - long quotaAvailable = 0; - long quotaUsed = 0; - - for (Property property : properties) { - if (property instanceof QuotaAvailableBytes) { - quotaAvailable = ((QuotaAvailableBytes) property).getQuotaAvailableBytes(); - } - if (property instanceof QuotaUsedBytes) { - quotaUsed = ((QuotaUsedBytes) property).getQuotaUsedBytes(); - } - } - - // If there's a special case, quota available will contain a negative code - // -1, PENDING: Not computed yet, e.g. external storage mounted but folder sizes need scanning - // -2, UNKNOWN: Storage not accessible, e.g. external storage with no API to ask for the free space - // -3, UNLIMITED: Quota using all the storage - if (quotaAvailable < 0) { - return new RemoteQuota( - quotaAvailable, - quotaUsed, - 0, - 0 - ); - } else { - long totalQuota = quotaAvailable + quotaUsed; - double relativeQuota = (double) (quotaUsed * 100) / totalQuota; - double roundedRelativeQuota = Math.round(relativeQuota * 100) / 100.0d; - - return new RemoteQuota( - quotaAvailable, - quotaUsed, - totalQuota, - roundedRelativeQuota - ); - } - } - - static public class RemoteQuota { - - long mFree, mUsed, mTotal; - double mRelative; - - public RemoteQuota(long free, long used, long total, double relative) { - mFree = free; - mUsed = used; - mTotal = total; - mRelative = relative; - } - - public long getFree() { - return mFree; - } - - public long getUsed() { - return mUsed; - } - - public long getTotal() { - return mTotal; - } - - public double getRelative() { - return mRelative; - } - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt new file mode 100644 index 00000000..0848620a --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -0,0 +1,126 @@ +/* ownCloud Android Library is available under MIT license +* +* Copyright (C) 2020 ownCloud Inc. +* Copyright (C) 2015 Bartosz Przybylski +* Copyright (C) 2014 Marcello Steiner +* +* 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 at.bitfire.dav4android.Property +import at.bitfire.dav4android.property.QuotaAvailableBytes +import at.bitfire.dav4android.property.QuotaUsedBytes +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.webdav.DavConstants +import com.owncloud.android.lib.common.http.methods.webdav.DavUtils +import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode +import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation.RemoteQuota +import timber.log.Timber +import java.net.URL +import kotlin.math.roundToLong + +/** + * @author marcello + * @author David González Verdugo + */ +class GetRemoteUserQuotaOperation : RemoteOperation() { + override fun run(client: OwnCloudClient): RemoteOperationResult { + lateinit var result: RemoteOperationResult + try { + val propfindMethod = PropfindMethod( + URL(client.userFilesWebDavUri.toString()), + DavConstants.DEPTH_0, + DavUtils.getQuotaPropSet() + ) + val status = client.executeHttpMethod(propfindMethod) + if (isSuccess(status)) { + val remoteQuota = readData(propfindMethod.root.properties) + result = RemoteOperationResult(ResultCode.OK) + + // Add data to the result + if (result.isSuccess) { + result.data = remoteQuota + } + } else { // synchronization failed + result = RemoteOperationResult(propfindMethod) + } + } catch (e: Exception) { + result = RemoteOperationResult(e) + } finally { + if (result.isSuccess) { + Timber.i("Get quota completed: ${result.data} and message: ${result.logMessage}") + } else { + if (result.isException) { + Timber.e(result.exception, "Get quota: ${result.logMessage}") + } else { + Timber.e("Get quota without success: ${result.logMessage}") + } + } + } + return result + } + + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_OK + + /** + * Read the data retrieved from the server about the quota + * + * @param properties WebDAV properties containing quota data + * @return new [RemoteQuota] instance representing the data read from the server + */ + private fun readData(properties: List): RemoteQuota { + var quotaAvailable: Long = 0 + var quotaUsed: Long = 0 + for (property in properties) { + if (property is QuotaAvailableBytes) { + quotaAvailable = property.quotaAvailableBytes + } + if (property is QuotaUsedBytes) { + quotaUsed = property.quotaUsedBytes + } + } + + // If there's a special case, quota available will contain a negative code + // -1, PENDING: Not computed yet, e.g. external storage mounted but folder sizes need scanning + // -2, UNKNOWN: Storage not accessible, e.g. external storage with no API to ask for the free space + // -3, UNLIMITED: Quota using all the storage + return if (quotaAvailable < 0) { + RemoteQuota( + free = quotaAvailable, + used = quotaUsed, + total = 0, + relative = 0.0 + ) + } else { + val totalQuota = quotaAvailable + quotaUsed + val relativeQuota = (quotaUsed * 100).toDouble() / totalQuota + val roundedRelativeQuota = (relativeQuota * 100).roundToLong() / 100.0 + RemoteQuota(quotaAvailable, quotaUsed, totalQuota, roundedRelativeQuota) + } + } + + data class RemoteQuota(var free: Long, var used: Long, var total: Long, var relative: Double) +} From 337c57da1ade64481bf8688481e0be2d6aca7985 Mon Sep 17 00:00:00 2001 From: agarcia Date: Thu, 30 Apr 2020 15:07:44 +0200 Subject: [PATCH 02/10] Include GetQuota in UserService --- .../android/lib/resources/users/services/UserService.kt | 2 ++ .../resources/users/services/implementation/OCUserService.kt | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt index 4ef2e1a7..1030aff2 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt @@ -26,8 +26,10 @@ package com.owncloud.android.lib.resources.users.services import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.Service +import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation import com.owncloud.android.lib.resources.users.RemoteUserInfo interface UserService: Service { fun getUserInfo() : RemoteOperationResult + fun getUserQuota(): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt index 6b07b25e..d4b40e06 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt @@ -22,6 +22,7 @@ package com.owncloud.android.lib.resources.users.services.implementation import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation +import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation import com.owncloud.android.lib.resources.users.RemoteUserInfo import com.owncloud.android.lib.resources.users.services.UserService @@ -29,4 +30,7 @@ class OCUserService(override val client: OwnCloudClient) : UserService { override fun getUserInfo(): RemoteOperationResult = GetRemoteUserInfoOperation().execute(client) + + override fun getUserQuota(): RemoteOperationResult = + GetRemoteUserQuotaOperation().execute(client) } From bbac8d027840a29014fbac8020b5c58859c3e994 Mon Sep 17 00:00:00 2001 From: agarcia Date: Mon, 25 May 2020 13:50:29 +0200 Subject: [PATCH 03/10] Polish remote operation --- .../users/GetRemoteUserQuotaOperation.kt | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index 0848620a..11c60a03 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -58,27 +58,18 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { val status = client.executeHttpMethod(propfindMethod) if (isSuccess(status)) { val remoteQuota = readData(propfindMethod.root.properties) - result = RemoteOperationResult(ResultCode.OK) - - // Add data to the result - if (result.isSuccess) { - result.data = remoteQuota + result = RemoteOperationResult(ResultCode.OK).apply { + data = remoteQuota } + Timber.i("Get quota completed: ${result.data} and message: ${result.logMessage}") + } else { // synchronization failed result = RemoteOperationResult(propfindMethod) + Timber.e("Get quota without success: ${result.logMessage}") } } catch (e: Exception) { result = RemoteOperationResult(e) - } finally { - if (result.isSuccess) { - Timber.i("Get quota completed: ${result.data} and message: ${result.logMessage}") - } else { - if (result.isException) { - Timber.e(result.exception, "Get quota: ${result.logMessage}") - } else { - Timber.e("Get quota without success: ${result.logMessage}") - } - } + Timber.e(result.exception, "Get quota: ${result.logMessage}") } return result } From 8a88fbd9380f188c9a03e8d6b1a0d619bd0f30b6 Mon Sep 17 00:00:00 2001 From: agarcia Date: Tue, 26 May 2020 18:07:12 +0200 Subject: [PATCH 04/10] Apply CR suggestions --- .../users/GetRemoteUserQuotaOperation.kt | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index 11c60a03..e2e00230 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -47,32 +47,31 @@ import kotlin.math.roundToLong * @author David González Verdugo */ class GetRemoteUserQuotaOperation : RemoteOperation() { - override fun run(client: OwnCloudClient): RemoteOperationResult { - lateinit var result: RemoteOperationResult + override fun run(client: OwnCloudClient): RemoteOperationResult = try { val propfindMethod = PropfindMethod( URL(client.userFilesWebDavUri.toString()), DavConstants.DEPTH_0, DavUtils.getQuotaPropSet() ) - val status = client.executeHttpMethod(propfindMethod) - if (isSuccess(status)) { - val remoteQuota = readData(propfindMethod.root.properties) - result = RemoteOperationResult(ResultCode.OK).apply { - data = remoteQuota + with(client.executeHttpMethod(propfindMethod)) { + if (isSuccess(this)) { + RemoteOperationResult(ResultCode.OK).apply { + data = readData(propfindMethod.root.properties) + }.also { + Timber.i("Get quota completed: ${it.data} and message: ${it.logMessage}") + } + } else { // synchronization failed + RemoteOperationResult(propfindMethod).also { + Timber.e("Get quota without success: ${it.logMessage}") + } } - Timber.i("Get quota completed: ${result.data} and message: ${result.logMessage}") - - } else { // synchronization failed - result = RemoteOperationResult(propfindMethod) - Timber.e("Get quota without success: ${result.logMessage}") } } catch (e: Exception) { - result = RemoteOperationResult(e) - Timber.e(result.exception, "Get quota: ${result.logMessage}") + RemoteOperationResult(e).also { + Timber.e(it.exception, "Get quota: ${it.logMessage}") + } } - return result - } private fun isSuccess(status: Int) = status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_OK From 6f74907af670bdea41eb07000f1f8849444c8681 Mon Sep 17 00:00:00 2001 From: agarcia Date: Mon, 18 May 2020 15:06:06 +0200 Subject: [PATCH 05/10] Migrate GetRemoteUserAvatarOperation to kotlin --- .../users/GetRemoteUserAvatarOperation.java | 185 ------------------ .../users/GetRemoteUserAvatarOperation.kt | 109 +++++++++++ .../lib/resources/users/RemoteAvatarData.kt | 30 +++ .../resources/users/services/UserService.kt | 6 +- .../services/implementation/OCUserService.kt | 8 +- 5 files changed, 150 insertions(+), 188 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/RemoteAvatarData.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.java deleted file mode 100644 index e9e78df6..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.java +++ /dev/null @@ -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 { - - 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 run(OwnCloudClient client) { - GetMethod getMethod = null; - RemoteOperationResult 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; - } - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt new file mode 100644 index 00000000..18191218 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -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() { + override fun run(client: OwnCloudClient): RemoteOperationResult { + lateinit var inputStream: InputStream + lateinit var getMethod: GetMethod + + lateinit var result: RemoteOperationResult + 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/" + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/RemoteAvatarData.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/RemoteAvatarData.kt new file mode 100644 index 00000000..1f68b7b0 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/RemoteAvatarData.kt @@ -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 = "" +) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt index 1030aff2..bf5c63c9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt @@ -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.resources.Service import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation +import com.owncloud.android.lib.resources.users.RemoteAvatarData import com.owncloud.android.lib.resources.users.RemoteUserInfo -interface UserService: Service { - fun getUserInfo() : RemoteOperationResult +interface UserService : Service { + fun getUserInfo(): RemoteOperationResult fun getUserQuota(): RemoteOperationResult + fun getUserAvatar(dimension: Int): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt index d4b40e06..33e301ba 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt @@ -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.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.GetRemoteUserQuotaOperation +import com.owncloud.android.lib.resources.users.RemoteAvatarData import com.owncloud.android.lib.resources.users.RemoteUserInfo 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 { override fun getUserInfo(): RemoteOperationResult = GetRemoteUserInfoOperation().execute(client) override fun getUserQuota(): RemoteOperationResult = GetRemoteUserQuotaOperation().execute(client) + + override fun getUserAvatar(dimension: Int): RemoteOperationResult = + GetRemoteUserAvatarOperation(avatarDimension).execute(client) + } From d997c84a809a2aadeea57f3406a9dffe615be6e4 Mon Sep 17 00:00:00 2001 From: agarcia Date: Tue, 19 May 2020 09:53:28 +0200 Subject: [PATCH 06/10] Keep working on avatar rearquitecture --- .../android/lib/resources/users/services/UserService.kt | 2 +- .../resources/users/services/implementation/OCUserService.kt | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt index bf5c63c9..a90b0cd9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt @@ -33,5 +33,5 @@ import com.owncloud.android.lib.resources.users.RemoteUserInfo interface UserService : Service { fun getUserInfo(): RemoteOperationResult fun getUserQuota(): RemoteOperationResult - fun getUserAvatar(dimension: Int): RemoteOperationResult + fun getUserAvatar(): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt index 33e301ba..3a089c97 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt @@ -28,15 +28,14 @@ import com.owncloud.android.lib.resources.users.RemoteAvatarData import com.owncloud.android.lib.resources.users.RemoteUserInfo import com.owncloud.android.lib.resources.users.services.UserService -class OCUserService(override val client: OwnCloudClient, private val avatarDimension: Int) : - UserService { +class OCUserService(override val client: OwnCloudClient, private val avatarDimension: Int) : UserService { override fun getUserInfo(): RemoteOperationResult = GetRemoteUserInfoOperation().execute(client) override fun getUserQuota(): RemoteOperationResult = GetRemoteUserQuotaOperation().execute(client) - override fun getUserAvatar(dimension: Int): RemoteOperationResult = + override fun getUserAvatar(): RemoteOperationResult = GetRemoteUserAvatarOperation(avatarDimension).execute(client) } From ac21650da731667a8d86fbf591253eace3344f5e Mon Sep 17 00:00:00 2001 From: agarcia Date: Mon, 25 May 2020 11:51:13 +0200 Subject: [PATCH 07/10] Manage responses with no avatar --- .../resources/users/GetRemoteUserAvatarOperation.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt index 18191218..44769622 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -38,7 +38,7 @@ import java.net.URL class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult { - lateinit var inputStream: InputStream + var inputStream: InputStream? = null lateinit var getMethod: GetMethod lateinit var result: RemoteOperationResult @@ -71,7 +71,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : Timber.d("Avatar size: Bytes received ${bytesArray.size} of $contentLength") // find out etag - val etag = WebdavUtils.getEtagFromResponse(getMethod); + val etag = WebdavUtils.getEtagFromResponse(getMethod) if (etag.isEmpty()) { Timber.w("Could not read Etag from avatar") } @@ -87,14 +87,14 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : } catch (e: Exception) { result = RemoteOperationResult(e) - Timber.e(e, "Exception while getting OC user avatar"); + Timber.e(e, "Exception while getting OC user avatar") } finally { try { - client.exhaustResponse(inputStream); - inputStream.close() + client.exhaustResponse(inputStream) + inputStream?.close() } catch (i: IOException) { - Timber.e(i, "Unexpected exception closing input stream"); + Timber.e(i, "Unexpected exception closing input stream") } } From 59229aa6ab27f46692b5c60b1127d4b7e0248279 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 5 Jun 2020 12:32:36 +0200 Subject: [PATCH 08/10] Remove avatar dimension from Service constructor --- .../android/lib/resources/users/services/UserService.kt | 2 +- .../resources/users/services/implementation/OCUserService.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt index a90b0cd9..15a201bb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt @@ -33,5 +33,5 @@ import com.owncloud.android.lib.resources.users.RemoteUserInfo interface UserService : Service { fun getUserInfo(): RemoteOperationResult fun getUserQuota(): RemoteOperationResult - fun getUserAvatar(): RemoteOperationResult + fun getUserAvatar(avatarDimension: Int): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt index 3a089c97..d21d2909 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt @@ -28,14 +28,14 @@ import com.owncloud.android.lib.resources.users.RemoteAvatarData import com.owncloud.android.lib.resources.users.RemoteUserInfo import com.owncloud.android.lib.resources.users.services.UserService -class OCUserService(override val client: OwnCloudClient, private val avatarDimension: Int) : UserService { +class OCUserService(override val client: OwnCloudClient) : UserService { override fun getUserInfo(): RemoteOperationResult = GetRemoteUserInfoOperation().execute(client) override fun getUserQuota(): RemoteOperationResult = GetRemoteUserQuotaOperation().execute(client) - override fun getUserAvatar(): RemoteOperationResult = + override fun getUserAvatar(avatarDimension: Int): RemoteOperationResult = GetRemoteUserAvatarOperation(avatarDimension).execute(client) } From d6ea5800eb119c0e683cb867e014745ccd74d2a6 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 5 Jun 2020 15:01:39 +0200 Subject: [PATCH 09/10] Take care when quota available and used are 0 --- .../lib/resources/users/GetRemoteUserQuotaOperation.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index e2e00230..4e6bd84a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -92,7 +92,7 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { quotaUsed = property.quotaUsedBytes } } - + Timber.d("Quota used: $quotaUsed, QuotaAvailable: $quotaAvailable") // If there's a special case, quota available will contain a negative code // -1, PENDING: Not computed yet, e.g. external storage mounted but folder sizes need scanning // -2, UNKNOWN: Storage not accessible, e.g. external storage with no API to ask for the free space @@ -106,8 +106,11 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { ) } else { val totalQuota = quotaAvailable + quotaUsed - val relativeQuota = (quotaUsed * 100).toDouble() / totalQuota - val roundedRelativeQuota = (relativeQuota * 100).roundToLong() / 100.0 + val roundedRelativeQuota = if (totalQuota > 0) { + val relativeQuota = (quotaUsed * 100).toDouble() / totalQuota + (relativeQuota * 100).roundToLong() / 100.0 + } else 0.0 + RemoteQuota(quotaAvailable, quotaUsed, totalQuota, roundedRelativeQuota) } } From 7536ba8f3cae793c98851d0ae33ca16e1bc47eac Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 10 Jun 2020 09:05:06 +0200 Subject: [PATCH 10/10] Apply code review suggestions --- .../users/GetRemoteUserAvatarOperation.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt index 44769622..3ab8cfa8 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -30,24 +30,29 @@ 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 com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR import timber.log.Timber import java.io.IOException import java.io.InputStream import java.net.URL -class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : - RemoteOperation() { +/** + * Gets avatar about the user logged in, if available + * + * @author David A. Velasco + * @author David González Verdugo + */ +class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult { var inputStream: InputStream? = null - lateinit var getMethod: GetMethod + var result: RemoteOperationResult - lateinit var result: RemoteOperationResult try { val endPoint = - client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + "/" + avatarDimension + client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + PATH_SEPARATOR + avatarDimension Timber.d("avatar URI: %s", endPoint) - getMethod = GetMethod(URL(endPoint)) + val getMethod = GetMethod(URL(endPoint)) val status = client.executeHttpMethod(getMethod)