4df5fea6ec0b16eac3f0b63c9f508e72da9d3185
[pub/Android/ownCloud.git] /
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.oc_framework.operations.remote;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.RandomAccessFile;
24 import java.nio.channels.FileChannel;
25 import java.util.Random;
26
27 import org.apache.commons.httpclient.HttpException;
28 import org.apache.commons.httpclient.methods.PutMethod;
29
30 import com.owncloud.android.oc_framework.network.ProgressiveDataTransferer;
31 import com.owncloud.android.oc_framework.network.webdav.ChunkFromFileChannelRequestEntity;
32 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
33 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
34
35
36 import android.util.Log;
37
38
39 public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation {
40
41 public static final long CHUNK_SIZE = 1024000;
42 private static final String OC_CHUNKED_HEADER = "OC-Chunked";
43 private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();
44
45 public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType) {
46 super(storagePath, remotePath, mimeType);
47 }
48
49 @Override
50 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
51 int status = -1;
52
53 FileChannel channel = null;
54 RandomAccessFile raf = null;
55 try {
56 File file = new File(mStoragePath);
57 raf = new RandomAccessFile(file, "r");
58 channel = raf.getChannel();
59 mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file);
60 //((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners());
61 synchronized (mDataTransferListeners) {
62 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(mDataTransferListeners);
63 }
64
65 long offset = 0;
66 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
67 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
68 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
69 if (mPutMethod != null) {
70 mPutMethod.releaseConnection(); // let the connection available for other methods
71 }
72 mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
73 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
74 ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset);
75 mPutMethod.setRequestEntity(mEntity);
76 status = client.executeMethod(mPutMethod);
77 client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
78 Log.d(TAG, "Upload of " + mStoragePath + " to " + mRemotePath + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
79 if (!isSuccess(status))
80 break;
81 }
82
83 } finally {
84 if (channel != null)
85 channel.close();
86 if (raf != null)
87 raf.close();
88 if (mPutMethod != null)
89 mPutMethod.releaseConnection(); // let the connection available for other methods
90 }
91 return status;
92 }
93
94 }