Search in sources :

Example 56 with RemoteOperationResult

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

the class UploadFileOperation method grantFolderExistence.

/**
 * Checks the existence of the folder where the current file will be uploaded both
 * in the remote server and in the local database.
 * <p/>
 * If the upload is set to enforce the creation of the folder, the method tries to
 * create it both remote and locally.
 *
 * @param pathToGrant Full remote path whose existence will be granted.
 * @return An {@link OCFile} instance corresponding to the folder where the file
 * will be uploaded.
 */
private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
    RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, false);
    RemoteOperationResult result = operation.execute(client);
    if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
        SyncOperation syncOp = new CreateFolderOperation(pathToGrant, user, getContext(), getStorageManager());
        result = syncOp.execute(client);
    }
    if (result.isSuccess()) {
        OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
        if (parentDir == null) {
            parentDir = createLocalFolder(pathToGrant);
        }
        if (parentDir != null) {
            result = new RemoteOperationResult(ResultCode.OK);
        } else {
            result = new RemoteOperationResult(ResultCode.CANNOT_CREATE_FILE);
        }
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ChunkedFileUploadRemoteOperation(com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) SyncOperation(com.owncloud.android.operations.common.SyncOperation)

Example 57 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult 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 58 with RemoteOperationResult

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

the class UpdateSharePermissionsOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // ShareType.USER | ShareType.GROUP
    OCShare share = getStorageManager().getShareById(shareId);
    if (share == null) {
        // TODO try to get remote share before failing?
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
    }
    path = share.getPath();
    // Update remote share with password
    UpdateShareRemoteOperation updateOp = new UpdateShareRemoteOperation(share.getRemoteId());
    updateOp.setPassword(password);
    updateOp.setPermissions(permissions);
    updateOp.setExpirationDate(expirationDateInMillis);
    RemoteOperationResult result = updateOp.execute(client);
    if (result.isSuccess()) {
        RemoteOperation getShareOp = new GetShareRemoteOperation(share.getRemoteId());
        result = getShareOp.execute(client);
        if (result.isSuccess()) {
            share = (OCShare) result.getData().get(0);
            // TODO check permissions are being saved
            updateData(share);
        }
    }
    return result;
}
Also used : UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) UpdateShareRemoteOperation(com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetShareRemoteOperation(com.owncloud.android.lib.resources.shares.GetShareRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare)

Example 59 with RemoteOperationResult

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

the class DocumentsStorageProvider method createFolder.

private String createFolder(Document targetFolder, String displayName) throws FileNotFoundException {
    Context context = getNonNullContext();
    String newDirPath = targetFolder.getRemotePath() + displayName + PATH_SEPARATOR;
    FileDataStorageManager storageManager = targetFolder.getStorageManager();
    RemoteOperationResult result = new CreateFolderOperation(newDirPath, accountManager.getUser(), context, storageManager).execute(targetFolder.getClient());
    if (!result.isSuccess()) {
        Log_OC.e(TAG, result.toString());
        throw new FileNotFoundException("Failed to create document with name " + displayName + " and documentId " + targetFolder.getDocumentId());
    }
    RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), false, false, true, storageManager, targetFolder.getUser(), context).execute(targetFolder.getClient());
    if (!updateParent.isSuccess()) {
        Log_OC.e(TAG, updateParent.toString());
        throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId());
    }
    Document newFolder = new Document(storageManager, newDirPath);
    context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
    return newFolder.getDocumentId();
}
Also used : Context(android.content.Context) CreateFolderOperation(com.owncloud.android.operations.CreateFolderOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) FileNotFoundException(java.io.FileNotFoundException)

Example 60 with RemoteOperationResult

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

the class DocumentsStorageProvider method renameDocument.

@Override
public String renameDocument(String documentId, String displayName) throws FileNotFoundException {
    Log.d(TAG, "renameDocument(), id=" + documentId);
    Document document = toDocument(documentId);
    RemoteOperationResult result = new RenameFileOperation(document.getRemotePath(), displayName, document.getStorageManager()).execute(document.getClient());
    if (!result.isSuccess()) {
        Log_OC.e(TAG, result.toString());
        throw new FileNotFoundException("Failed to rename document with documentId " + documentId + ": " + result.getException());
    }
    Context context = getNonNullContext();
    context.getContentResolver().notifyChange(toNotifyUri(document.getParent()), null, false);
    return null;
}
Also used : Context(android.content.Context) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileNotFoundException(java.io.FileNotFoundException) RenameFileOperation(com.owncloud.android.operations.RenameFileOperation)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)172 OCFile (com.owncloud.android.datamodel.OCFile)48 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)24 ArrayList (java.util.ArrayList)24 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)22 IOException (java.io.IOException)22 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)18 OCShare (com.owncloud.android.lib.resources.shares.OCShare)18 File (java.io.File)18 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)17 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)16 Context (android.content.Context)15 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)12 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 FileNotFoundException (java.io.FileNotFoundException)12 Intent (android.content.Intent)11 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)11 Uri (android.net.Uri)10