DownloadFileOperation.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.operations;
  22. import java.io.File;
  23. import java.util.HashSet;
  24. import java.util.Iterator;
  25. import java.util.Set;
  26. import java.util.concurrent.atomic.AtomicBoolean;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  29. import com.owncloud.android.lib.common.OwnCloudClient;
  30. import com.owncloud.android.lib.common.operations.OperationCancelledException;
  31. import com.owncloud.android.lib.common.operations.RemoteOperation;
  32. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
  35. import com.owncloud.android.utils.FileStorageUtils;
  36. import android.accounts.Account;
  37. import android.webkit.MimeTypeMap;
  38. /**
  39. * Remote mDownloadOperation performing the download of a file to an ownCloud server
  40. */
  41. public class DownloadFileOperation extends RemoteOperation {
  42. private static final String TAG = DownloadFileOperation.class.getSimpleName();
  43. private Account mAccount;
  44. private OCFile mFile;
  45. private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  46. private long mModificationTimestamp = 0;
  47. private String mEtag = "";
  48. private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
  49. private DownloadRemoteFileOperation mDownloadOperation;
  50. public DownloadFileOperation(Account account, OCFile file) {
  51. if (account == null) {
  52. throw new IllegalArgumentException("Illegal null account in DownloadFileOperation " +
  53. "creation");
  54. }
  55. if (file == null) {
  56. throw new IllegalArgumentException("Illegal null file in DownloadFileOperation " +
  57. "creation");
  58. }
  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 getTmpFolder() {
  79. return FileStorageUtils.getTemporalPath(mAccount.name);
  80. }
  81. public String getRemotePath() {
  82. return mFile.getRemotePath();
  83. }
  84. public String getMimeType() {
  85. String mimeType = mFile.getMimetype();
  86. if (mimeType == null || mimeType.length() <= 0) {
  87. try {
  88. mimeType = MimeTypeMap.getSingleton()
  89. .getMimeTypeFromExtension(
  90. mFile.getRemotePath().substring(
  91. mFile.getRemotePath().lastIndexOf('.') + 1));
  92. } catch (IndexOutOfBoundsException e) {
  93. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " +
  94. mFile.getRemotePath());
  95. }
  96. }
  97. if (mimeType == null) {
  98. mimeType = "application/octet-stream";
  99. }
  100. return mimeType;
  101. }
  102. public long getSize() {
  103. return mFile.getFileLength();
  104. }
  105. public long getModificationTimestamp() {
  106. return (mModificationTimestamp > 0) ? mModificationTimestamp :
  107. mFile.getModificationTimestamp();
  108. }
  109. public String getEtag() {
  110. return mEtag;
  111. }
  112. @Override
  113. protected RemoteOperationResult run(OwnCloudClient client) {
  114. RemoteOperationResult result;
  115. File newFile;
  116. boolean moved;
  117. /// download will be performed to a temporal file, then moved to the final location
  118. File tmpFile = new File(getTmpPath());
  119. String tmpFolder = getTmpFolder();
  120. /// perform the download
  121. synchronized(mCancellationRequested) {
  122. if (mCancellationRequested.get()) {
  123. return new RemoteOperationResult(new OperationCancelledException());
  124. }
  125. }
  126. mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
  127. Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
  128. while (listener.hasNext()) {
  129. mDownloadOperation.addDatatransferProgressListener(listener.next());
  130. }
  131. result = mDownloadOperation.execute(client);
  132. if (result.isSuccess()) {
  133. mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
  134. mEtag = mDownloadOperation.getEtag();
  135. newFile = new File(getSavePath());
  136. newFile.getParentFile().mkdirs();
  137. moved = tmpFile.renameTo(newFile);
  138. if (!moved) {
  139. result = new RemoteOperationResult(
  140. RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
  141. }
  142. }
  143. Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " +
  144. result.getLogMessage());
  145. return result;
  146. }
  147. public void cancel() {
  148. mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
  149. if (mDownloadOperation != null) {
  150. mDownloadOperation.cancel();
  151. }
  152. }
  153. public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
  154. synchronized (mDataTransferListeners) {
  155. mDataTransferListeners.add(listener);
  156. }
  157. }
  158. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  159. synchronized (mDataTransferListeners) {
  160. mDataTransferListeners.remove(listener);
  161. }
  162. }
  163. }