Adding cancellation to uploads (WIP)
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / ChunkedUploadFileOperation.java
index 18fc1ef..3e7a29d 100644 (file)
@@ -28,24 +28,27 @@ import java.util.Random;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.PutMethod;
 
+import android.accounts.Account;
+import android.util.Log;
+
 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
-import eu.alefzero.webdav.OnDatatransferProgressListener;
 import eu.alefzero.webdav.WebdavClient;
 import eu.alefzero.webdav.WebdavUtils;
 
 public class ChunkedUploadFileOperation extends UploadFileOperation {
     
-    private static final long CHUNK_SIZE = 8192;
+    private static final long CHUNK_SIZE = 102400;
     private static final String OC_CHUNKED_HEADER = "OC-Chunked";
+    private static final String TAG = ChunkedUploadFileOperation.class.getSimpleName();
 
-    public ChunkedUploadFileOperation(  String localPath, 
+    public ChunkedUploadFileOperation(  Account account,
+                                        String localPath, 
                                         String remotePath, 
                                         String mimeType, 
                                         boolean isInstant, 
-                                        boolean forceOverwrite, 
-                                        OnDatatransferProgressListener dataTransferProgressListener) {
+                                        boolean forceOverwrite) {
         
-        super(localPath, remotePath, mimeType, isInstant, forceOverwrite, dataTransferProgressListener);
+        super(account, localPath, remotePath, mimeType, isInstant, forceOverwrite);
     }
 
     @Override
@@ -55,22 +58,25 @@ public class ChunkedUploadFileOperation extends UploadFileOperation {
         PutMethod put = null;
         FileChannel channel = null;
         FileLock lock = null;
+        RandomAccessFile raf = null;
         try {
             File file = new File(getLocalPath());
-            channel = new RandomAccessFile(file, "rw").getChannel();
+            raf = new RandomAccessFile(file, "rw");
+            channel = raf.getChannel();
             lock = channel.tryLock();
             ChunkFromFileChannelRequestEntity entity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE);
-            entity.setOnDatatransferProgressListener(getDataTransferListener());
+            entity.addOnDatatransferProgressListeners(getDataTransferListeners());
             long offset = 0;
-            String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt()) + "-" ;
+            String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
             long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
             for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
-                put = new PutMethod(uriPrefix + chunkIndex + "-" + chunkCount);
+                put = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
                 put.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
                 entity.setOffset(offset);
                 put.setRequestEntity(entity);
                 status = client.executeMethod(put);
                 client.exhaustResponse(put.getResponseBodyAsStream());
+                Log.d(TAG, "Upload of " + getLocalPath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
                 if (!isSuccess(status))
                     break;
             }
@@ -80,6 +86,8 @@ public class ChunkedUploadFileOperation extends UploadFileOperation {
                 lock.release();
             if (channel != null)
                 channel.close();
+            if (raf != null)
+                raf.close();
             if (put != null)
                 put.releaseConnection();    // let the connection available for other methods
         }