|
| 1 | +/** |
| 2 | + * ownCloud Android client application |
| 3 | + * |
| 4 | + * @author Jorge Aguado Recio |
| 5 | + * |
| 6 | + * Copyright (C) 2026 ownCloud GmbH. |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License version 2, |
| 10 | + * as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.owncloud.android.lib.resources.links |
| 22 | + |
| 23 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 24 | +import com.owncloud.android.lib.common.http.HttpConstants |
| 25 | +import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_JSON |
| 26 | +import com.owncloud.android.lib.common.http.methods.nonwebdav.PatchMethod |
| 27 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 28 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 29 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode |
| 30 | +import okhttp3.MediaType.Companion.toMediaType |
| 31 | +import okhttp3.RequestBody.Companion.toRequestBody |
| 32 | +import org.json.JSONObject |
| 33 | +import timber.log.Timber |
| 34 | +import java.net.URL |
| 35 | + |
| 36 | +class EditRemoteLinkOperation( |
| 37 | + private val spaceId: String, |
| 38 | + private val linkId: String, |
| 39 | + private val displayName: String, |
| 40 | + private val type: String, |
| 41 | + private val expirationDate: String? |
| 42 | +): RemoteOperation<Unit>() { |
| 43 | + override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { |
| 44 | + var result: RemoteOperationResult<Unit> |
| 45 | + try { |
| 46 | + val uriBuilder = client.baseUri.buildUpon().apply { |
| 47 | + appendEncodedPath(GRAPH_API_DRIVES_PATH) |
| 48 | + appendEncodedPath(spaceId) |
| 49 | + appendEncodedPath(GRAPH_API_ROOT_PERMISSIONS_PATH) |
| 50 | + appendEncodedPath(linkId) |
| 51 | + } |
| 52 | + |
| 53 | + val requestBody = JSONObject().apply { |
| 54 | + put(EXPIRATION_DATE_BODY_PARAM, expirationDate ?: JSONObject.NULL) |
| 55 | + put(LINK_BODY_PARAM, JSONObject().apply { |
| 56 | + put(DISPLAY_NAME_BODY_PARAM, displayName) |
| 57 | + put(TYPE_BODY_PARAM, type) |
| 58 | + }) |
| 59 | + }.toString().toRequestBody(CONTENT_TYPE_JSON.toMediaType()) |
| 60 | + |
| 61 | + val patchMethod = PatchMethod(URL(uriBuilder.build().toString()), requestBody) |
| 62 | + |
| 63 | + val status = client.executeHttpMethod(patchMethod) |
| 64 | + |
| 65 | + val response = patchMethod.getResponseBodyAsString() |
| 66 | + |
| 67 | + if (status == HttpConstants.HTTP_OK) { |
| 68 | + Timber.d("Successful response: $response") |
| 69 | + result = RemoteOperationResult(ResultCode.OK) |
| 70 | + Timber.d("Edit public link operation completed and parsed to ${result.data}") |
| 71 | + } else { |
| 72 | + result = RemoteOperationResult(patchMethod) |
| 73 | + Timber.e("Failed response while editing a public link; status code: $status, response: $response") |
| 74 | + } |
| 75 | + } catch (e: Exception) { |
| 76 | + result = RemoteOperationResult(e) |
| 77 | + Timber.e(e, "Exception while editing a public link") |
| 78 | + } |
| 79 | + return result |
| 80 | + } |
| 81 | + |
| 82 | + companion object { |
| 83 | + private const val GRAPH_API_DRIVES_PATH = "graph/v1beta1/drives/" |
| 84 | + private const val GRAPH_API_ROOT_PERMISSIONS_PATH = "root/permissions" |
| 85 | + private const val EXPIRATION_DATE_BODY_PARAM = "expirationDateTime" |
| 86 | + private const val LINK_BODY_PARAM = "link" |
| 87 | + private const val DISPLAY_NAME_BODY_PARAM = "@libre.graph.displayName" |
| 88 | + private const val TYPE_BODY_PARAM = "type" |
| 89 | + } |
| 90 | +} |
0 commit comments