1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
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
;
32 import android
.accounts
.Account
;
33 import android
.webkit
.MimeTypeMap
;
36 * Remote mDownloadOperation performing the download of a file to an ownCloud server
38 * @author David A. Velasco
41 public class DownloadFileOperation
extends RemoteOperation
{
43 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
45 private Account mAccount
;
47 private OnDatatransferProgressListener mDatatransferProgressListener
;
48 private long mModificationTimestamp
= 0;
50 private DownloadRemoteFileOperation mDownloadOperation
;
53 public DownloadFileOperation(Account account
, OCFile file
, OnDatatransferProgressListener listener
) {
55 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
57 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
62 mDatatransferProgressListener
= listener
;
66 public Account
getAccount() {
70 public OCFile
getFile() {
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) {
79 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
82 public String
getTmpPath() {
83 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
86 public String
getTmpFolder() {
87 return FileStorageUtils
.getTemporalPath(mAccount
.name
);
90 public String
getRemotePath() {
91 return mFile
.getRemotePath();
94 public String
getMimeType() {
95 String mimeType
= mFile
.getMimetype();
96 if (mimeType
== null
|| mimeType
.length() <= 0) {
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());
105 if (mimeType
== null
) {
106 mimeType
= "application/octet-stream";
111 public long getSize() {
112 return mFile
.getFileLength();
115 public long getModificationTimestamp() {
116 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
120 protected RemoteOperationResult
run(WebdavClient client
) {
121 RemoteOperationResult result
= null
;
123 boolean moved
= true
;
125 /// download will be performed to a temporal file, then moved to the final location
126 File tmpFile
= new File(getTmpPath());
128 String tmpFolder
= getTmpFolder();
129 RemoteFile remoteFile
= FileStorageUtils
.fillRemoteFile(mFile
);
131 /// perform the download
132 mDownloadOperation
= new DownloadRemoteFileOperation(remoteFile
, tmpFolder
);
133 mDownloadOperation
.addDatatransferProgressListener(mDatatransferProgressListener
);
134 result
= mDownloadOperation
.execute(client
);
136 if (result
.isSuccess()) {
137 newFile
= new File(getSavePath());
138 newFile
.getParentFile().mkdirs();
139 moved
= tmpFile
.renameTo(newFile
);
142 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
144 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
150 public void cancel() {
151 mDownloadOperation
.cancel();