1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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
.HashSet
;
26 import java
.util
.Iterator
;
28 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
30 import org
.apache
.commons
.httpclient
.HttpException
;
31 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
32 import org
.apache
.http
.HttpStatus
;
34 import com
.owncloud
.android
.datamodel
.OCFile
;
35 import com
.owncloud
.android
.operations
.RemoteOperation
;
36 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
37 import com
.owncloud
.android
.utils
.FileStorageUtils
;
39 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
40 import eu
.alefzero
.webdav
.WebdavClient
;
41 import eu
.alefzero
.webdav
.WebdavUtils
;
42 import android
.accounts
.Account
;
43 import android
.util
.Log
;
44 import android
.webkit
.MimeTypeMap
;
47 * Remote operation performing the download of a file to an ownCloud server
49 * @author David A. Velasco
51 public class DownloadFileOperation
extends RemoteOperation
{
53 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
55 private Account mAccount
;
57 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
58 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
61 public DownloadFileOperation(Account account
, OCFile file
) {
63 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
65 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
72 public Account
getAccount() {
76 public OCFile
getFile() {
80 public String
getSavePath() {
81 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
82 if (path
!= null
&& path
.length() > 0) {
85 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
88 public String
getTmpPath() {
89 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
92 public String
getRemotePath() {
93 return mFile
.getRemotePath();
96 public String
getMimeType() {
97 String mimeType
= mFile
.getMimetype(); // TODO fix the mime types in OCFiles FOREVER
98 if (mimeType
== null
|| mimeType
.length() <= 0) {
100 mimeType
= MimeTypeMap
.getSingleton()
101 .getMimeTypeFromExtension(
102 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
103 } catch (IndexOutOfBoundsException e
) {
104 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
107 if (mimeType
== null
) {
108 mimeType
= "application/octet-stream";
113 public long getSize() {
114 return mFile
.getFileLength();
118 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
119 mDataTransferListeners
.add(listener
);
123 protected RemoteOperationResult
run(WebdavClient client
) {
124 RemoteOperationResult result
= null
;
126 boolean moved
= true
;
128 /// download will be performed to a temporal file, then moved to the final location
129 File tmpFile
= new File(getTmpPath());
131 /// perform the download
133 tmpFile
.getParentFile().mkdirs();
134 int status
= downloadFile(client
, tmpFile
);
135 if (isSuccess(status
)) {
136 newFile
= new File(getSavePath());
137 newFile
.getParentFile().mkdirs();
138 moved
= tmpFile
.renameTo(newFile
);
141 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
);
143 result
= new RemoteOperationResult(isSuccess(status
), status
);
144 Log
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
146 } catch (Exception e
) {
147 result
= new RemoteOperationResult(e
);
148 Log
.e(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage(), e
);
155 public boolean isSuccess(int status
) {
156 return (status
== HttpStatus
.SC_OK
);
160 protected int downloadFile(WebdavClient client
, File targetFile
) throws HttpException
, IOException
, OperationCancelledException
{
162 boolean savedFile
= false
;
163 GetMethod get
= new GetMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()));
164 Iterator
<OnDatatransferProgressListener
> it
= null
;
166 FileOutputStream fos
= null
;
168 status
= client
.executeMethod(get
);
169 if (isSuccess(status
)) {
170 targetFile
.createNewFile();
171 BufferedInputStream bis
= new BufferedInputStream(get
.getResponseBodyAsStream());
172 fos
= new FileOutputStream(targetFile
);
173 long transferred
= 0;
175 byte[] bytes
= new byte[4096];
177 while ((readResult
= bis
.read(bytes
)) != -1) {
178 synchronized(mCancellationRequested
) {
179 if (mCancellationRequested
.get()) {
181 throw new OperationCancelledException();
184 fos
.write(bytes
, 0, readResult
);
185 transferred
+= readResult
;
186 it
= mDataTransferListeners
.iterator();
187 while (it
.hasNext()) {
188 it
.next().onTransferProgress(readResult
, transferred
, mFile
.getFileLength(), targetFile
.getName());
194 client
.exhaustResponse(get
.getResponseBodyAsStream());
198 if (fos
!= null
) fos
.close();
199 if (!savedFile
&& targetFile
.exists()) {
202 get
.releaseConnection(); // let the connection available for other methods
208 public void cancel() {
209 mCancellationRequested
.set(true
); // atomic set; there is no need of synchronizing it