|
| 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 | +} |
0 commit comments