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;
}
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);
}
}
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;
}
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();
}
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;
}
Aggregations