Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 79173af

Browse files
committed
Polish a little bit the code
1 parent c36eedd commit 79173af

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/ChunkFromFileRequestBody.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.network
2525

26+
import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSystemOperation.Companion.CHUNK_SIZE
2627
import okhttp3.MediaType
2728
import okio.BufferedSink
2829
import timber.log.Timber
@@ -39,7 +40,7 @@ class ChunkFromFileRequestBody(
3940
file: File,
4041
contentType: MediaType?,
4142
private val channel: FileChannel,
42-
private val chunkSize: Long
43+
private val chunkSize: Long = CHUNK_SIZE
4344
) : FileRequestBody(file, contentType) {
4445

4546
private var offset: Long = 0

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadFromFileSystemOperation.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,27 @@ class ChunkedUploadFromFileSystemOperation(
6969

7070
val fileToUpload = File(localPath)
7171
val mediaType: MediaType? = mimeType.toMediaTypeOrNull()
72-
val raf: RandomAccessFile = RandomAccessFile(fileToUpload, MODE_READ_ONLY)
72+
val raf = RandomAccessFile(fileToUpload, MODE_READ_ONLY)
7373
val channel: FileChannel = raf.channel
7474

75-
fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE).also {
75+
val fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel).also {
7676
synchronized(dataTransferListener) { it.addDatatransferProgressListeners(dataTransferListener) }
7777
}
7878

7979
val uriPrefix = client.uploadsWebDavUri.toString() + File.separator + transferId
8080
val totalLength = fileToUpload.length()
8181
val chunkCount = ceil(totalLength.toDouble() / CHUNK_SIZE).toLong()
82-
var chunkIndex = 0
8382
var offset: Long = 0
8483

85-
while (chunkIndex < chunkCount) {
86-
(fileRequestBody as ChunkFromFileRequestBody).setOffset(offset)
84+
for (chunkIndex in 0..chunkCount) {
85+
fileRequestBody.setOffset(offset)
8786

8887
if (cancellationRequested.get()) {
8988
result = RemoteOperationResult<Unit>(OperationCancelledException())
9089
break
9190
} else {
92-
putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody as ChunkFromFileRequestBody).apply {
93-
if (chunkIndex.toLong() == chunkCount - 1) {
91+
putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody).apply {
92+
if (chunkIndex == chunkCount - 1) {
9493
// Added a high timeout to the last chunk due to when the last chunk
9594
// arrives to the server with the last PUT, all chunks get assembled
9695
// within that PHP request, so last one takes longer.
@@ -109,7 +108,6 @@ class ChunkedUploadFromFileSystemOperation(
109108
break
110109
}
111110
}
112-
chunkIndex++
113111
offset += CHUNK_SIZE
114112
}
115113
channel.close()

0 commit comments

Comments
 (0)