|
| 1 | +/* |
| 2 | + * Nextcloud Android Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 TSI-mc <[email protected]> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.owncloud.android.lib.resources.albums; |
| 9 | + |
| 10 | +import android.util.Log; |
| 11 | + |
| 12 | +import com.nextcloud.common.SessionTimeOut; |
| 13 | +import com.nextcloud.common.SessionTimeOutKt; |
| 14 | +import com.owncloud.android.lib.common.OwnCloudClient; |
| 15 | +import com.owncloud.android.lib.common.network.WebdavUtils; |
| 16 | +import com.owncloud.android.lib.common.operations.RemoteOperation; |
| 17 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult; |
| 18 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; |
| 19 | + |
| 20 | +import org.apache.commons.httpclient.HttpStatus; |
| 21 | +import org.apache.jackrabbit.webdav.DavException; |
| 22 | +import org.apache.jackrabbit.webdav.MultiStatusResponse; |
| 23 | +import org.apache.jackrabbit.webdav.Status; |
| 24 | +import org.apache.jackrabbit.webdav.client.methods.CopyMethod; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Remote operation moving a remote file or folder in the ownCloud server to a different folder |
| 31 | + * in the same account. |
| 32 | + * <p> |
| 33 | + * Allows renaming the moving file/folder at the same time. |
| 34 | + */ |
| 35 | +public class CopyFileToAlbumRemoteOperation extends RemoteOperation { |
| 36 | + private static final String TAG = CopyFileToAlbumRemoteOperation.class.getSimpleName(); |
| 37 | + |
| 38 | + private final String mSrcRemotePath; |
| 39 | + private final String mTargetRemotePath; |
| 40 | + |
| 41 | + private final SessionTimeOut sessionTimeOut; |
| 42 | + |
| 43 | + public CopyFileToAlbumRemoteOperation(String srcRemotePath, String targetRemotePath) { |
| 44 | + this(srcRemotePath, targetRemotePath, SessionTimeOutKt.getDefaultSessionTimeOut()); |
| 45 | + } |
| 46 | + |
| 47 | + public CopyFileToAlbumRemoteOperation(String srcRemotePath, String targetRemotePath, SessionTimeOut sessionTimeOut) { |
| 48 | + mSrcRemotePath = srcRemotePath; |
| 49 | + mTargetRemotePath = targetRemotePath; |
| 50 | + this.sessionTimeOut = sessionTimeOut; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Performs the operation. |
| 55 | + * |
| 56 | + * @param client Client object to communicate with the remote ownCloud server. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + protected RemoteOperationResult run(OwnCloudClient client) { |
| 60 | + |
| 61 | + /// check parameters |
| 62 | + if (mTargetRemotePath.equals(mSrcRemotePath)) { |
| 63 | + // nothing to do! |
| 64 | + return new RemoteOperationResult<>(ResultCode.OK); |
| 65 | + } |
| 66 | + |
| 67 | + if (mTargetRemotePath.startsWith(mSrcRemotePath)) { |
| 68 | + return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT); |
| 69 | + } |
| 70 | + |
| 71 | + /// perform remote operation |
| 72 | + CopyMethod copyMethod = null; |
| 73 | + RemoteOperationResult result; |
| 74 | + try { |
| 75 | + copyMethod = new CopyMethod( |
| 76 | + client.getFilesDavUri(this.mSrcRemotePath), |
| 77 | + client.getBaseUri() + "/remote.php/dav/photos/" + client.getUserId() + "/albums" + WebdavUtils.encodePath(mTargetRemotePath), |
| 78 | + false |
| 79 | + ); |
| 80 | + int status = client.executeMethod(copyMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); |
| 81 | + |
| 82 | + /// process response |
| 83 | + if (status == HttpStatus.SC_MULTI_STATUS) { |
| 84 | + result = processPartialError(copyMethod); |
| 85 | + |
| 86 | + } else if (status == HttpStatus.SC_PRECONDITION_FAILED) { |
| 87 | + |
| 88 | + result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); |
| 89 | + client.exhaustResponse(copyMethod.getResponseBodyAsStream()); |
| 90 | + |
| 91 | + } else { |
| 92 | + result = new RemoteOperationResult<>(isSuccess(status), copyMethod); |
| 93 | + client.exhaustResponse(copyMethod.getResponseBodyAsStream()); |
| 94 | + } |
| 95 | + |
| 96 | + Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage()); |
| 97 | + |
| 98 | + } catch (Exception e) { |
| 99 | + result = new RemoteOperationResult<>(e); |
| 100 | + Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e); |
| 101 | + |
| 102 | + } finally { |
| 103 | + if (copyMethod != null) { |
| 104 | + copyMethod.releaseConnection(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * Analyzes a multistatus response from the OC server to generate an appropriate result. |
| 114 | + * <p> |
| 115 | + * In WebDAV, a COPY request on collections (folders) can be PARTIALLY successful: some |
| 116 | + * children are copied, some other aren't. |
| 117 | + * <p> |
| 118 | + * According to the WebDAV specification, a multistatus response SHOULD NOT include partial |
| 119 | + * successes (201, 204) nor for descendants of already failed children (424) in the response |
| 120 | + * entity. But SHOULD NOT != MUST NOT, so take carefully. |
| 121 | + * |
| 122 | + * @param copyMethod Copy operation just finished with a multistatus response |
| 123 | + * @return A result for the {@link CopyFileToAlbumRemoteOperation} caller |
| 124 | + * @throws java.io.IOException If the response body could not be parsed |
| 125 | + * @throws org.apache.jackrabbit.webdav.DavException If the status code is other than MultiStatus or if obtaining |
| 126 | + * the response XML document fails |
| 127 | + */ |
| 128 | + private RemoteOperationResult processPartialError(CopyMethod copyMethod) |
| 129 | + throws IOException, DavException { |
| 130 | + // Adding a list of failed descendants to the result could be interesting; or maybe not. |
| 131 | + // For the moment, let's take the easy way. |
| 132 | + |
| 133 | + /// check that some error really occurred |
| 134 | + MultiStatusResponse[] responses = copyMethod.getResponseBodyAsMultiStatus().getResponses(); |
| 135 | + Status[] status; |
| 136 | + boolean failFound = false; |
| 137 | + for (int i = 0; i < responses.length && !failFound; i++) { |
| 138 | + status = responses[i].getStatus(); |
| 139 | + failFound = ( |
| 140 | + status != null && |
| 141 | + status.length > 0 && |
| 142 | + status[0].getStatusCode() > 299 |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + RemoteOperationResult result; |
| 147 | + if (failFound) { |
| 148 | + result = new RemoteOperationResult<>(ResultCode.PARTIAL_COPY_DONE); |
| 149 | + } else { |
| 150 | + result = new RemoteOperationResult<>(true, copyMethod); |
| 151 | + } |
| 152 | + |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | + protected boolean isSuccess(int status) { |
| 157 | + return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT; |
| 158 | + } |
| 159 | +} |
0 commit comments