Search in sources :

Example 6 with OperationCancelledException

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

Example 7 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class SynchronizeFolderOperation method run.

/**
     * Performs the synchronization.
     *
     * {@inheritDoc}
     */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    mFailsInFileSyncsFound = 0;
    mConflictsFound = 0;
    mForgottenLocalFiles.clear();
    try {
        // get locally cached information about folder
        mLocalFolder = getStorageManager().getFileByPath(mRemotePath);
        if (mPushOnly) {
            // assuming there is no update in the server side, still need to handle local changes
            Log_OC.i(TAG, "Push only sync of " + mAccount.name + mRemotePath);
            preparePushOfLocalChanges();
            syncContents();
            //pushOnlySync();
            result = new RemoteOperationResult(ResultCode.OK);
        } else {
            // get list of files in folder from remote server
            result = fetchRemoteFolder(client);
            if (result.isSuccess()) {
                // success - merge updates in server with local state
                mergeRemoteFolder(result.getData());
                syncContents();
            } else {
                // fail fetching the server
                if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
                    removeLocalFolder();
                }
                if (result.isException()) {
                    Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " + result.getLogMessage(), result.getException());
                } else {
                    Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " + result.getLogMessage());
                }
            }
        }
    } catch (OperationCancelledException e) {
        result = new RemoteOperationResult(e);
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException)

Aggregations

OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)7 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)5 OCFile (com.owncloud.android.datamodel.OCFile)4 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 File (java.io.File)3 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)2 IOException (java.io.IOException)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 ChunkedUploadRemoteFileOperation (com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation)1 DownloadRemoteFileOperation (com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation)1 ReadRemoteFolderOperation (com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation)1 UploadRemoteFileOperation (com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)1 SyncOperation (com.owncloud.android.operations.common.SyncOperation)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1