diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkUploadListener.kt b/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkUploadListener.kt new file mode 100644 index 0000000000..81500d7060 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkUploadListener.kt @@ -0,0 +1,12 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.files + +interface ChunkUploadListener { + fun isCancelled(): Boolean = false +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkedFileUploadRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkedFileUploadRemoteOperation.java index 818c4e0e4a..fbc4dc6652 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkedFileUploadRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ChunkedFileUploadRemoteOperation.java @@ -50,6 +50,7 @@ public class ChunkedFileUploadRemoteOperation extends UploadFileRemoteOperation private final boolean onWifiConnection; private String uploadFolderUri; private String destinationUri; + private ChunkUploadListener chunkUploadListener; public ChunkedFileUploadRemoteOperation(String storagePath, String remotePath, @@ -117,6 +118,10 @@ public ChunkedFileUploadRemoteOperation(String storagePath, this.onWifiConnection = onWifiConnection; } + public void setChunkUploadListener(ChunkUploadListener listener) { + chunkUploadListener = listener; + } + protected static Chunk calcNextChunk(long fileSize, int chunkId, long startByte, long chunkSize) { if (chunkId < 0 || String.valueOf(chunkId).length() > CHUNK_NAME_LENGTH) { throw new IllegalArgumentException( @@ -198,13 +203,13 @@ protected RemoteOperationResult run(OwnCloudClient client) { // determine size of next chunk Chunk chunk = calcNextChunk(file.length(), ++lastId, nextByte, chunkSize); - RemoteOperationResult chunkResult = uploadChunk(client, chunk); - if (!chunkResult.isSuccess()) { - return chunkResult; + if (cancellationRequested.get() || (chunkUploadListener != null && chunkUploadListener.isCancelled())) { + return new RemoteOperationResult<>(new OperationCancelledException()); } - if (cancellationRequested.get()) { - return new RemoteOperationResult(new OperationCancelledException()); + final var chunkResult = uploadChunk(client, chunk); + if (!chunkResult.isSuccess()) { + return chunkResult; } nextByte += chunk.getLength();