mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-11-04 12:28:25 +00:00 
			
		
		
		
	Polish a little bit the code
This commit is contained in:
		
							parent
							
								
									f1d04b8037
								
							
						
					
					
						commit
						df07be666d
					
				@ -23,6 +23,7 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
package com.owncloud.android.lib.common.network
 | 
					package com.owncloud.android.lib.common.network
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSystemOperation.Companion.CHUNK_SIZE
 | 
				
			||||||
import okhttp3.MediaType
 | 
					import okhttp3.MediaType
 | 
				
			||||||
import okio.BufferedSink
 | 
					import okio.BufferedSink
 | 
				
			||||||
import timber.log.Timber
 | 
					import timber.log.Timber
 | 
				
			||||||
@ -39,7 +40,7 @@ class ChunkFromFileRequestBody(
 | 
				
			|||||||
    file: File,
 | 
					    file: File,
 | 
				
			||||||
    contentType: MediaType?,
 | 
					    contentType: MediaType?,
 | 
				
			||||||
    private val channel: FileChannel,
 | 
					    private val channel: FileChannel,
 | 
				
			||||||
    private val chunkSize: Long
 | 
					    private val chunkSize: Long = CHUNK_SIZE
 | 
				
			||||||
) : FileRequestBody(file, contentType) {
 | 
					) : FileRequestBody(file, contentType) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private var offset: Long = 0
 | 
					    private var offset: Long = 0
 | 
				
			||||||
 | 
				
			|||||||
@ -69,28 +69,27 @@ class ChunkedUploadFromFileSystemOperation(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        val fileToUpload = File(localPath)
 | 
					        val fileToUpload = File(localPath)
 | 
				
			||||||
        val mediaType: MediaType? = mimeType.toMediaTypeOrNull()
 | 
					        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
 | 
					        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) }
 | 
					            synchronized(dataTransferListener) { it.addDatatransferProgressListeners(dataTransferListener) }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        val uriPrefix = client.uploadsWebDavUri.toString() + File.separator + transferId
 | 
					        val uriPrefix = client.uploadsWebDavUri.toString() + File.separator + transferId
 | 
				
			||||||
        val totalLength = fileToUpload.length()
 | 
					        val totalLength = fileToUpload.length()
 | 
				
			||||||
        val chunkCount = ceil(totalLength.toDouble() / CHUNK_SIZE).toLong()
 | 
					        val chunkCount = ceil(totalLength.toDouble() / CHUNK_SIZE).toLong()
 | 
				
			||||||
        var chunkIndex = 0
 | 
					 | 
				
			||||||
        var offset: Long = 0
 | 
					        var offset: Long = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        while (chunkIndex < chunkCount) {
 | 
					        for (chunkIndex in 0..chunkCount) {
 | 
				
			||||||
            (fileRequestBody as ChunkFromFileRequestBody).setOffset(offset)
 | 
					            fileRequestBody.setOffset(offset)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (cancellationRequested.get()) {
 | 
					            if (cancellationRequested.get()) {
 | 
				
			||||||
                result = RemoteOperationResult<Unit>(OperationCancelledException())
 | 
					                result = RemoteOperationResult<Unit>(OperationCancelledException())
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody as ChunkFromFileRequestBody).apply {
 | 
					                putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody).apply {
 | 
				
			||||||
                    if (chunkIndex.toLong() == chunkCount - 1) {
 | 
					                    if (chunkIndex == chunkCount - 1) {
 | 
				
			||||||
                        // Added a high timeout to the last chunk due to when the last chunk
 | 
					                        // 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
 | 
					                        // arrives to the server with the last PUT, all chunks get assembled
 | 
				
			||||||
                        // within that PHP request, so last one takes longer.
 | 
					                        // within that PHP request, so last one takes longer.
 | 
				
			||||||
@ -109,7 +108,6 @@ class ChunkedUploadFromFileSystemOperation(
 | 
				
			|||||||
                    break
 | 
					                    break
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            chunkIndex++
 | 
					 | 
				
			||||||
            offset += CHUNK_SIZE
 | 
					            offset += CHUNK_SIZE
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        channel.close()
 | 
					        channel.close()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user