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
							
								
									08d75d347b
								
							
						
					
					
						commit
						84996b410b
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -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()
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user