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

Commit 0ea053d

Browse files
authored
Merge pull request #503 from owncloud/feature/open_in_web
[Feature] Open in web
2 parents 24b850d + 0be462c commit 0ea053d

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2022 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
package com.owncloud.android.lib.resources.files
25+
26+
import com.owncloud.android.lib.common.OwnCloudClient
27+
import com.owncloud.android.lib.common.http.HttpConstants
28+
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
29+
import com.owncloud.android.lib.common.network.WebdavUtils
30+
import com.owncloud.android.lib.common.operations.RemoteOperation
31+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
32+
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
33+
import com.owncloud.android.lib.resources.files.GetUrlToOpenInWebRemoteOperation.OpenInWebParams.Companion.PARAM_FILE_ID
34+
import com.squareup.moshi.JsonAdapter
35+
import com.squareup.moshi.JsonClass
36+
import com.squareup.moshi.Moshi
37+
import okhttp3.FormBody
38+
import okhttp3.RequestBody
39+
import timber.log.Timber
40+
import java.net.URL
41+
import java.util.concurrent.TimeUnit
42+
43+
class GetUrlToOpenInWebRemoteOperation(
44+
val openWithWebEndpoint: String,
45+
val fileId: String,
46+
) : RemoteOperation<String>() {
47+
48+
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
49+
return try {
50+
51+
val openInWebRequestBody = OpenInWebParams(fileId).toRequestBody()
52+
53+
val stringUrl = client.baseUri.toString() + WebdavUtils.encodePath(openWithWebEndpoint) + "?$PARAM_FILE_ID=$fileId"
54+
55+
val postMethod = PostMethod(URL(stringUrl), openInWebRequestBody).apply {
56+
setReadTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
57+
setConnectionTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
58+
}
59+
60+
val status = client.executeHttpMethod(postMethod)
61+
Timber.d("Open in web for file: $fileId - $status${if (!isSuccess(status)) "(FAIL)" else ""}")
62+
63+
if (isSuccess(status)) RemoteOperationResult<String>(ResultCode.OK).apply {
64+
val moshi = Moshi.Builder().build()
65+
val adapter: JsonAdapter<OpenInWebResponse> = moshi.adapter(OpenInWebResponse::class.java)
66+
67+
data = postMethod.getResponseBodyAsString()?.let { adapter.fromJson(it)!!.uri }
68+
}
69+
else RemoteOperationResult<String>(postMethod).apply { data = "" }
70+
71+
} catch (e: Exception) {
72+
val result = RemoteOperationResult<String>(e)
73+
Timber.e(e, "Open in web for file: $fileId failed")
74+
result
75+
}
76+
}
77+
78+
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS
79+
80+
data class OpenInWebParams(val fileId: String) {
81+
fun toRequestBody(): RequestBody =
82+
FormBody.Builder().build()
83+
84+
companion object {
85+
const val PARAM_FILE_ID = "file_id"
86+
}
87+
}
88+
89+
@JsonClass(generateAdapter = true)
90+
data class OpenInWebResponse(val uri: String)
91+
92+
companion object {
93+
/**
94+
* Maximum time to wait for a response from the server in milliseconds.
95+
*/
96+
private const val TIMEOUT = 5_000L
97+
}
98+
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ import com.owncloud.android.lib.resources.Service
2828

2929
interface FileService : Service {
3030
fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Boolean>
31+
fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String): RemoteOperationResult<String>
3132
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package com.owncloud.android.lib.resources.files.services.implementation
2626
import com.owncloud.android.lib.common.OwnCloudClient
2727
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2828
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
29+
import com.owncloud.android.lib.resources.files.GetUrlToOpenInWebRemoteOperation
2930
import com.owncloud.android.lib.resources.files.services.FileService
3031

3132
class OCFileService(override val client: OwnCloudClient) :
@@ -35,4 +36,8 @@ class OCFileService(override val client: OwnCloudClient) :
3536
remotePath = path,
3637
isUserLoggedIn = isUserLogged
3738
).execute(client)
39+
40+
override fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String): RemoteOperationResult<String> =
41+
GetUrlToOpenInWebRemoteOperation(openWithWebEndpoint = openWebEndpoint, fileId = fileId).execute(client)
42+
3843
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/RemoteCapability.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ data class RemoteCapability(
6666
// Files
6767
var filesBigFileChunking: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
6868
var filesUndelete: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
69-
var filesVersioning: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN
69+
var filesVersioning: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
70+
val filesPrivateLinks: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
71+
val filesAppProviders: List<RemoteOCISProvider>?,
7072
) {
7173
/**
7274
* Enum for Boolean Type in capabilities, with values:
@@ -98,4 +100,13 @@ data class RemoteCapability(
98100
}
99101
}
100102
}
103+
104+
data class RemoteOCISProvider(
105+
val enabled: Boolean,
106+
val version: String,
107+
val appsUrl: String?,
108+
val openUrl: String?,
109+
val openWebUrl: String?,
110+
val newUrl: String?,
111+
)
101112
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/responses/CapabilityResponse.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package com.owncloud.android.lib.resources.status.responses
2626

2727
import com.owncloud.android.lib.resources.status.RemoteCapability
2828
import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType
29+
import com.owncloud.android.lib.resources.status.RemoteCapability.RemoteOCISProvider
2930
import com.squareup.moshi.Json
3031
import com.squareup.moshi.JsonClass
3132

@@ -68,6 +69,8 @@ data class CapabilityResponse(
6869
filesBigFileChunking = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.bigfilechunking),
6970
filesUndelete = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.undelete),
7071
filesVersioning = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.versioning),
72+
filesPrivateLinks = capabilities?.fileCapabilities?.privateLinks?.let { CapabilityBooleanType.fromBooleanValue(it) } ?: CapabilityBooleanType.UNKNOWN,
73+
filesAppProviders = capabilities?.fileCapabilities?.appProviders?.map { it.toOCISProvider() },
7174
filesSharingFederationIncoming = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.incoming),
7275
filesSharingFederationOutgoing = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.outgoing),
7376
filesSharingUserProfilePicture = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingUser?.profilePicture),
@@ -160,9 +163,28 @@ data class FileSharingUser(
160163
data class FileCapabilities(
161164
val bigfilechunking: Boolean?,
162165
val undelete: Boolean?,
163-
val versioning: Boolean?
166+
val versioning: Boolean?,
167+
val privateLinks: Boolean?,
168+
@Json(name = "app_providers")
169+
val appProviders: List<AppProvider>?
164170
)
165171

172+
@JsonClass(generateAdapter = true)
173+
data class AppProvider(
174+
val enabled: Boolean,
175+
val version: String,
176+
@Json(name = "apps_url")
177+
val appsUrl: String?,
178+
@Json(name = "open_url")
179+
val openUrl: String?,
180+
@Json(name = "open_web_url")
181+
val openWebUrl: String?,
182+
@Json(name = "new_url")
183+
val newUrl: String?,
184+
) {
185+
fun toOCISProvider() = RemoteOCISProvider(enabled, version, appsUrl, openUrl, openWebUrl, newUrl)
186+
}
187+
166188
@JsonClass(generateAdapter = true)
167189
data class DavCapabilities(
168190
val chunking: String?

0 commit comments

Comments
 (0)