Fixed local path NULL when making favourite a file not down ; fixed change of local...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / DownloadFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import java.io.BufferedInputStream;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Set;
28 import java.util.concurrent.atomic.AtomicBoolean;
29
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.apache.http.HttpStatus;
33
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;
38
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;
45
46 /**
47 * Remote operation performing the download of a file to an ownCloud server
48 *
49 * @author David A. Velasco
50 */
51 public class DownloadFileOperation extends RemoteOperation {
52
53 private static final String TAG = DownloadFileOperation.class.getSimpleName();
54
55 private Account mAccount;
56 private OCFile mFile;
57 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
58 private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
59
60
61 public DownloadFileOperation(Account account, OCFile file) {
62 if (account == null)
63 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
64 if (file == null)
65 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
66
67 mAccount = account;
68 mFile = file;
69 }
70
71
72 public Account getAccount() {
73 return mAccount;
74 }
75
76 public OCFile getFile() {
77 return mFile;
78 }
79
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) {
83 return path;
84 }
85 return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
86 }
87
88 public String getTmpPath() {
89 return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
90 }
91
92 public String getRemotePath() {
93 return mFile.getRemotePath();
94 }
95
96 public String getMimeType() {
97 String mimeType = mFile.getMimetype(); // TODO fix the mime types in OCFiles FOREVER
98 if (mimeType == null || mimeType.length() <= 0) {
99 try {
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());
105 }
106 }
107 if (mimeType == null) {
108 mimeType = "application/octet-stream";
109 }
110 return mimeType;
111 }
112
113 public long getSize() {
114 return mFile.getFileLength();
115 }
116
117
118 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
119 mDataTransferListeners.add(listener);
120 }
121
122 @Override
123 protected RemoteOperationResult run(WebdavClient client) {
124 RemoteOperationResult result = null;
125 File newFile = null;
126 boolean moved = true;
127
128 /// download will be performed to a temporal file, then moved to the final location
129 File tmpFile = new File(getTmpPath());
130
131 /// perform the download
132 try {
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);
139 }
140 if (!moved)
141 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.STORAGE_ERROR_MOVING_FROM_TMP);
142 else
143 result = new RemoteOperationResult(isSuccess(status), status);
144 Log.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
145
146 } catch (Exception e) {
147 result = new RemoteOperationResult(e);
148 Log.e(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage(), e);
149 }
150
151 return result;
152 }
153
154
155 public boolean isSuccess(int status) {
156 return (status == HttpStatus.SC_OK);
157 }
158
159
160 protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
161 int status = -1;
162 boolean savedFile = false;
163 GetMethod get = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()));
164 Iterator<OnDatatransferProgressListener> it = null;
165
166 FileOutputStream fos = null;
167 try {
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;
174
175 byte[] bytes = new byte[4096];
176 int readResult = 0;
177 while ((readResult = bis.read(bytes)) != -1) {
178 synchronized(mCancellationRequested) {
179 if (mCancellationRequested.get()) {
180 get.abort();
181 throw new OperationCancelledException();
182 }
183 }
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());
189 }
190 }
191 savedFile = true;
192
193 } else {
194 client.exhaustResponse(get.getResponseBodyAsStream());
195 }
196
197 } finally {
198 if (fos != null) fos.close();
199 if (!savedFile && targetFile.exists()) {
200 targetFile.delete();
201 }
202 get.releaseConnection(); // let the connection available for other methods
203 }
204 return status;
205 }
206
207
208 public void cancel() {
209 mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
210 }
211
212 }