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
.Log_OC
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.operations
.RemoteOperation
;
39 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
40 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
.util
.Log
;
47 import android
.webkit
.MimeTypeMap
;
50 * Remote operation performing the download of a file to an ownCloud server
52 * @author David A. Velasco
54 public class DownloadFileOperation
extends RemoteOperation
{
56 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
58 private Account mAccount
;
60 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
61 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
62 private long mModificationTimestamp
= 0;
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
);
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 GetMethod get
= new GetMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()));
180 Iterator
<OnDatatransferProgressListener
> it
= null
;
182 FileOutputStream fos
= null
;
184 status
= client
.executeMethod(get
);
185 if (isSuccess(status
)) {
186 targetFile
.createNewFile();
187 BufferedInputStream bis
= new BufferedInputStream(get
.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
= get
.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(get
.getResponseBodyAsStream());
221 if (fos
!= null
) fos
.close();
222 if (!savedFile
&& targetFile
.exists()) {
225 get
.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