DownloadFileOperation.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.content.Context;
  23. import android.text.TextUtils;
  24. import android.util.Log;
  25. import android.webkit.MimeTypeMap;
  26. import com.nextcloud.client.account.User;
  27. import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
  28. import com.owncloud.android.datamodel.DecryptedFolderMetadata;
  29. import com.owncloud.android.datamodel.FileDataStorageManager;
  30. import com.owncloud.android.datamodel.OCFile;
  31. import com.owncloud.android.lib.common.OwnCloudClient;
  32. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  33. import com.owncloud.android.lib.common.operations.OperationCancelledException;
  34. import com.owncloud.android.lib.common.operations.RemoteOperation;
  35. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation;
  38. import com.owncloud.android.utils.EncryptionUtils;
  39. import com.owncloud.android.utils.FileExportUtils;
  40. import com.owncloud.android.utils.FileStorageUtils;
  41. import java.io.File;
  42. import java.io.FileOutputStream;
  43. import java.lang.ref.WeakReference;
  44. import java.util.HashSet;
  45. import java.util.Iterator;
  46. import java.util.Set;
  47. import java.util.concurrent.atomic.AtomicBoolean;
  48. /**
  49. * Remote DownloadOperation performing the download of a file to an ownCloud server
  50. */
  51. public class DownloadFileOperation extends RemoteOperation {
  52. private static final String TAG = DownloadFileOperation.class.getSimpleName();
  53. private User user;
  54. private OCFile file;
  55. private String behaviour;
  56. private String etag = "";
  57. private String activityName;
  58. private String packageName;
  59. private DownloadType downloadType;
  60. private final WeakReference<Context> context;
  61. private Set<OnDatatransferProgressListener> dataTransferListeners = new HashSet<>();
  62. private long modificationTimestamp;
  63. private DownloadFileRemoteOperation downloadOperation;
  64. private final AtomicBoolean cancellationRequested = new AtomicBoolean(false);
  65. public DownloadFileOperation(User user,
  66. OCFile file,
  67. String behaviour,
  68. String activityName,
  69. String packageName,
  70. Context context,
  71. DownloadType downloadType) {
  72. if (user == null) {
  73. throw new IllegalArgumentException("Illegal null user in DownloadFileOperation " +
  74. "creation");
  75. }
  76. if (file == null) {
  77. throw new IllegalArgumentException("Illegal null file in DownloadFileOperation " +
  78. "creation");
  79. }
  80. this.user = user;
  81. this.file = file;
  82. this.behaviour = behaviour;
  83. this.activityName = activityName;
  84. this.packageName = packageName;
  85. this.context = new WeakReference<>(context);
  86. this.downloadType = downloadType;
  87. }
  88. public DownloadFileOperation(User user, OCFile file, Context context) {
  89. this(user, file, null, null, null, context, DownloadType.DOWNLOAD);
  90. }
  91. public boolean isMatching(String accountName, long fileId) {
  92. return getFile().getFileId() == fileId && getUser().getAccountName().equals(accountName);
  93. }
  94. public void cancelMatchingOperation(String accountName, long fileId) {
  95. if (isMatching(accountName, fileId)) {
  96. cancel();
  97. }
  98. }
  99. public String getSavePath() {
  100. if (file.getStoragePath() != null) {
  101. File parentFile = new File(file.getStoragePath()).getParentFile();
  102. if (parentFile != null && !parentFile.exists()) {
  103. parentFile.mkdirs();
  104. }
  105. File path = new File(file.getStoragePath()); // re-downloads should be done over the original file
  106. if (path.canWrite() || parentFile != null && parentFile.canWrite()) {
  107. return path.getAbsolutePath();
  108. }
  109. }
  110. return FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), file);
  111. }
  112. public String getTmpPath() {
  113. return FileStorageUtils.getTemporalPath(user.getAccountName()) + file.getRemotePath();
  114. }
  115. public String getTmpFolder() {
  116. return FileStorageUtils.getTemporalPath(user.getAccountName());
  117. }
  118. public String getRemotePath() {
  119. return file.getRemotePath();
  120. }
  121. public String getMimeType() {
  122. String mimeType = file.getMimeType();
  123. if (TextUtils.isEmpty(mimeType)) {
  124. try {
  125. mimeType = MimeTypeMap.getSingleton()
  126. .getMimeTypeFromExtension(
  127. file.getRemotePath().substring(
  128. file.getRemotePath().lastIndexOf('.') + 1));
  129. } catch (IndexOutOfBoundsException e) {
  130. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " +
  131. file.getRemotePath());
  132. }
  133. }
  134. if (mimeType == null) {
  135. mimeType = "application/octet-stream";
  136. }
  137. return mimeType;
  138. }
  139. public long getSize() {
  140. return file.getFileLength();
  141. }
  142. public long getModificationTimestamp() {
  143. return modificationTimestamp > 0 ? modificationTimestamp : file.getModificationTimestamp();
  144. }
  145. @Override
  146. protected RemoteOperationResult run(OwnCloudClient client) {
  147. /// perform the download
  148. synchronized(cancellationRequested) {
  149. if (cancellationRequested.get()) {
  150. return new RemoteOperationResult(new OperationCancelledException());
  151. }
  152. }
  153. Context operationContext = context.get();
  154. if (operationContext == null) {
  155. return new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
  156. }
  157. RemoteOperationResult result;
  158. File newFile = null;
  159. boolean moved;
  160. /// download will be performed to a temporal file, then moved to the final location
  161. File tmpFile = new File(getTmpPath());
  162. String tmpFolder = getTmpFolder();
  163. downloadOperation = new DownloadFileRemoteOperation(file.getRemotePath(), tmpFolder);
  164. if (downloadType == DownloadType.DOWNLOAD) {
  165. Iterator<OnDatatransferProgressListener> listener = dataTransferListeners.iterator();
  166. while (listener.hasNext()) {
  167. downloadOperation.addDatatransferProgressListener(listener.next());
  168. }
  169. }
  170. result = downloadOperation.execute(client);
  171. if (result.isSuccess()) {
  172. modificationTimestamp = downloadOperation.getModificationTimestamp();
  173. etag = downloadOperation.getEtag();
  174. if (downloadType == DownloadType.DOWNLOAD) {
  175. newFile = new File(getSavePath());
  176. if (!newFile.getParentFile().exists() && !newFile.getParentFile().mkdirs()) {
  177. Log_OC.e(TAG, "Unable to create parent folder " + newFile.getParentFile().getAbsolutePath());
  178. }
  179. }
  180. // decrypt file
  181. if (file.isEncrypted()) {
  182. FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, operationContext.getContentResolver());
  183. OCFile parent = fileDataStorageManager.getFileByEncryptedRemotePath(file.getParentRemotePath());
  184. DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent,
  185. client,
  186. operationContext,
  187. user);
  188. if (metadata == null) {
  189. return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
  190. }
  191. byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  192. .get(file.getEncryptedFileName()).getEncrypted().getKey());
  193. byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  194. .get(file.getEncryptedFileName()).getInitializationVector());
  195. byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
  196. .get(file.getEncryptedFileName()).getAuthenticationTag());
  197. try {
  198. byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile,
  199. key,
  200. iv,
  201. authenticationTag,
  202. new ArbitraryDataProviderImpl(operationContext),
  203. user);
  204. try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
  205. fileOutputStream.write(decryptedBytes);
  206. }
  207. } catch (Exception e) {
  208. return new RemoteOperationResult(e);
  209. }
  210. }
  211. if (downloadType == DownloadType.DOWNLOAD) {
  212. moved = tmpFile.renameTo(newFile);
  213. newFile.setLastModified(file.getModificationTimestamp());
  214. if (!moved) {
  215. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
  216. }
  217. } else if (downloadType == DownloadType.EXPORT) {
  218. new FileExportUtils().exportFile(file.getFileName(),
  219. file.getMimeType(),
  220. operationContext.getContentResolver(),
  221. null,
  222. tmpFile);
  223. if (!tmpFile.delete()) {
  224. Log_OC.e(TAG, "Deletion of " + tmpFile.getAbsolutePath() + " failed!");
  225. }
  226. }
  227. }
  228. Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " +
  229. result.getLogMessage());
  230. return result;
  231. }
  232. public void cancel() {
  233. cancellationRequested.set(true); // atomic set; there is no need of synchronizing it
  234. if (downloadOperation != null) {
  235. downloadOperation.cancel();
  236. }
  237. }
  238. public void addDownloadDataTransferProgressListener(OnDatatransferProgressListener listener) {
  239. synchronized (dataTransferListeners) {
  240. dataTransferListeners.add(listener);
  241. }
  242. }
  243. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  244. synchronized (dataTransferListeners) {
  245. dataTransferListeners.remove(listener);
  246. }
  247. }
  248. public User getUser() {
  249. return this.user;
  250. }
  251. public OCFile getFile() {
  252. return this.file;
  253. }
  254. public String getBehaviour() {
  255. return this.behaviour;
  256. }
  257. public String getEtag() {
  258. return this.etag;
  259. }
  260. public String getActivityName() {
  261. return this.activityName;
  262. }
  263. public String getPackageName() {
  264. return this.packageName;
  265. }
  266. public DownloadType getDownloadType() {
  267. return downloadType;
  268. }
  269. }