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
;
29 import org
.apache
.commons
.httpclient
.HttpException
;
30 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
31 import org
.apache
.http
.HttpStatus
;
33 import com
.owncloud
.android
.files
.services
.FileDownloader
;
34 import com
.owncloud
.android
.operations
.RemoteOperation
;
35 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
37 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
38 import eu
.alefzero
.webdav
.WebdavClient
;
39 import eu
.alefzero
.webdav
.WebdavUtils
;
40 import android
.accounts
.Account
;
41 import android
.util
.Log
;
42 import android
.webkit
.MimeTypeMap
;
45 * Remote operation performing the download of a file to an ownCloud server
47 * @author David A. Velasco
49 public class DownloadFileOperation
extends RemoteOperation
{
51 private static final String TAG
= DownloadFileOperation
.class.getCanonicalName();
53 private Account mAccount
= null
;
54 private String mLocalPath
= null
;
55 private String mRemotePath
= null
;
56 private String mMimeType
= null
;
57 private long mSize
= -1;
58 private Boolean mCancellationRequested
= false
;
60 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
63 public Account
getAccount() {
67 public String
getLocalPath() {
71 public String
getRemotePath() {
75 public String
getMimeType() {
79 public long getSize() {
84 public DownloadFileOperation( Account account
,
89 boolean forceOverwrite
) {
92 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
93 if (localPath
== null
)
94 throw new IllegalArgumentException("Illegal null local path in DownloadFileOperation creation");
95 if (remotePath
== null
)
96 throw new IllegalArgumentException("Illegal null remote path in DownloadFileOperation creation");
99 mLocalPath
= localPath
;
100 mRemotePath
= remotePath
;
101 mMimeType
= mimeType
;
102 if (mMimeType
== null
) {
104 mMimeType
= MimeTypeMap
.getSingleton()
105 .getMimeTypeFromExtension(
106 localPath
.substring(localPath
.lastIndexOf('.') + 1));
107 } catch (IndexOutOfBoundsException e
) {
108 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + localPath
);
111 if (mMimeType
== null
) {
112 mMimeType
= "application/octet-stream";
117 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
118 mDataTransferListeners
.add(listener
);
124 protected RemoteOperationResult
run(WebdavClient client
) {
125 RemoteOperationResult result
= null
;
127 boolean moved
= false
;
129 /// download will be in a temporal file
130 File tmpFile
= new File(FileDownloader
.getTemporalPath(mAccount
.name
) + mLocalPath
);
132 /// perform the download
134 tmpFile
.getParentFile().mkdirs();
135 int status
= downloadFile(client
, tmpFile
);
136 if (isSuccess(status
)) {
137 newFile
= new File(FileDownloader
.getSavePath(mAccount
.name
) + mLocalPath
);
138 newFile
.getParentFile().mkdirs();
139 moved
= tmpFile
.renameTo(newFile
);
142 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
);
144 result
= new RemoteOperationResult(isSuccess(status
), status
);
145 Log
.i(TAG
, "Download of " + mLocalPath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage());
147 } catch (Exception e
) {
148 result
= new RemoteOperationResult(e
);
149 Log
.e(TAG
, "Download of " + mRemotePath
+ " to " + mLocalPath
+ ": " + result
.getLogMessage(), e
);
156 public boolean isSuccess(int status
) {
157 return (status
== HttpStatus
.SC_OK
);
161 protected int downloadFile(WebdavClient client
, File targetFile
) throws HttpException
, IOException
, OperationCancelledException
{
163 boolean savedFile
= false
;
164 GetMethod get
= new GetMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
165 Iterator
<OnDatatransferProgressListener
> it
= null
;
168 status
= client
.executeMethod(get
);
169 if (isSuccess(status
)) {
170 targetFile
.createNewFile();
171 BufferedInputStream bis
= new BufferedInputStream(get
.getResponseBodyAsStream());
172 FileOutputStream 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
) {
180 throw new OperationCancelledException();
183 fos
.write(bytes
, 0, readResult
);
184 transferred
+= readResult
;
185 it
= mDataTransferListeners
.iterator();
186 while (it
.hasNext()) {
187 it
.next().onTransferProgress(readResult
, transferred
, mSize
, targetFile
.getName());
194 client
.exhaustResponse(get
.getResponseBodyAsStream());
198 if (!savedFile
&& targetFile
.exists()) {
201 get
.releaseConnection(); // let the connection available for other methods
207 public void cancel() {
208 synchronized(mCancellationRequested
) {
209 mCancellationRequested
= true
;