DownloadFileOperation.java 14 KB

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