Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 6f74907

Browse files
committed
Migrate GetRemoteUserAvatarOperation to kotlin
1 parent 8a88fbd commit 6f74907

File tree

5 files changed

+150
-188
lines changed

5 files changed

+150
-188
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.java

Lines changed: 0 additions & 185 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 timber.log.Timber
34+
import java.io.IOException
35+
import java.io.InputStream
36+
import java.net.URL
37+
38+
class GetRemoteUserAvatarOperation(private val avatarDimension: Int) :
39+
RemoteOperation<RemoteAvatarData>() {
40+
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteAvatarData> {
41+
lateinit var inputStream: InputStream
42+
lateinit var getMethod: GetMethod
43+
44+
lateinit var result: RemoteOperationResult<RemoteAvatarData>
45+
try {
46+
val endPoint =
47+
client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + "/" + avatarDimension
48+
Timber.d("avatar URI: %s", endPoint)
49+
50+
getMethod = GetMethod(URL(endPoint))
51+
52+
val status = client.executeHttpMethod(getMethod)
53+
54+
if (isSuccess(status)) {
55+
// find out size of file to read
56+
val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER).toInt()
57+
58+
// find out MIME-type!
59+
val mimeType = getMethod.getResponseHeader(HttpConstants.CONTENT_TYPE_HEADER)
60+
61+
if (mimeType == null || !mimeType.startsWith("image")) {
62+
Timber.w("Not an image, failing with no avatar")
63+
return RemoteOperationResult(RemoteOperationResult.ResultCode.FILE_NOT_FOUND)
64+
}
65+
66+
/// download will be performed to a buffer
67+
inputStream = getMethod.responseBodyAsStream
68+
val bytesArray = inputStream.readBytes()
69+
70+
// TODO check total bytes transferred?
71+
Timber.d("Avatar size: Bytes received ${bytesArray.size} of $contentLength")
72+
73+
// find out etag
74+
val etag = WebdavUtils.getEtagFromResponse(getMethod);
75+
if (etag.isEmpty()) {
76+
Timber.w("Could not read Etag from avatar")
77+
}
78+
79+
// Result
80+
result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
81+
result.setData(RemoteAvatarData(bytesArray, mimeType, etag))
82+
83+
} else {
84+
result = RemoteOperationResult(getMethod)
85+
client.exhaustResponse(getMethod.responseBodyAsStream)
86+
}
87+
88+
} catch (e: Exception) {
89+
result = RemoteOperationResult(e)
90+
Timber.e(e, "Exception while getting OC user avatar");
91+
92+
} finally {
93+
try {
94+
client.exhaustResponse(inputStream);
95+
inputStream.close()
96+
} catch (i: IOException) {
97+
Timber.e(i, "Unexpected exception closing input stream");
98+
}
99+
}
100+
101+
return result
102+
}
103+
104+
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
105+
106+
companion object {
107+
private const val NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"
108+
}
109+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
package com.owncloud.android.lib.resources.users
25+
26+
data class RemoteAvatarData(
27+
val avatarData: ByteArray = byteArrayOf(),
28+
val mimeType: String = "",
29+
val eTag: String = ""
30+
)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/UserService.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ package com.owncloud.android.lib.resources.users.services
2727
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2828
import com.owncloud.android.lib.resources.Service
2929
import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation
30+
import com.owncloud.android.lib.resources.users.RemoteAvatarData
3031
import com.owncloud.android.lib.resources.users.RemoteUserInfo
3132

32-
interface UserService: Service {
33-
fun getUserInfo() : RemoteOperationResult<RemoteUserInfo>
33+
interface UserService : Service {
34+
fun getUserInfo(): RemoteOperationResult<RemoteUserInfo>
3435
fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota>
36+
fun getUserAvatar(dimension: Int): RemoteOperationResult<RemoteAvatarData>
3537
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/services/implementation/OCUserService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ package com.owncloud.android.lib.resources.users.services.implementation
2121

2222
import com.owncloud.android.lib.common.OwnCloudClient
2323
import com.owncloud.android.lib.common.operations.RemoteOperationResult
24+
import com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation
2425
import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation
2526
import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation
27+
import com.owncloud.android.lib.resources.users.RemoteAvatarData
2628
import com.owncloud.android.lib.resources.users.RemoteUserInfo
2729
import com.owncloud.android.lib.resources.users.services.UserService
2830

29-
class OCUserService(override val client: OwnCloudClient) :
31+
class OCUserService(override val client: OwnCloudClient, private val avatarDimension: Int) :
3032
UserService {
3133
override fun getUserInfo(): RemoteOperationResult<RemoteUserInfo> =
3234
GetRemoteUserInfoOperation().execute(client)
3335

3436
override fun getUserQuota(): RemoteOperationResult<GetRemoteUserQuotaOperation.RemoteQuota> =
3537
GetRemoteUserQuotaOperation().execute(client)
38+
39+
override fun getUserAvatar(dimension: Int): RemoteOperationResult<RemoteAvatarData> =
40+
GetRemoteUserAvatarOperation(avatarDimension).execute(client)
41+
3642
}

0 commit comments

Comments
 (0)