Used Log_OC in all the logs
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.operations;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.RandomAccessFile;
25 import java.nio.channels.FileChannel;
26 import java.util.Random;
27
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.methods.PutMethod;
30
31 import com.owncloud.android.Log_OC;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.network.ProgressiveDataTransferer;
34
35 import android.accounts.Account;
36 import android.util.Log;
37
38 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
39 import eu.alefzero.webdav.WebdavClient;
40 import eu.alefzero.webdav.WebdavUtils;
41
42 public class ChunkedUploadFileOperation extends UploadFileOperation {
43
44 private static final long CHUNK_SIZE = 1024000;
45 private static final String OC_CHUNKED_HEADER = "OC-Chunked";
46 private static final String TAG = ChunkedUploadFileOperation.class.getSimpleName();
47
48 public ChunkedUploadFileOperation( Account account,
49 OCFile file,
50 boolean isInstant,
51 boolean forceOverwrite,
52 int localBehaviour) {
53
54 super(account, file, isInstant, forceOverwrite, localBehaviour);
55 }
56
57 @Override
58 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
59 int status = -1;
60
61 FileChannel channel = null;
62 RandomAccessFile raf = null;
63 try {
64 File file = new File(getStoragePath());
65 raf = new RandomAccessFile(file, "r");
66 channel = raf.getChannel();
67 mEntity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE, file);
68 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners());
69 long offset = 0;
70 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
71 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
72 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
73 mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
74 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
75 ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset);
76 mPutMethod.setRequestEntity(mEntity);
77 status = client.executeMethod(mPutMethod);
78 client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
79 Log_OC.d(TAG, "Upload of " + getStoragePath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
80 if (!isSuccess(status))
81 break;
82 }
83
84 } finally {
85 if (channel != null)
86 channel.close();
87 if (raf != null)
88 raf.close();
89 if (mPutMethod != null)
90 mPutMethod.releaseConnection(); // let the connection available for other methods
91 }
92 return status;
93 }
94
95 }