mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Cancel chunk upload and implement move files after uploading
This commit is contained in:
		
							parent
							
								
									c8a0451e06
								
							
						
					
					
						commit
						78613ed4bc
					
				| @ -28,11 +28,11 @@ import com.owncloud.android.lib.common.OwnCloudClient; | |||||||
| import com.owncloud.android.lib.common.http.HttpUtils; | import com.owncloud.android.lib.common.http.HttpUtils; | ||||||
| import com.owncloud.android.lib.common.http.methods.webdav.PutMethod; | import com.owncloud.android.lib.common.http.methods.webdav.PutMethod; | ||||||
| import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody; | 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; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.IOException; |  | ||||||
| import java.io.RandomAccessFile; | import java.io.RandomAccessFile; | ||||||
| import java.nio.channels.FileChannel; | import java.nio.channels.FileChannel; | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
| @ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit; | |||||||
| import okhttp3.MediaType; | import okhttp3.MediaType; | ||||||
| 
 | 
 | ||||||
| import static com.owncloud.android.lib.common.http.HttpConstants.IF_MATCH_HEADER; | import static com.owncloud.android.lib.common.http.HttpConstants.IF_MATCH_HEADER; | ||||||
| import static com.owncloud.android.lib.common.http.HttpConstants.OC_TOTAL_LENGTH_HEADER; |  | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -64,46 +63,44 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { |     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws Exception { | ||||||
|         int status; |         int status; | ||||||
|         RemoteOperationResult result = null; |         RemoteOperationResult result = null; | ||||||
|         FileChannel channel = null; |         FileChannel channel; | ||||||
|         RandomAccessFile raf = null; |         RandomAccessFile raf; | ||||||
| 
 | 
 | ||||||
|         try { |         File fileToUpload = new File(mLocalPath); | ||||||
|             File fileToUpload = new File(mLocalPath); |         MediaType mediaType = MediaType.parse(mMimeType); | ||||||
|             MediaType mediaType = MediaType.parse(mMimeType); |  | ||||||
| 
 | 
 | ||||||
|             raf = new RandomAccessFile(fileToUpload, "r"); |         raf = new RandomAccessFile(fileToUpload, "r"); | ||||||
|             channel = raf.getChannel(); |         channel = raf.getChannel(); | ||||||
| 
 | 
 | ||||||
|             mFileRequestBody = new ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE); |         mFileRequestBody = new ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE); | ||||||
| 
 | 
 | ||||||
|             synchronized (mDataTransferListeners) { |         synchronized (mDataTransferListeners) { | ||||||
|                 mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); |             mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         long offset = 0; | ||||||
|  |         String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); | ||||||
|  |         long totalLength = fileToUpload.length(); | ||||||
|  |         long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); | ||||||
|  | 
 | ||||||
|  |         for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { | ||||||
|  |             mPutMethod = new PutMethod( | ||||||
|  |                     HttpUtils.stringUrlToHttpUrl(uriPrefix + FileUtils.PATH_SEPARATOR + chunkIndex) | ||||||
|  |             ); | ||||||
|  | 
 | ||||||
|  |             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||||
|  |                 mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             long offset = 0; |             ((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset); | ||||||
|             String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); |  | ||||||
|             long totalLength = fileToUpload.length(); |  | ||||||
|             long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); |  | ||||||
| 
 |  | ||||||
|             for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { |  | ||||||
|                 mPutMethod = new PutMethod( |  | ||||||
|                         HttpUtils.stringUrlToHttpUrl(uriPrefix + FileUtils.PATH_SEPARATOR + chunkIndex) |  | ||||||
|                 ); |  | ||||||
| 
 |  | ||||||
|                 if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |  | ||||||
|                     mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 ((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset); |  | ||||||
| 
 |  | ||||||
| //                if (mCancellationRequested.get()) { |  | ||||||
| //                    mPutMethod.abort(); |  | ||||||
| //                    // next method will throw an exception |  | ||||||
| //                } |  | ||||||
| 
 | 
 | ||||||
|  |             if (mCancellationRequested.get()) { | ||||||
|  |                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||||
|  |                 break; | ||||||
|  |             } else { | ||||||
|                 if (chunkIndex == 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 | ||||||
| @ -126,15 +123,14 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|                     break; |                     break; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 |  | ||||||
|         } catch (Exception e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } finally { |  | ||||||
|             if (channel != null) |  | ||||||
|                 channel.close(); |  | ||||||
|             if (raf != null) |  | ||||||
|                 raf.close(); |  | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         if (channel != null) | ||||||
|  |             channel.close(); | ||||||
|  | 
 | ||||||
|  |         if (raf != null) | ||||||
|  |             raf.close(); | ||||||
|  | 
 | ||||||
|         return result; |         return result; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -85,7 +85,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     protected RemoteOperationResult run(OwnCloudClient client) { |     protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|         RemoteOperationResult result = null; |         RemoteOperationResult result; | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
|             mPutMethod = new PutMethod( |             mPutMethod = new PutMethod( | ||||||
| @ -96,7 +96,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|             if (mCancellationRequested.get()) { |             if (mCancellationRequested.get()) { | ||||||
|                 // the operation was cancelled before getting it's turn to be executed in the queue of uploads |                 // the operation was cancelled before getting it's turn to be executed in the queue of uploads | ||||||
|                 result = new RemoteOperationResult(new OperationCancelledException()); |                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||||
| 
 |  | ||||||
|             } else { |             } else { | ||||||
|                 // perform the upload |                 // perform the upload | ||||||
|                 result = uploadFile(client); |                 result = uploadFile(client); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user