X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/38512099c49a32406725086074ac4973220807c1..bcc972d63d3ed1d76b51a8a03a27e809983dc756:/src/com/owncloud/android/operations/DownloadFileOperation.java?ds=inline diff --git a/src/com/owncloud/android/operations/DownloadFileOperation.java b/src/com/owncloud/android/operations/DownloadFileOperation.java index fbeedd8a..8d89f636 100644 --- a/src/com/owncloud/android/operations/DownloadFileOperation.java +++ b/src/com/owncloud/android/operations/DownloadFileOperation.java @@ -1,10 +1,9 @@ /* ownCloud Android client application - * Copyright (C) 2012 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,195 +17,155 @@ package com.owncloud.android.operations; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.http.HttpStatus; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener; +import com.owncloud.android.oc_framework.network.webdav.WebdavClient; +import com.owncloud.android.oc_framework.operations.RemoteFile; +import com.owncloud.android.oc_framework.operations.RemoteOperation; +import com.owncloud.android.oc_framework.operations.RemoteOperationResult; +import com.owncloud.android.oc_framework.operations.remote.DownloadRemoteFileOperation; +import com.owncloud.android.utils.FileStorageUtils; +import com.owncloud.android.utils.Log_OC; -import com.owncloud.android.files.services.FileDownloader; -import com.owncloud.android.operations.RemoteOperation; -import com.owncloud.android.operations.RemoteOperationResult; - -import eu.alefzero.webdav.OnDatatransferProgressListener; -import eu.alefzero.webdav.WebdavClient; -import eu.alefzero.webdav.WebdavUtils; import android.accounts.Account; -import android.util.Log; import android.webkit.MimeTypeMap; /** - * Remote operation performing the download of a file to an ownCloud server + * Remote mDownloadOperation performing the download of a file to an ownCloud server * * @author David A. Velasco + * @author masensio */ public class DownloadFileOperation extends RemoteOperation { - private static final String TAG = DownloadFileOperation.class.getCanonicalName(); + private static final String TAG = DownloadFileOperation.class.getSimpleName(); - private Account mAccount = null; - private String mLocalPath = null; - private String mRemotePath = null; - private String mMimeType = null; - private long mSize = -1; - private Boolean mCancellationRequested = false; - + private Account mAccount; + private OCFile mFile; private Set mDataTransferListeners = new HashSet(); + private long mModificationTimestamp = 0; + + private DownloadRemoteFileOperation mDownloadOperation; - 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() { + String path = mFile.getStoragePath(); // re-downloads should be done over the original file + if (path != null && path.length() > 0) { + return path; + } + return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile); } - public long getSize() { - return mSize; + public String getTmpPath() { + return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath(); } + public String getTmpFolder() { + return FileStorageUtils.getTemporalPath(mAccount.name); + } - 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_OC.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 long getModificationTimestamp() { + return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp(); + } + @Override protected RemoteOperationResult run(WebdavClient client) { RemoteOperationResult result = null; File newFile = null; - boolean moved = false; + boolean moved = true; + + /// download will be performed to a temporal file, then moved to the final location + File tmpFile = new File(getTmpPath()); - /// download will be in a temporal file - File tmpFile = new File(FileDownloader.getTemporalPath(mAccount.name) + mLocalPath); + String tmpFolder = getTmpFolder(); + RemoteFile remoteFile = FileStorageUtils.fillRemoteFile(mFile); /// perform the download - try { - tmpFile.getParentFile().mkdirs(); - int status = downloadFile(client, tmpFile); - if (isSuccess(status)) { - newFile = new File(FileDownloader.getSavePath(mAccount.name) + mLocalPath); - newFile.getParentFile().mkdirs(); - moved = tmpFile.renameTo(newFile); - } + mDownloadOperation = new DownloadRemoteFileOperation(remoteFile, tmpFolder); + Iterator listener = mDataTransferListeners.iterator(); + while (listener.hasNext()) { + mDownloadOperation.addDatatransferProgressListener(listener.next()); + } + result = mDownloadOperation.execute(client); + + if (result.isSuccess()) { + newFile = new File(getSavePath()); + newFile.getParentFile().mkdirs(); + moved = tmpFile.renameTo(newFile); + if (!moved) - 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()); - - } catch (Exception e) { - result = new RemoteOperationResult(e); - Log.e(TAG, "Download of " + mRemotePath + " to " + mLocalPath + ": " + result.getLogMessage(), e); + result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED); } + Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage()); + return result; } - - public boolean isSuccess(int status) { - return (status == HttpStatus.SC_OK); + public void cancel() { + mDownloadOperation.cancel(); } - - - 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)); - Iterator it = null; - - try { - status = client.executeMethod(get); - if (isSuccess(status)) { - targetFile.createNewFile(); - BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream()); - FileOutputStream fos = new FileOutputStream(targetFile); - long transferred = 0; - byte[] bytes = new byte[4096]; - int readResult = 0; - while ((readResult = bis.read(bytes)) != -1) { - synchronized(mCancellationRequested) { - if (mCancellationRequested) { - throw new OperationCancelledException(); - } - } - fos.write(bytes, 0, readResult); - transferred += readResult; - it = mDataTransferListeners.iterator(); - while (it.hasNext()) { - it.next().onTransferProgress(readResult, transferred, mSize, targetFile.getName()); - } - } - fos.close(); - savedFile = true; - - } else { - client.exhaustResponse(get.getResponseBodyAsStream()); - } - - } finally { - if (!savedFile && targetFile.exists()) { - targetFile.delete(); - } - get.releaseConnection(); // let the connection available for other methods + + public void addDatatransferProgressListener (OnDatatransferProgressListener listener) { + synchronized (mDataTransferListeners) { + mDataTransferListeners.add(listener); } - return status; } - - public void cancel() { - synchronized(mCancellationRequested) { - mCancellationRequested = true; + public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) { + synchronized (mDataTransferListeners) { + mDataTransferListeners.remove(listener); } }