DownloadFileOperation.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 android.accounts.Account;
  23. import android.content.Context;
  24. import android.webkit.MimeTypeMap;
  25. import com.owncloud.android.datamodel.DecryptedFolderMetadata;
  26. import com.owncloud.android.datamodel.FileDataStorageManager;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.lib.common.OwnCloudClient;
  29. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  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.DownloadFileRemoteOperation;
  35. import com.owncloud.android.utils.EncryptionUtils;
  36. import com.owncloud.android.utils.FileStorageUtils;
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.util.HashSet;
  40. import java.util.Iterator;
  41. import java.util.Set;
  42. import java.util.concurrent.atomic.AtomicBoolean;
  43. import lombok.Getter;
  44. /**
  45. * Remote DownloadOperation performing the download of a file to an ownCloud server
  46. */
  47. public class DownloadFileOperation extends RemoteOperation {
  48. private static final String TAG = DownloadFileOperation.class.getSimpleName();
  49. @Getter private Account account;
  50. @Getter private OCFile file;
  51. @Getter private String behaviour;
  52. @Getter private String etag = "";
  53. @Getter private String activityName;
  54. @Getter private String packageName;
  55. private Context context;
  56. private Set<OnDatatransferProgressListener> dataTransferListeners = new HashSet<>();
  57. private long modificationTimestamp;
  58. private DownloadFileRemoteOperation downloadOperation;
  59. private final AtomicBoolean cancellationRequested = new AtomicBoolean(false);
  60. public DownloadFileOperation(Account account, OCFile file, String behaviour, String activityName,
  61. String packageName, Context context) {
  62. if (account == null) {
  63. throw new IllegalArgumentException("Illegal null account in DownloadFileOperation " +
  64. "creation");
  65. }
  66. if (file == null) {
  67. throw new IllegalArgumentException("Illegal null file in DownloadFileOperation " +
  68. "creation");
  69. }
  70. this.account = account;
  71. this.file = file;
  72. this.behaviour = behaviour;
  73. this.activityName = activityName;
  74. this.packageName = packageName;
  75. this.context = context;
  76. }
  77. public String getSavePath() {
  78. if (file.getStoragePath() != null) {
  79. File path = new File(file.getStoragePath()); // re-downloads should be done over the original file
  80. if (path.canWrite()) {
  81. return path.getAbsolutePath();
  82. }
  83. }
  84. return FileStorageUtils.getDefaultSavePathFor(account.name, file);
  85. }
  86. public String getTmpPath() {
  87. return FileStorageUtils.getTemporalPath(account.name) + file.getRemotePath();
  88. }
  89. public String getTmpFolder() {
  90. return FileStorageUtils.getTemporalPath(account.name);
  91. }
  92. public String getRemotePath() {
  93. return file.getRemotePath();
  94. }
  95. public String getMimeType() {
  96. String mimeType = file.getMimeType();
  97. if (mimeType == null || mimeType.length() <= 0) {
  98. try {
  99. mimeType = MimeTypeMap.getSingleton()
  100. .getMimeTypeFromExtension(
  101. file.getRemotePath().substring(
  102. file.getRemotePath().lastIndexOf('.') + 1));
  103. } catch (IndexOutOfBoundsException e) {
  104. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " +
  105. file.getRemotePath());
  106. }
  107. }
  108. if (mimeType == null) {
  109. mimeType = "application/octet-stream";
  110. }
  111. return mimeType;
  112. }
  113. public long getSize() {
  114. return file.getFileLength();
  115. }
  116. public long getModificationTimestamp() {
  117. return modificationTimestamp > 0 ? modificationTimestamp : file.getModificationTimestamp();
  118. }
  119. @Override
  120. protected RemoteOperationResult run(OwnCloudClient client) {
  121. /// perform the download
  122. synchronized(cancellationRequested) {
  123. if (cancellationRequested.get()) {
  124. return new RemoteOperationResult(new OperationCancelledException());
  125. }
  126. }
  127. RemoteOperationResult result;
  128. File newFile;
  129. boolean moved;
  130. /// download will be performed to a temporal file, then moved to the final location
  131. File tmpFile = new File(getTmpPath());
  132. String tmpFolder = getTmpFolder();
  133. downloadOperation = new DownloadFileRemoteOperation(file.getRemotePath(), tmpFolder);
  134. Iterator<OnDatatransferProgressListener> listener = dataTransferListeners.iterator();
  135. while (listener.hasNext()) {
  136. downloadOperation.addDatatransferProgressListener(listener.next());
  137. }
  138. result = downloadOperation.execute(client);
  139. if (result.isSuccess()) {
  140. modificationTimestamp = downloadOperation.getModificationTimestamp();
  141. etag = downloadOperation.getEtag();
  142. newFile = new File(getSavePath());
  143. if (!newFile.getParentFile().mkdirs()) {
  144. Log_OC.e(TAG, "Unable to create parent folder " + newFile.getParentFile().getAbsolutePath());
  145. }
  146. // decrypt file
  147. if (file.isEncrypted() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
  148. FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account, context.getContentResolver());
  149. OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath());
  150. DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, context, account);
  151. if (metadata == null) {
  152. return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
  153. }
  154. byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  155. .get(file.getEncryptedFileName()).getEncrypted().getKey());
  156. byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  157. .get(file.getEncryptedFileName()).getInitializationVector());
  158. byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  159. .get(file.getEncryptedFileName()).getAuthenticationTag());
  160. try {
  161. byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
  162. try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
  163. fileOutputStream.write(decryptedBytes);
  164. }
  165. } catch (Exception e) {
  166. return new RemoteOperationResult(e);
  167. }
  168. }
  169. moved = tmpFile.renameTo(newFile);
  170. newFile.setLastModified(file.getModificationTimestamp());
  171. if (!moved) {
  172. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
  173. }
  174. }
  175. Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " +
  176. result.getLogMessage());
  177. return result;
  178. }
  179. public void cancel() {
  180. cancellationRequested.set(true); // atomic set; there is no need of synchronizing it
  181. if (downloadOperation != null) {
  182. downloadOperation.cancel();
  183. }
  184. }
  185. public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
  186. synchronized (dataTransferListeners) {
  187. dataTransferListeners.add(listener);
  188. }
  189. }
  190. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  191. synchronized (dataTransferListeners) {
  192. dataTransferListeners.remove(listener);
  193. }
  194. }
  195. }