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