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 as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
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.
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/>.
19 package com
.owncloud
.android
.operations
;
21 import java
.io
.BufferedInputStream
;
23 import java
.io
.FileOutputStream
;
24 import java
.io
.IOException
;
25 import java
.util
.Date
;
26 import java
.util
.HashSet
;
27 import java
.util
.Iterator
;
29 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
31 import org
.apache
.commons
.httpclient
.Header
;
32 import org
.apache
.commons
.httpclient
.HttpException
;
33 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
34 import org
.apache
.http
.HttpStatus
;
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
;
41 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
42 import eu
.alefzero
.webdav
.WebdavClient
;
43 import eu
.alefzero
.webdav
.WebdavUtils
;
44 import android
.accounts
.Account
;
45 import android
.util
.Log
;
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;
64 public DownloadFileOperation(Account account
, OCFile file
) {
66 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
68 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
75 public Account
getAccount() {
79 public OCFile
getFile() {
83 public String
getSavePath() {
84 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
85 if (path
!= null
&& path
.length() > 0) {
88 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
91 public String
getTmpPath() {
92 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
95 public String
getRemotePath() {
96 return mFile
.getRemotePath();
99 public String
getMimeType() {
100 String mimeType
= mFile
.getMimetype();
101 if (mimeType
== null
|| mimeType
.length() <= 0) {
103 mimeType
= MimeTypeMap
.getSingleton()
104 .getMimeTypeFromExtension(
105 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
106 } catch (IndexOutOfBoundsException e
) {
107 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
110 if (mimeType
== null
) {
111 mimeType
= "application/octet-stream";
116 public long getSize() {
117 return mFile
.getFileLength();
120 public long getModificationTimestamp() {
121 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
125 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
126 mDataTransferListeners
.add(listener
);
130 protected RemoteOperationResult
run(WebdavClient client
) {
131 RemoteOperationResult result
= null
;
133 boolean moved
= true
;
135 /// download will be performed to a temporal file, then moved to the final location
136 File tmpFile
= new File(getTmpPath());
138 /// perform the download
140 tmpFile
.getParentFile().mkdirs();
141 int status
= downloadFile(client
, tmpFile
);
142 if (isSuccess(status
)) {
143 newFile
= new File(getSavePath());
144 newFile
.getParentFile().mkdirs();
145 moved
= tmpFile
.renameTo(newFile
);
148 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
150 result
= new RemoteOperationResult(isSuccess(status
), status
);
151 Log
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
153 } catch (Exception e
) {
154 result
= new RemoteOperationResult(e
);
155 Log
.e(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage(), e
);
162 public boolean isSuccess(int status
) {
163 return (status
== HttpStatus
.SC_OK
);
167 protected int downloadFile(WebdavClient client
, File targetFile
) throws HttpException
, IOException
, OperationCancelledException
{
169 boolean savedFile
= false
;
170 GetMethod get
= new GetMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()));
171 Iterator
<OnDatatransferProgressListener
> it
= null
;
173 FileOutputStream fos
= null
;
175 status
= client
.executeMethod(get
);
176 if (isSuccess(status
)) {
177 targetFile
.createNewFile();
178 BufferedInputStream bis
= new BufferedInputStream(get
.getResponseBodyAsStream());
179 fos
= new FileOutputStream(targetFile
);
180 long transferred
= 0;
182 byte[] bytes
= new byte[4096];
184 while ((readResult
= bis
.read(bytes
)) != -1) {
185 synchronized(mCancellationRequested
) {
186 if (mCancellationRequested
.get()) {
188 throw new OperationCancelledException();
191 fos
.write(bytes
, 0, readResult
);
192 transferred
+= readResult
;
193 it
= mDataTransferListeners
.iterator();
194 while (it
.hasNext()) {
195 it
.next().onTransferProgress(readResult
, transferred
, mFile
.getFileLength(), targetFile
.getName());
199 Header modificationTime
= get
.getResponseHeader("Last-Modified");
200 if (modificationTime
!= null
) {
201 Date d
= WebdavUtils
.parseResponseDate((String
) modificationTime
.getValue());
202 mModificationTimestamp
= (d
!= null
) ? d
.getTime() : 0;
206 client
.exhaustResponse(get
.getResponseBodyAsStream());
210 if (fos
!= null
) fos
.close();
211 if (!savedFile
&& targetFile
.exists()) {
214 get
.releaseConnection(); // let the connection available for other methods
220 public void cancel() {
221 mCancellationRequested
.set(true
); // atomic set; there is no need of synchronizing it