DownloadFileOperation.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.File;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.Set;
  22. import com.owncloud.android.datamodel.OCFile;
  23. import com.owncloud.android.lib.network.OnDatatransferProgressListener;
  24. import com.owncloud.android.lib.network.OwnCloudClient;
  25. import com.owncloud.android.lib.operations.common.RemoteOperation;
  26. import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  27. import com.owncloud.android.lib.operations.remote.DownloadRemoteFileOperation;
  28. import com.owncloud.android.utils.FileStorageUtils;
  29. import com.owncloud.android.utils.Log_OC;
  30. import android.accounts.Account;
  31. import android.webkit.MimeTypeMap;
  32. /**
  33. * Remote mDownloadOperation performing the download of a file to an ownCloud server
  34. *
  35. * @author David A. Velasco
  36. * @author masensio
  37. */
  38. public class DownloadFileOperation extends RemoteOperation {
  39. private static final String TAG = DownloadFileOperation.class.getSimpleName();
  40. private Account mAccount;
  41. private OCFile mFile;
  42. private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
  43. private long mModificationTimestamp = 0;
  44. private DownloadRemoteFileOperation mDownloadOperation;
  45. public DownloadFileOperation(Account account, OCFile file) {
  46. if (account == null)
  47. throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
  48. if (file == null)
  49. throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
  50. mAccount = account;
  51. mFile = file;
  52. }
  53. public Account getAccount() {
  54. return mAccount;
  55. }
  56. public OCFile getFile() {
  57. return mFile;
  58. }
  59. public String getSavePath() {
  60. String path = mFile.getStoragePath(); // re-downloads should be done over the original file
  61. if (path != null && path.length() > 0) {
  62. return path;
  63. }
  64. return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
  65. }
  66. public String getTmpPath() {
  67. return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
  68. }
  69. public String getTmpFolder() {
  70. return FileStorageUtils.getTemporalPath(mAccount.name);
  71. }
  72. public String getRemotePath() {
  73. return mFile.getRemotePath();
  74. }
  75. public String getMimeType() {
  76. String mimeType = mFile.getMimetype();
  77. if (mimeType == null || mimeType.length() <= 0) {
  78. try {
  79. mimeType = MimeTypeMap.getSingleton()
  80. .getMimeTypeFromExtension(
  81. mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1));
  82. } catch (IndexOutOfBoundsException e) {
  83. Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath());
  84. }
  85. }
  86. if (mimeType == null) {
  87. mimeType = "application/octet-stream";
  88. }
  89. return mimeType;
  90. }
  91. public long getSize() {
  92. return mFile.getFileLength();
  93. }
  94. public long getModificationTimestamp() {
  95. return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp();
  96. }
  97. @Override
  98. protected RemoteOperationResult run(OwnCloudClient client) {
  99. RemoteOperationResult result = null;
  100. File newFile = null;
  101. boolean moved = true;
  102. /// download will be performed to a temporal file, then moved to the final location
  103. File tmpFile = new File(getTmpPath());
  104. String tmpFolder = getTmpFolder();
  105. /// perform the download
  106. mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
  107. Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
  108. while (listener.hasNext()) {
  109. mDownloadOperation.addDatatransferProgressListener(listener.next());
  110. }
  111. result = mDownloadOperation.execute(client);
  112. if (result.isSuccess()) {
  113. newFile = new File(getSavePath());
  114. newFile.getParentFile().mkdirs();
  115. moved = tmpFile.renameTo(newFile);
  116. if (!moved)
  117. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
  118. }
  119. Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
  120. return result;
  121. }
  122. public void cancel() {
  123. mDownloadOperation.cancel();
  124. }
  125. public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
  126. synchronized (mDataTransferListeners) {
  127. mDataTransferListeners.add(listener);
  128. }
  129. }
  130. public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
  131. synchronized (mDataTransferListeners) {
  132. mDataTransferListeners.remove(listener);
  133. }
  134. }
  135. }