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