1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Polish a little bit the code

This commit is contained in:
Abel García de Prada 2022-06-08 17:08:24 +02:00 committed by Juan Carlos Garrote
parent f1d04b8037
commit df07be666d
2 changed files with 8 additions and 9 deletions

View File

@ -23,6 +23,7 @@
*/
package com.owncloud.android.lib.common.network
import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSystemOperation.Companion.CHUNK_SIZE
import okhttp3.MediaType
import okio.BufferedSink
import timber.log.Timber
@ -39,7 +40,7 @@ class ChunkFromFileRequestBody(
file: File,
contentType: MediaType?,
private val channel: FileChannel,
private val chunkSize: Long
private val chunkSize: Long = CHUNK_SIZE
) : FileRequestBody(file, contentType) {
private var offset: Long = 0

View File

@ -69,28 +69,27 @@ class ChunkedUploadFromFileSystemOperation(
val fileToUpload = File(localPath)
val mediaType: MediaType? = mimeType.toMediaTypeOrNull()
val raf: RandomAccessFile = RandomAccessFile(fileToUpload, MODE_READ_ONLY)
val raf = RandomAccessFile(fileToUpload, MODE_READ_ONLY)
val channel: FileChannel = raf.channel
fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE).also {
val fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel).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)
for (chunkIndex in 0..chunkCount) {
fileRequestBody.setOffset(offset)
if (cancellationRequested.get()) {
result = RemoteOperationResult<Unit>(OperationCancelledException())
break
} else {
putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody as ChunkFromFileRequestBody).apply {
if (chunkIndex.toLong() == chunkCount - 1) {
putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody).apply {
if (chunkIndex == 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.
@ -109,7 +108,6 @@ class ChunkedUploadFromFileSystemOperation(
break
}
}
chunkIndex++
offset += CHUNK_SIZE
}
channel.close()