import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpStatus;
+import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileDownloader;
import com.owncloud.android.operations.RemoteOperation;
import com.owncloud.android.operations.RemoteOperationResult;
private static final String TAG = DownloadFileOperation.class.getCanonicalName();
private Account mAccount = null;
- private String mLocalPath = null;
- private String mRemotePath = null;
- private String mMimeType = null;
- private long mSize = -1;
- private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
-
+ private OCFile mFile;
private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
+ private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
- public Account getAccount() {
- return mAccount;
+ public DownloadFileOperation(Account account, OCFile file) {
+ if (account == null)
+ throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
+ if (file == null)
+ throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
+
+ mAccount = account;
+ mFile = file;
}
- public String getLocalPath() {
- return mLocalPath;
+
+ public Account getAccount() {
+ return mAccount;
}
- public String getRemotePath() {
- return mRemotePath;
+ public OCFile getFile() {
+ return mFile;
}
- public String getMimeType() {
- return mMimeType;
+ public String getSavePath() {
+ return FileDownloader.getSavePath(mAccount.name) + mFile.getRemotePath();
}
- public long getSize() {
- return mSize;
+ public String getTmpPath() {
+ return FileDownloader.getTemporalPath(mAccount.name) + mFile.getRemotePath();
}
-
- public DownloadFileOperation( Account account,
- String localPath,
- String remotePath,
- String mimeType,
- long size,
- boolean forceOverwrite) {
-
- if (account == null)
- throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
- if (localPath == null)
- throw new IllegalArgumentException("Illegal null local path in DownloadFileOperation creation");
- if (remotePath == null)
- throw new IllegalArgumentException("Illegal null remote path in DownloadFileOperation creation");
-
- mAccount = account;
- mLocalPath = localPath;
- mRemotePath = remotePath;
- mMimeType = mimeType;
- if (mMimeType == null) {
+ public String getRemotePath() {
+ return mFile.getRemotePath();
+ }
+
+ public String getMimeType() {
+ String mimeType = mFile.getMimetype();
+ if (mimeType == null || mimeType.length() <= 0) {
try {
- mMimeType = MimeTypeMap.getSingleton()
+ mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(
- localPath.substring(localPath.lastIndexOf('.') + 1));
+ mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1));
} catch (IndexOutOfBoundsException e) {
- Log.e(TAG, "Trying to find out MIME type of a file without extension: " + localPath);
+ Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath());
}
}
- if (mMimeType == null) {
- mMimeType = "application/octet-stream";
+ if (mimeType == null) {
+ mimeType = "application/octet-stream";
}
- mSize = size;
+ return mimeType;
}
- public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
- mDataTransferListeners.add(listener);
+ public long getSize() {
+ return mFile.getFileLength();
}
+ public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
+ mDataTransferListeners.add(listener);
+ }
@Override
protected RemoteOperationResult run(WebdavClient client) {
File newFile = null;
boolean moved = false;
- /// download will be in a temporal file
- File tmpFile = new File(FileDownloader.getTemporalPath(mAccount.name) + mLocalPath);
+ /// download will be performed to a temporal file, then moved to the final location
+ File tmpFile = new File(getTmpPath());
/// perform the download
try {
tmpFile.getParentFile().mkdirs();
int status = downloadFile(client, tmpFile);
if (isSuccess(status)) {
- newFile = new File(FileDownloader.getSavePath(mAccount.name) + mLocalPath);
+ newFile = new File(getSavePath());
newFile.getParentFile().mkdirs();
moved = tmpFile.renameTo(newFile);
}
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.STORAGE_ERROR_MOVING_FROM_TMP);
else
result = new RemoteOperationResult(isSuccess(status), status);
- Log.i(TAG, "Download of " + mLocalPath + " to " + mRemotePath + ": " + result.getLogMessage());
+ Log.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
} catch (Exception e) {
result = new RemoteOperationResult(e);
- Log.e(TAG, "Download of " + mRemotePath + " to " + mLocalPath + ": " + result.getLogMessage(), e);
+ Log.e(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage(), e);
}
return result;
protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
int status = -1;
boolean savedFile = false;
- GetMethod get = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
+ GetMethod get = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()));
Iterator<OnDatatransferProgressListener> it = null;
FileOutputStream fos = null;
transferred += readResult;
it = mDataTransferListeners.iterator();
while (it.hasNext()) {
- it.next().onTransferProgress(readResult, transferred, mSize, targetFile.getName());
+ it.next().onTransferProgress(readResult, transferred, mFile.getFileLength(), targetFile.getName());
}
}
savedFile = true;
public void cancel() {
mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
}
-
+
}