Fixed crash when the device is turned while the warning dialog about server certifica...
[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.nio.channels.FileLock;
26 import java.util.Random;
27
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.methods.PutMethod;
30
31 import eu.alefzero.webdav.ChunkFromFileChannelRequestEntity;
32 import eu.alefzero.webdav.OnDatatransferProgressListener;
33 import eu.alefzero.webdav.WebdavClient;
34 import eu.alefzero.webdav.WebdavUtils;
35
36 public class ChunkedUploadFileOperation extends UploadFileOperation {
37
38 private static final long CHUNK_SIZE = 102400;
39 private static final String OC_CHUNKED_HEADER = "OC-Chunked";
40
41 public ChunkedUploadFileOperation( String localPath,
42 String remotePath,
43 String mimeType,
44 boolean isInstant,
45 boolean forceOverwrite,
46 OnDatatransferProgressListener dataTransferProgressListener) {
47
48 super(localPath, remotePath, mimeType, isInstant, forceOverwrite, dataTransferProgressListener);
49 }
50
51 @Override
52 protected int uploadFile(WebdavClient client) throws HttpException, IOException {
53 int status = -1;
54
55 PutMethod put = null;
56 FileChannel channel = null;
57 FileLock lock = null;
58 try {
59 File file = new File(getLocalPath());
60 channel = new RandomAccessFile(file, "rw").getChannel();
61 lock = channel.tryLock();
62 ChunkFromFileChannelRequestEntity entity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE);
63 entity.setOnDatatransferProgressListener(getDataTransferListener());
64 long offset = 0;
65 String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt()) + "-" ;
66 long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
67 for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
68 put = new PutMethod(uriPrefix + chunkIndex + "-" + chunkCount);
69 put.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
70 entity.setOffset(offset);
71 put.setRequestEntity(entity);
72 status = client.executeMethod(put);
73 client.exhaustResponse(put.getResponseBodyAsStream());
74 if (!isSuccess(status))
75 break;
76 }
77
78 } finally {
79 if (lock != null)
80 lock.release();
81 if (channel != null)
82 channel.close();
83 if (put != null)
84 put.releaseConnection(); // let the connection available for other methods
85 }
86 return status;
87 }
88
89 }