Search in sources :

Example 6 with UploadFileRemoteOperation

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

the class UploadFileOperation method normalUpload.

private RemoteOperationResult normalUpload(OwnCloudClient client) {
    RemoteOperationResult result = null;
    File temporalFile = null;
    File originalFile = new File(mOriginalStoragePath);
    File expectedFile = null;
    FileLock fileLock = null;
    long size;
    try {
        // check conditions
        result = checkConditions(originalFile);
        if (result != null) {
            return result;
        }
        // check name collision
        RemoteOperationResult collisionResult = checkNameCollision(client, null, false);
        if (collisionResult != null) {
            result = collisionResult;
            return collisionResult;
        }
        String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
        expectedFile = new File(expectedPath);
        result = copyFile(originalFile, expectedPath);
        if (!result.isSuccess()) {
            return result;
        }
        // Get the last modification date of the file from the file system
        String lastModifiedTimestamp = Long.toString(originalFile.lastModified() / 1000);
        final Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
        FileChannel channel = null;
        try {
            channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
            fileLock = channel.tryLock();
        } catch (FileNotFoundException e) {
            // this basically means that the file is on SD card
            // try to copy file to temporary dir if it doesn't exist
            String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) + mFile.getRemotePath();
            mFile.setStoragePath(temporalPath);
            temporalFile = new File(temporalPath);
            Files.deleteIfExists(Paths.get(temporalPath));
            result = copy(originalFile, temporalFile);
            if (result.isSuccess()) {
                if (temporalFile.length() == originalFile.length()) {
                    channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
                    fileLock = channel.tryLock();
                } else {
                    result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
                }
            }
        }
        try {
            size = channel.size();
        } catch (Exception e1) {
            size = new File(mFile.getStoragePath()).length();
        }
        for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
            if (ocUpload.getUploadId() == getOCUploadId()) {
                ocUpload.setFileSize(size);
                uploadsStorageManager.updateUpload(ocUpload);
                break;
            }
        }
        // perform the upload
        if (size > ChunkedFileUploadRemoteOperation.CHUNK_SIZE_MOBILE) {
            boolean onWifiConnection = connectivityService.getConnectivity().isWifi();
            mUploadOperation = new ChunkedFileUploadRemoteOperation(mFile.getStoragePath(), mFile.getRemotePath(), mFile.getMimeType(), mFile.getEtagInConflict(), lastModifiedTimestamp, creationTimestamp, onWifiConnection, mDisableRetries);
        } else {
            mUploadOperation = new UploadFileRemoteOperation(mFile.getStoragePath(), mFile.getRemotePath(), mFile.getMimeType(), mFile.getEtagInConflict(), lastModifiedTimestamp, creationTimestamp, mDisableRetries);
        }
        for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners) {
            mUploadOperation.addDataTransferProgressListener(mDataTransferListener);
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        if (result.isSuccess() && mUploadOperation != null) {
            result = mUploadOperation.execute(client);
            // location in the Nextcloud local folder
            if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
                result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
            }
        }
    } catch (FileNotFoundException e) {
        Log_OC.d(TAG, mOriginalStoragePath + " not exists anymore");
        result = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
    } catch (OverlappingFileLockException e) {
        Log_OC.d(TAG, "Overlapping file lock exception");
        result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        mUploadStarted.set(false);
        if (fileLock != null) {
            try {
                fileLock.release();
            } catch (IOException e) {
                Log_OC.e(TAG, "Failed to unlock file with path " + mOriginalStoragePath);
            }
        }
        if (temporalFile != null && !originalFile.equals(temporalFile)) {
            temporalFile.delete();
        }
        if (result == null) {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
        logResult(result, mOriginalStoragePath, mRemotePath);
    }
    if (result.isSuccess()) {
        handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
    } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
    }
    // delete temporal file
    if (temporalFile != null && temporalFile.exists() && !temporalFile.delete()) {
        Log_OC.e(TAG, "Could not delete temporal file " + temporalFile.getAbsolutePath());
    }
    return result;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileNotFoundException(java.io.FileNotFoundException) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException) ChunkedFileUploadRemoteOperation(com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation) OCUpload(com.owncloud.android.db.OCUpload) RandomAccessFile(java.io.RandomAccessFile) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Aggregations

UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)6 File (java.io.File)6 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)5 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)4 OCFile (com.owncloud.android.datamodel.OCFile)3 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 ContentValues (android.content.ContentValues)2 OCUpload (com.owncloud.android.db.OCUpload)2 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)2 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)2 ChunkedFileUploadRemoteOperation (com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation)2 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)2 RandomAccessFile (java.io.RandomAccessFile)2 FileChannel (java.nio.channels.FileChannel)2 FileLock (java.nio.channels.FileLock)2 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)2 ArrayList (java.util.ArrayList)2