Fixed lack of File#getUsableSpace() in Android versions previous to GINGERBREAD
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / ChunkedUploadFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
32 import android.accounts.Account;
33 import android.util.Log;
34
35 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
36 import eu.alefzero.webdav.WebdavClient;
37 import eu.alefzero.webdav.WebdavUtils;
38
39 public class ChunkedUploadFileOperation extends UploadFileOperation {
40
41 private static final long CHUNK_SIZE = 1024000;
42 private static final String OC_CHUNKED_HEADER = "OC-Chunked";
43 private static final String TAG = ChunkedUploadFileOperation.class.getSimpleName();
44
45 public ChunkedUploadFileOperation( Account account,
46 OCFile file,
47 boolean isInstant,
48 boolean forceOverwrite,
49 int localBehaviour) {
50
51 super(account, file, isInstant, forceOverwrite, localBehaviour);
52 }
53
54 @Override
55 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
56 int status = -1;
57
58 FileChannel channel = null;
59 RandomAccessFile raf = null;
60 try {
61 File file = new File(getStoragePath());
62 raf = new RandomAccessFile(file, "r");
63 channel = raf.getChannel();
64 ChunkFromFileChannelRequestEntity entity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE, file);
65 entity.addOnDatatransferProgressListeners(getDataTransferListeners());
66 long offset = 0;
67 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
68 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
69 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
70 mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
71 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
72 entity.setOffset(offset);
73 mPutMethod.setRequestEntity(entity);
74 status = client.executeMethod(mPutMethod);
75 client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
76 Log.d(TAG, "Upload of " + getStoragePath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
77 if (!isSuccess(status))
78 break;
79 }
80
81 } finally {
82 if (channel != null)
83 channel.close();
84 if (raf != null)
85 raf.close();
86 if (mPutMethod != null)
87 mPutMethod.releaseConnection(); // let the connection available for other methods
88 }
89 return status;
90 }
91
92 }