697c154ed77b786101d7fb636a0ea9ae304944fa
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / ChunkedUploadFileOperation.java
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.operations;
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.Log_OC;
31 import com.owncloud.android.datamodel.OCFile;
32 import com.owncloud.android.network.ProgressiveDataTransferer;
33
34 import android.accounts.Account;
35
36 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
37 import eu.alefzero.webdav.WebdavClient;
38 import eu.alefzero.webdav.WebdavUtils;
39
40 public class ChunkedUploadFileOperation extends UploadFileOperation {
41
42 private static final long CHUNK_SIZE = 1024000;
43 private static final String OC_CHUNKED_HEADER = "OC-Chunked";
44 private static final String TAG = ChunkedUploadFileOperation.class.getSimpleName();
45
46 public ChunkedUploadFileOperation( Account account,
47 OCFile file,
48 boolean isInstant,
49 boolean forceOverwrite,
50 int localBehaviour) {
51
52 super(account, file, isInstant, forceOverwrite, localBehaviour);
53 }
54
55 @Override
56 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
57 int status = -1;
58
59 FileChannel channel = null;
60 RandomAccessFile raf = null;
61 try {
62 File file = new File(getStoragePath());
63 raf = new RandomAccessFile(file, "r");
64 channel = raf.getChannel();
65 mEntity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE, file);
66 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners());
67 long offset = 0;
68 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
69 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
70 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
71 mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
72 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
73 ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset);
74 mPutMethod.setRequestEntity(mEntity);
75 status = client.executeMethod(mPutMethod);
76 client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
77 Log_OC.d(TAG, "Upload of " + getStoragePath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
78 if (!isSuccess(status))
79 break;
80 }
81
82 } finally {
83 if (channel != null)
84 channel.close();
85 if (raf != null)
86 raf.close();
87 if (mPutMethod != null)
88 mPutMethod.releaseConnection(); // let the connection available for other methods
89 }
90 return status;
91 }
92
93 }