Search in sources :

Example 1 with UploadRemoteFileOperation

use of com.owncloud.android.lib.resources.files.UploadRemoteFileOperation 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)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 ChunkedUploadRemoteFileOperation (com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation)1 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)1 UploadRemoteFileOperation (com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)1 File (java.io.File)1 IOException (java.io.IOException)1