Search in sources :

Example 1 with DownloadRemoteFileOperation

use of com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation in project android by owncloud.

the class DownloadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    File newFile;
    boolean moved;
    /// download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());
    String tmpFolder = getTmpFolder();
    /// perform the download
    synchronized (mCancellationRequested) {
        if (mCancellationRequested.get()) {
            return new RemoteOperationResult(new OperationCancelledException());
        }
    }
    mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
    Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
    while (listener.hasNext()) {
        mDownloadOperation.addDatatransferProgressListener(listener.next());
    }
    result = mDownloadOperation.execute(client);
    if (result.isSuccess()) {
        mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
        mEtag = mDownloadOperation.getEtag();
        if (FileStorageUtils.getUsableSpace(mAccount.name) < tmpFile.length()) {
            Log_OC.w(TAG, "Not enough space to copy " + tmpFile.getAbsolutePath());
        }
        newFile = new File(getSavePath());
        Log_OC.d(TAG, "Save path: " + newFile.getAbsolutePath());
        File parent = newFile.getParentFile();
        boolean created = parent.mkdirs();
        Log_OC.d(TAG, "Creation of parent folder " + parent.getAbsolutePath() + " succeeded: " + created);
        Log_OC.d(TAG, "Parent folder " + parent.getAbsolutePath() + " exists: " + parent.exists());
        Log_OC.d(TAG, "Parent folder " + parent.getAbsolutePath() + " is directory: " + parent.isDirectory());
        moved = tmpFile.renameTo(newFile);
        Log_OC.d(TAG, "New file " + newFile.getAbsolutePath() + " exists: " + newFile.exists());
        Log_OC.d(TAG, "New file " + newFile.getAbsolutePath() + " is directory: " + newFile.isDirectory());
        if (!moved) {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
        }
    }
    Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
    return result;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) DownloadRemoteFileOperation(com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Aggregations

OCFile (com.owncloud.android.datamodel.OCFile)1 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)1 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 DownloadRemoteFileOperation (com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation)1 File (java.io.File)1