Search in sources :

Example 1 with OnDatatransferProgressListener

use of com.owncloud.android.lib.common.network.OnDatatransferProgressListener 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)

Example 2 with OnDatatransferProgressListener

use of com.owncloud.android.lib.common.network.OnDatatransferProgressListener in project android by owncloud.

the class UploadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    mCancellationRequested.set(false);
    mUploadStarted.set(true);
    RemoteOperationResult result = null;
    File temporalFile = null, originalFile = new File(mOriginalStoragePath), expectedFile = null;
    try {
        /// Check that connectivity conditions are met and delays the upload otherwise
        if (delayForWifi()) {
            Log_OC.d(TAG, "Upload delayed until WiFi is available: " + getRemotePath());
            return new RemoteOperationResult(ResultCode.DELAYED_FOR_WIFI);
        }
        /// check if the file continues existing before schedule the operation
        if (!originalFile.exists()) {
            Log_OC.d(TAG, mOriginalStoragePath.toString() + " not exists anymore");
            return new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
        }
        /// check the existence of the parent folder for the file to upload
        String remoteParentPath = new File(getRemotePath()).getParent();
        remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ? remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
        result = grantFolderExistence(remoteParentPath, client);
        if (!result.isSuccess()) {
            return result;
        }
        /// set parent local id in uploading file
        OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
        mFile.setParentId(parent.getFileId());
        /// automatic rename of file to upload in case of name collision in server
        Log_OC.d(TAG, "Checking name collision in server");
        if (!mForceOverwrite) {
            String remotePath = getAvailableRemotePath(client, mRemotePath);
            mWasRenamed = !remotePath.equals(mRemotePath);
            if (mWasRenamed) {
                createNewOCFile(remotePath);
                Log_OC.d(TAG, "File renamed as " + remotePath);
            }
            mRemotePath = remotePath;
            mRenameUploadListener.onRenameUpload();
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
        expectedFile = new File(expectedPath);
        /// copy the file locally before uploading
        if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY && !mOriginalStoragePath.equals(expectedPath)) {
            String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
            mFile.setStoragePath(temporalPath);
            temporalFile = new File(temporalPath);
            result = copy(originalFile, temporalFile);
            if (result != null) {
                return result;
            }
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        // Get the last modification date of the file from the file system
        Long timeStampLong = originalFile.lastModified() / 1000;
        String timeStamp = timeStampLong.toString();
        /// perform the upload
        if (mChunked && (new File(mFile.getStoragePath())).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE) {
            mUploadOperation = new ChunkedUploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
        } else {
            mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
        }
        Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
        while (listener.hasNext()) {
            mUploadOperation.addDatatransferProgressListener(listener.next());
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        result = mUploadOperation.execute(client);
        // location in the ownCloud local folder
        if (result.isSuccess()) {
            if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
                String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
                if (mOriginalStoragePath.equals(temporalPath)) {
                    // delete local file is was pre-copied in temporary folder (see .ui.helpers.UriUploader)
                    temporalFile = new File(temporalPath);
                    temporalFile.delete();
                }
                mFile.setStoragePath("");
            } else {
                mFile.setStoragePath(expectedPath);
                if (temporalFile != null) {
                    // FileUploader.LOCAL_BEHAVIOUR_COPY
                    move(temporalFile, expectedFile);
                } else {
                    // FileUploader.LOCAL_BEHAVIOUR_MOVE
                    move(originalFile, expectedFile);
                    getStorageManager().deleteFileInMediaScan(originalFile.getAbsolutePath());
                }
                FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath());
            }
        } else if (result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
            result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        mUploadStarted.set(false);
        if (temporalFile != null && !originalFile.equals(temporalFile)) {
            temporalFile.delete();
        }
        if (result == null) {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
        if (result.isSuccess()) {
            Log_OC.i(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
        } else {
            if (result.getException() != null) {
                if (result.isCancelled()) {
                    Log_OC.w(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
                } else {
                    Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage(), result.getException());
                }
            } else {
                Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
            }
        }
    }
    if (result.isSuccess()) {
        saveUploadedFile(client);
    } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ChunkedUploadRemoteFileOperation(com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation) UploadRemoteFileOperation(com.owncloud.android.lib.resources.files.UploadRemoteFileOperation) OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) ChunkedUploadRemoteFileOperation(com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException)

Aggregations

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