1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
20 import java
.io
.BufferedInputStream
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.util
.Date
;
25 import java
.util
.HashSet
;
26 import java
.util
.Iterator
;
28 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
30 import org
.apache
.commons
.httpclient
.Header
;
31 import org
.apache
.commons
.httpclient
.HttpException
;
32 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
33 import org
.apache
.http
.HttpStatus
;
35 import com
.owncloud
.android
.Log_OC
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
37 import com
.owncloud
.android
.operations
.RemoteOperation
;
38 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
39 import com
.owncloud
.android
.utils
.FileStorageUtils
;
42 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
43 import eu
.alefzero
.webdav
.WebdavClient
;
44 import eu
.alefzero
.webdav
.WebdavUtils
;
45 import android
.accounts
.Account
;
46 import android
.webkit
.MimeTypeMap
;
49 * Remote operation performing the download of a file to an ownCloud server
51 * @author David A. Velasco
53 public class DownloadFileOperation
extends RemoteOperation
{
55 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
57 private Account mAccount
;
59 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
60 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
61 private long mModificationTimestamp
= 0;
62 private GetMethod mGet
;
65 public DownloadFileOperation(Account account
, OCFile file
) {
67 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
69 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
76 public Account
getAccount() {
80 public OCFile
getFile() {
84 public String
getSavePath() {
85 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
86 if (path
!= null
&& path
.length() > 0) {
89 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
92 public String
getTmpPath() {
93 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
96 public String
getRemotePath() {
97 return mFile
.getRemotePath();
100 public String
getMimeType() {
101 String mimeType
= mFile
.getMimetype();
102 if (mimeType
== null
|| mimeType
.length() <= 0) {
104 mimeType
= MimeTypeMap
.getSingleton()
105 .getMimeTypeFromExtension(
106 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
107 } catch (IndexOutOfBoundsException e
) {
108 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
111 if (mimeType
== null
) {
112 mimeType
= "application/octet-stream";
117 public long getSize() {
118 return mFile
.getFileLength();
121 public long getModificationTimestamp() {
122 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
126 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
127 synchronized (mDataTransferListeners
) {
128 mDataTransferListeners
.add(listener
);
132 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
133 synchronized (mDataTransferListeners
) {
134 mDataTransferListeners
.remove(listener
);
139 protected RemoteOperationResult
run(WebdavClient client
) {
140 RemoteOperationResult result
= null
;
142 boolean moved
= true
;
144 /// download will be performed to a temporal file, then moved to the final location
145 File tmpFile
= new File(getTmpPath());
147 /// perform the download
149 tmpFile
.getParentFile().mkdirs();
150 int status
= downloadFile(client
, tmpFile
);
151 if (isSuccess(status
)) {
152 newFile
= new File(getSavePath());
153 newFile
.getParentFile().mkdirs();
154 moved
= tmpFile
.renameTo(newFile
);
157 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
159 result
= new RemoteOperationResult(isSuccess(status
), status
, (mGet
!= null ? mGet
.getResponseHeaders() : null
));
160 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
162 } catch (Exception e
) {
163 result
= new RemoteOperationResult(e
);
164 Log_OC
.e(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage(), e
);
171 public boolean isSuccess(int status
) {
172 return (status
== HttpStatus
.SC_OK
);
176 protected int downloadFile(WebdavClient client
, File targetFile
) throws HttpException
, IOException
, OperationCancelledException
{
178 boolean savedFile
= false
;
179 mGet
= new GetMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()));
180 Iterator
<OnDatatransferProgressListener
> it
= null
;
182 FileOutputStream fos
= null
;
184 status
= client
.executeMethod(mGet
);
185 if (isSuccess(status
)) {
186 targetFile
.createNewFile();
187 BufferedInputStream bis
= new BufferedInputStream(mGet
.getResponseBodyAsStream());
188 fos
= new FileOutputStream(targetFile
);
189 long transferred
= 0;
191 byte[] bytes
= new byte[4096];
193 while ((readResult
= bis
.read(bytes
)) != -1) {
194 synchronized(mCancellationRequested
) {
195 if (mCancellationRequested
.get()) {
197 throw new OperationCancelledException();
200 fos
.write(bytes
, 0, readResult
);
201 transferred
+= readResult
;
202 synchronized (mDataTransferListeners
) {
203 it
= mDataTransferListeners
.iterator();
204 while (it
.hasNext()) {
205 it
.next().onTransferProgress(readResult
, transferred
, mFile
.getFileLength(), targetFile
.getName());
210 Header modificationTime
= mGet
.getResponseHeader("Last-Modified");
211 if (modificationTime
!= null
) {
212 Date d
= WebdavUtils
.parseResponseDate((String
) modificationTime
.getValue());
213 mModificationTimestamp
= (d
!= null
) ? d
.getTime() : 0;
217 client
.exhaustResponse(mGet
.getResponseBodyAsStream());
221 if (fos
!= null
) fos
.close();
222 if (!savedFile
&& targetFile
.exists()) {
225 mGet
.releaseConnection(); // let the connection available for other methods
231 public void cancel() {
232 mCancellationRequested
.set(true
); // atomic set; there is no need of synchronizing it