DownloadFileOperation.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  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 version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.Iterator;
  25. import java.util.Set;
  26. import java.util.concurrent.atomic.AtomicBoolean;
  27. import org.apache.commons.httpclient.Header;
  28. import org.apache.commons.httpclient.HttpException;
  29. import org.apache.commons.httpclient.methods.GetMethod;
  30. import org.apache.http.HttpStatus;
  31. import com.owncloud.android.Log_OC;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.operations.RemoteOperation;
  34. import com.owncloud.android.operations.RemoteOperationResult;
  35. import com.owncloud.android.utils.FileStorageUtils;
  36. import eu.alefzero.webdav.OnDatatransferProgressListener;
  37. import eu.alefzero.webdav.WebdavClient;
  38. import eu.alefzero.webdav.WebdavUtils;
  39. import android.accounts.Account;
  40. import android.webkit.MimeTypeMap;
  41. /**
  42. * Remote operation performing the download of a file to an ownCloud server
  43. *
  44. * @author David A. Velasco
  45. */
  46. public class DownloadFileOperation extends RemoteOperation {
  47. private static final String TAG = DownloadFileOperation.class.getSimpleName();
  48. private Account mAccount;
  49. private OCFile mFile;
  50. private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  51. private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
  52. private long mModificationTimestamp = 0;
  53. private GetMethod mGet;
  54. public DownloadFileOperation(Account account, OCFile file) {
  55. if (account == null)
  56. throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
  57. if (file == null)
  58. throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
  59. mAccount = account;
  60. mFile = file;
  61. }
  62. public Account getAccount() {
  63. return mAccount;
  64. }
  65. public OCFile getFile() {
  66. return mFile;
  67. }
  68. public String getSavePath() {
  69. String path = mFile.getStoragePath(); // re-downloads should be done over the original file
  70. if (path != null && path.length() > 0) {
  71. return path;
  72. }
  73. return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
  74. }
  75. public String getTmpPath() {
  76. return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
  77. }
  78. public String getRemotePath() {
  79. return mFile.getRemotePath();
  80. }
  81. public String getMimeType() {
  82. String mimeType = mFile.getMimetype();
  83. if (mimeType == null || mimeType.length() <= 0) {
  84. try {
  85. mimeType = MimeTypeMap.getSingleton()
  86. .getMimeTypeFromExtension(
  87. mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1));
  88. } catch (IndexOutOfBoundsException e) {
  89. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath());
  90. }
  91. }
  92. if (mimeType == null) {
  93. mimeType = "application/octet-stream";
  94. }
  95. return mimeType;
  96. }
  97. public long getSize() {
  98. return mFile.getFileLength();
  99. }
  100. public long getModificationTimestamp() {
  101. return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp();
  102. }
  103. public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
  104. synchronized (mDataTransferListeners) {
  105. mDataTransferListeners.add(listener);
  106. }
  107. }
  108. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  109. synchronized (mDataTransferListeners) {
  110. mDataTransferListeners.remove(listener);
  111. }
  112. }
  113. @Override
  114. protected RemoteOperationResult run(WebdavClient client) {
  115. RemoteOperationResult result = null;
  116. File newFile = null;
  117. boolean moved = true;
  118. /// download will be performed to a temporal file, then moved to the final location
  119. File tmpFile = new File(getTmpPath());
  120. /// perform the download
  121. try {
  122. tmpFile.getParentFile().mkdirs();
  123. int status = downloadFile(client, tmpFile);
  124. if (isSuccess(status)) {
  125. newFile = new File(getSavePath());
  126. newFile.getParentFile().mkdirs();
  127. moved = tmpFile.renameTo(newFile);
  128. }
  129. if (!moved)
  130. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
  131. else
  132. result = new RemoteOperationResult(isSuccess(status), status, (mGet != null ? mGet.getResponseHeaders() : null));
  133. Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
  134. } catch (Exception e) {
  135. result = new RemoteOperationResult(e);
  136. Log_OC.e(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage(), e);
  137. }
  138. return result;
  139. }
  140. public boolean isSuccess(int status) {
  141. return (status == HttpStatus.SC_OK);
  142. }
  143. protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
  144. int status = -1;
  145. boolean savedFile = false;
  146. mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()));
  147. Iterator<OnDatatransferProgressListener> it = null;
  148. FileOutputStream fos = null;
  149. try {
  150. status = client.executeMethod(mGet);
  151. if (isSuccess(status)) {
  152. targetFile.createNewFile();
  153. BufferedInputStream bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
  154. fos = new FileOutputStream(targetFile);
  155. long transferred = 0;
  156. byte[] bytes = new byte[4096];
  157. int readResult = 0;
  158. while ((readResult = bis.read(bytes)) != -1) {
  159. synchronized(mCancellationRequested) {
  160. if (mCancellationRequested.get()) {
  161. mGet.abort();
  162. throw new OperationCancelledException();
  163. }
  164. }
  165. fos.write(bytes, 0, readResult);
  166. transferred += readResult;
  167. synchronized (mDataTransferListeners) {
  168. it = mDataTransferListeners.iterator();
  169. while (it.hasNext()) {
  170. it.next().onTransferProgress(readResult, transferred, mFile.getFileLength(), targetFile.getName());
  171. }
  172. }
  173. }
  174. savedFile = true;
  175. Header modificationTime = mGet.getResponseHeader("Last-Modified");
  176. if (modificationTime != null) {
  177. Date d = WebdavUtils.parseResponseDate((String) modificationTime.getValue());
  178. mModificationTimestamp = (d != null) ? d.getTime() : 0;
  179. }
  180. } else {
  181. client.exhaustResponse(mGet.getResponseBodyAsStream());
  182. }
  183. } finally {
  184. if (fos != null) fos.close();
  185. if (!savedFile && targetFile.exists()) {
  186. targetFile.delete();
  187. }
  188. mGet.releaseConnection(); // let the connection available for other methods
  189. }
  190. return status;
  191. }
  192. public void cancel() {
  193. mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
  194. }
  195. }