|
| 1 | +/* Nextcloud Android Library is available under MIT license |
| 2 | + * |
| 3 | + * @author Tobias Kaminsky |
| 4 | + * Copyright (C) 2023 Tobias Kaminsky |
| 5 | + * Copyright (C) 2023 Nextcloud GmbH |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + * |
| 26 | + */ |
| 27 | +package com.owncloud.android.lib.resources.files |
| 28 | + |
| 29 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 30 | +import com.owncloud.android.lib.common.network.WebdavEntry |
| 31 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 32 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 33 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode |
| 34 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 35 | +import org.apache.commons.httpclient.HttpStatus |
| 36 | +import org.apache.jackrabbit.webdav.DavException |
| 37 | +import org.apache.jackrabbit.webdav.client.methods.PropFindMethod |
| 38 | +import org.apache.jackrabbit.webdav.property.DavPropertyName |
| 39 | +import org.apache.jackrabbit.webdav.property.DavPropertyNameSet |
| 40 | +import java.io.File |
| 41 | +import java.io.IOException |
| 42 | + |
| 43 | +/** |
| 44 | + * Check if remaining quota is big enough |
| 45 | + * @param fileSize filesize in bytes |
| 46 | + */ |
| 47 | +class CheckEnoughQuotaRemoteOperation(val path: String, private val fileSize: Long) : |
| 48 | + RemoteOperation<Boolean>() { |
| 49 | + |
| 50 | + @Deprecated("Deprecated in Java") |
| 51 | + @Suppress("Detekt.ReturnCount") |
| 52 | + override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> { |
| 53 | + var propfind: PropFindMethod? = null |
| 54 | + try { |
| 55 | + val file = File(path) |
| 56 | + val folder = if (file.path.endsWith(FileUtils.PATH_SEPARATOR)) { |
| 57 | + file.path |
| 58 | + } else { |
| 59 | + file.parent ?: throw IllegalStateException("Parent path not found") |
| 60 | + } |
| 61 | + |
| 62 | + val propSet = DavPropertyNameSet() |
| 63 | + propSet.add(QUOTA_PROPERTY) |
| 64 | + propfind = PropFindMethod( |
| 65 | + client.getFilesDavUri(folder), |
| 66 | + propSet, |
| 67 | + 0 |
| 68 | + ) |
| 69 | + val status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT) |
| 70 | + if (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK) { |
| 71 | + val resp = propfind.responseBodyAsMultiStatus.responses[0] |
| 72 | + val string = resp.getProperties(HttpStatus.SC_OK)[QUOTA_PROPERTY].value as String |
| 73 | + val quota = string.toLong() |
| 74 | + return if (isSuccess(quota)) { |
| 75 | + RemoteOperationResult<Boolean>(true, propfind) |
| 76 | + } else { |
| 77 | + RemoteOperationResult<Boolean>(false, propfind) |
| 78 | + } |
| 79 | + } |
| 80 | + if (status == HttpStatus.SC_NOT_FOUND) { |
| 81 | + return RemoteOperationResult(ResultCode.FILE_NOT_FOUND) |
| 82 | + } |
| 83 | + } catch (e: DavException) { |
| 84 | + Log_OC.e(TAG, "Error while retrieving quota") |
| 85 | + } catch (e: IOException) { |
| 86 | + Log_OC.e(TAG, "Error while retrieving quota") |
| 87 | + } catch (e: NumberFormatException) { |
| 88 | + Log_OC.e(TAG, "Error while retrieving quota") |
| 89 | + } finally { |
| 90 | + propfind?.releaseConnection() |
| 91 | + } |
| 92 | + return RemoteOperationResult(ResultCode.ETAG_CHANGED) |
| 93 | + } |
| 94 | + |
| 95 | + private fun isSuccess(quota: Long): Boolean { |
| 96 | + return quota >= fileSize || |
| 97 | + quota == UNKNOWN_FREE_SPACE || |
| 98 | + quota == UNCOMPUTED_FREE_SPACE || |
| 99 | + quota == UNLIMITED_FREE_SPACE |
| 100 | + } |
| 101 | + |
| 102 | + companion object { |
| 103 | + private const val SYNC_READ_TIMEOUT = 40000 |
| 104 | + private const val SYNC_CONNECTION_TIMEOUT = 5000 |
| 105 | + private const val UNCOMPUTED_FREE_SPACE = -1L |
| 106 | + private const val UNKNOWN_FREE_SPACE = -2L |
| 107 | + private const val UNLIMITED_FREE_SPACE = -3L |
| 108 | + private val QUOTA_PROPERTY = DavPropertyName.create(WebdavEntry.PROPERTY_QUOTA_AVAILABLE_BYTES) |
| 109 | + private val TAG = CheckEnoughQuotaRemoteOperation::class.java.simpleName |
| 110 | + } |
| 111 | +} |
0 commit comments