diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/FileUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/FileUtils.java index cc159611..1ac0a6fb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/FileUtils.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/FileUtils.java @@ -32,6 +32,7 @@ public class FileUtils { public static final String FINAL_CHUNKS_FILE = ".file"; public static final String MIME_DIR = "DIR"; public static final String MIME_DIR_UNIX = "httpd/unix-directory"; + public static final String MODE_READ_ONLY = "r"; static String getParentPath(String remotePath) { String parentPath = new File(remotePath).getParent(); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadFromFileSystemOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadFromFileSystemOperation.kt new file mode 100644 index 00000000..cc42b6bf --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadFromFileSystemOperation.kt @@ -0,0 +1,124 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2022 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package com.owncloud.android.lib.resources.files.chunks + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.methods.webdav.PutMethod +import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody +import com.owncloud.android.lib.common.operations.OperationCancelledException +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode +import com.owncloud.android.lib.resources.files.FileUtils.MODE_READ_ONLY +import com.owncloud.android.lib.resources.files.UploadFileFromFileSystemOperation +import okhttp3.MediaType +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import timber.log.Timber +import java.io.File +import java.io.RandomAccessFile +import java.net.URL +import java.nio.channels.FileChannel +import java.util.concurrent.TimeUnit +import kotlin.math.ceil + +/** + * Remote operation performing the chunked upload of a remote file to the ownCloud server. + * + * @author David A. Velasco + * @author David González Verdugo + * @author Abel García de Prada + */ +class ChunkedUploadFromFileSystemOperation( + private val transferId: String, + localPath: String, + remotePath: String, + mimeType: String, + lastModifiedTimestamp: String, + requiredEtag: String, +) : UploadFileFromFileSystemOperation( + localPath = localPath, + remotePath = remotePath, + mimeType = mimeType, + lastModifiedTimestamp = lastModifiedTimestamp, + requiredEtag = requiredEtag +) { + + @Throws(Exception::class) + override fun uploadFile(client: OwnCloudClient): RemoteOperationResult { + lateinit var result: RemoteOperationResult + + val fileToUpload = File(localPath) + val mediaType: MediaType? = mimeType.toMediaTypeOrNull() + val raf: RandomAccessFile = RandomAccessFile(fileToUpload, MODE_READ_ONLY) + val channel: FileChannel = raf.channel + + fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE).also { + synchronized(dataTransferListener) { it.addDatatransferProgressListeners(dataTransferListener) } + } + + val uriPrefix = client.uploadsWebDavUri.toString() + File.separator + transferId + val totalLength = fileToUpload.length() + val chunkCount = ceil(totalLength.toDouble() / CHUNK_SIZE).toLong() + var chunkIndex = 0 + var offset: Long = 0 + + while (chunkIndex < chunkCount) { + (fileRequestBody as ChunkFromFileRequestBody).setOffset(offset) + + if (cancellationRequested.get()) { + result = RemoteOperationResult(OperationCancelledException()) + break + } else { + putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody as ChunkFromFileRequestBody).apply { + if (chunkIndex.toLong() == chunkCount - 1) { + // Added a high timeout to the last chunk due to when the last chunk + // arrives to the server with the last PUT, all chunks get assembled + // within that PHP request, so last one takes longer. + setReadTimeout(LAST_CHUNK_TIMEOUT.toLong(), TimeUnit.MILLISECONDS) + } + } + + val status = client.executeHttpMethod(putMethod) + + Timber.d("Upload of $localPath to $remotePath, chunk index $chunkIndex, count $chunkCount, HTTP result status $status") + + if (isSuccess(status)) { + result = RemoteOperationResult(ResultCode.OK) + } else { + result = RemoteOperationResult(putMethod) + break + } + } + chunkIndex++ + offset += CHUNK_SIZE + } + channel.close() + raf.close() + return result + } + + companion object { + const val CHUNK_SIZE = 1_024_000L + private const val LAST_CHUNK_TIMEOUT = 900_000 // 15 mins. + } +}