Search in sources :

Example 1 with UnlockFileOperation

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

the class UploadFileOperation method run.

@Override
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
protected RemoteOperationResult run(OwnCloudClient client) {
    mCancellationRequested.set(false);
    mUploadStarted.set(true);
    uploadsStorageManager = new UploadsStorageManager(mContext.getContentResolver(), mContext);
    for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
        if (ocUpload.getUploadId() == getOCUploadId()) {
            ocUpload.setFileSize(0);
            uploadsStorageManager.updateUpload(ocUpload);
            break;
        }
    }
    String remoteParentPath = new File(getRemotePath()).getParent();
    remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ? remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
    OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
    // in case of a fresh upload with subfolder, where parent does not exist yet
    if (parent == null && mFolderUnlockToken.isEmpty()) {
        // try to create folder
        RemoteOperationResult result = grantFolderExistence(remoteParentPath, client);
        if (!result.isSuccess()) {
            return result;
        }
        parent = getStorageManager().getFileByPath(remoteParentPath);
        if (parent == null) {
            return new RemoteOperationResult(false, "Parent folder not found", HttpStatus.SC_NOT_FOUND);
        }
    }
    // parent file is not null anymore:
    // - it was created on fresh upload or
    // - resume of encrypted upload, then parent file exists already as unlock is only for direct parent
    mFile.setParentId(parent.getFileId());
    // the parent folder should exist as it is a resume of a broken upload
    if (!mFolderUnlockToken.isEmpty()) {
        UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parent.getLocalId(), mFolderUnlockToken);
        RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client, true);
        if (!unlockFileOperationResult.isSuccess()) {
            return unlockFileOperationResult;
        }
    }
    // check if any parent is encrypted
    encryptedAncestor = FileStorageUtils.checkEncryptionStatus(parent, getStorageManager());
    mFile.setEncrypted(encryptedAncestor);
    if (encryptedAncestor) {
        Log_OC.d(TAG, "encrypted upload");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return encryptedUpload(client, parent);
        } else {
            Log_OC.e(TAG, "Encrypted upload on old Android API");
            return new RemoteOperationResult(ResultCode.OLD_ANDROID_API);
        }
    } else {
        Log_OC.d(TAG, "normal upload");
        return normalUpload(client);
    }
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) RandomAccessFile(java.io.RandomAccessFile) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) UnlockFileOperation(com.owncloud.android.lib.resources.files.UnlockFileOperation)

Example 2 with UnlockFileOperation

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

the class UploadFileOperation method unlockFolder.

private RemoteOperationResult unlockFolder(OCFile parentFolder, OwnCloudClient client, String token) {
    if (token != null) {
        UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parentFolder.getLocalId(), token);
        RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client, true);
        return unlockFileOperationResult;
    } else
        return new RemoteOperationResult(new Exception("No token available"));
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UnlockFileOperation(com.owncloud.android.lib.resources.files.UnlockFileOperation) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileNotFoundException(java.io.FileNotFoundException) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException)

Example 3 with UnlockFileOperation

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

the class RemoveRemoteEncryptedFileOperation method run.

/**
 * Performs the remove operation.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    DeleteMethod delete = null;
    String token = null;
    DecryptedFolderMetadata metadata;
    String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
    try {
        // Lock folder
        LockFileOperation lockFileOperation = new LockFileOperation(parentId);
        RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client, true);
        if (lockFileOperationResult.isSuccess()) {
            token = (String) lockFileOperationResult.getData().get(0);
        } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            throw new RemoteOperationFailedException("Forbidden! Please try again later.)");
        } else {
            throw new RemoteOperationFailedException("Unknown error!");
        }
        // refresh metadata
        GetMetadataOperation getMetadataOperation = new GetMetadataOperation(parentId);
        RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client, true);
        if (getMetadataOperationResult.isSuccess()) {
            // decrypt metadata
            String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
            });
            metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
        } else {
            throw new RemoteOperationFailedException("No Metadata found!");
        }
        // delete file remote
        delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(remotePath));
        int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
        // exhaust the response, although not interesting
        delete.getResponseBodyAsString();
        result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete);
        Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
        // remove file from metadata
        metadata.getFiles().remove(fileName);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
        String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
        // upload metadata
        UpdateMetadataOperation storeMetadataOperation = new UpdateMetadataOperation(parentId, serializedFolderMetadata, token);
        RemoteOperationResult uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
        if (!uploadMetadataOperationResult.isSuccess()) {
            throw new RemoteOperationFailedException("Metadata not uploaded!");
        }
        // return success
        return result;
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
    } finally {
        if (delete != null) {
            delete.releaseConnection();
        }
        // unlock file
        if (token != null) {
            UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parentId, token);
            RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client, true);
            if (!unlockFileOperationResult.isSuccess()) {
                Log_OC.e(TAG, "Failed to unlock " + parentId);
            }
        }
    }
    return result;
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UpdateMetadataOperation(com.owncloud.android.lib.resources.files.UpdateMetadataOperation) LockFileOperation(com.owncloud.android.lib.resources.files.LockFileOperation) GetMetadataOperation(com.owncloud.android.lib.resources.files.GetMetadataOperation) UnlockFileOperation(com.owncloud.android.lib.resources.files.UnlockFileOperation) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 UnlockFileOperation (com.owncloud.android.lib.resources.files.UnlockFileOperation)3 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 OCFile (com.owncloud.android.datamodel.OCFile)1 UploadsStorageManager (com.owncloud.android.datamodel.UploadsStorageManager)1 OCUpload (com.owncloud.android.db.OCUpload)1 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)1 GetMetadataOperation (com.owncloud.android.lib.resources.files.GetMetadataOperation)1 LockFileOperation (com.owncloud.android.lib.resources.files.LockFileOperation)1 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)1 UpdateMetadataOperation (com.owncloud.android.lib.resources.files.UpdateMetadataOperation)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)1