Search in sources :

Example 1 with UnlockFileRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation 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);
    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 == 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 != null && !mFolderUnlockToken.isEmpty()) {
        UnlockFileRemoteOperation unlockFileOperation = new UnlockFileRemoteOperation(parent.getLocalId(), mFolderUnlockToken);
        RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client);
        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");
        return encryptedUpload(client, parent);
    } 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) RandomAccessFile(java.io.RandomAccessFile) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation)

Example 2 with UnlockFileRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation 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
        RemoteOperationResult lockFileOperationResult = new LockFileRemoteOperation(parentId).execute(client);
        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
        RemoteOperationResult getMetadataOperationResult = new GetMetadataRemoteOperation(parentId).execute(client);
        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.getFilesDavUri(remotePath));
        delete.setQueryString(new NameValuePair[] { new NameValuePair(E2E_TOKEN, token) });
        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
        RemoteOperationResult uploadMetadataOperationResult = new UpdateMetadataRemoteOperation(parentId, serializedFolderMetadata, token).execute(client);
        if (!uploadMetadataOperationResult.isSuccess()) {
            throw new RemoteOperationFailedException("Metadata not uploaded!");
        }
        // return success
        return result;
    } catch (NoSuchAlgorithmException | IOException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeySpecException 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) {
            RemoteOperationResult unlockFileOperationResult = new UnlockFileRemoteOperation(parentId, token).execute(client);
            if (!unlockFileOperationResult.isSuccess()) {
                Log_OC.e(TAG, "Failed to unlock " + parentId);
            }
        }
    }
    return result;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation) UpdateMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation) GetMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) LockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)2 UnlockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation)2 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OCUpload (com.owncloud.android.db.OCUpload)1 GetMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation)1 LockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)1 UpdateMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation)1 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 BadPaddingException (javax.crypto.BadPaddingException)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1