Search in sources :

Example 1 with CreateFolderRemoteOperation

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

the class FileDataStorageManagerIT method testSaveNewFile.

@Test
public void testSaveNewFile() {
    assertTrue(new CreateFolderRemoteOperation("/1/1/", true).execute(client).isSuccess());
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/1/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    OCFile newFile = new OCFile("/1/1/1.txt");
    newFile.setRemoteId("123");
    sut.saveNewFile(newFile);
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) Test(org.junit.Test)

Example 2 with CreateFolderRemoteOperation

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

the class FileDisplayActivityIT method showShares.

@Test
public // @ScreenshotTest // todo run without real server
void showShares() {
    assertTrue(new ExistenceCheckRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareToGroup/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/shareViaLink/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/noShare/", true).execute(client).isSuccess());
    // assertTrue(new CreateFolderRemoteOperation("/shareToCircle/", true).execute(client).isSuccess());
    // share folder to user "admin"
    assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(client).isSuccess());
    // share folder via public link
    assertTrue(new CreateShareRemoteOperation("/shareViaLink/", ShareType.PUBLIC_LINK, "", true, "", OCShare.READ_PERMISSION_FLAG).execute(client).isSuccess());
    // share folder to group
    Assert.assertTrue(new CreateShareRemoteOperation("/shareToGroup/", ShareType.GROUP, "users", false, "", OCShare.NO_PERMISSION).execute(client).isSuccess());
    // share folder to circle
    // get share
    // RemoteOperationResult searchResult = new GetShareesRemoteOperation("publicCircle", 1, 50).execute(client);
    // assertTrue(searchResult.getLogMessage(), searchResult.isSuccess());
    // 
    // JSONObject resultJson = (JSONObject) searchResult.getData().get(0);
    // String circleId = resultJson.getJSONObject("value").getString("shareWith");
    // 
    // assertTrue(new CreateShareRemoteOperation("/shareToCircle/",
    // ShareType.CIRCLE,
    // circleId,
    // false,
    // "",
    // OCShare.DEFAULT_PERMISSION)
    // .execute(client).isSuccess());
    Activity sut = activityRule.launchActivity(null);
    getInstrumentation().waitForIdleSync();
    EventBus.getDefault().post(new SearchEvent("", SearchRemoteOperation.SearchType.SHARED_FILTER));
    shortSleep();
    shortSleep();
    screenshot(sut);
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) FileDisplayActivity(com.owncloud.android.ui.activity.FileDisplayActivity) Activity(android.app.Activity) SearchEvent(com.owncloud.android.ui.events.SearchEvent) CreateShareRemoteOperation(com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation) Test(org.junit.Test)

Example 3 with CreateFolderRemoteOperation

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

the class CreateFolderOperation method encryptedCreate.

private RemoteOperationResult encryptedCreate(OCFile parent, OwnCloudClient client) {
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String privateKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PRIVATE_KEY);
    String publicKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY);
    String token = null;
    Boolean metadataExists;
    DecryptedFolderMetadata metadata;
    String encryptedRemotePath = null;
    String filename = new File(remotePath).getName();
    try {
        // lock folder
        token = EncryptionUtils.lockFolder(parent, client);
        // get metadata
        Pair<Boolean, DecryptedFolderMetadata> metadataPair = EncryptionUtils.retrieveMetadata(parent, client, privateKey, publicKey);
        metadataExists = metadataPair.first;
        metadata = metadataPair.second;
        // check if filename already exists
        if (isFileExisting(metadata, filename)) {
            return new RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
        }
        // generate new random file name, check if it exists in metadata
        String encryptedFileName = createRandomFileName(metadata);
        encryptedRemotePath = parent.getRemotePath() + encryptedFileName;
        RemoteOperationResult result = new CreateFolderRemoteOperation(encryptedRemotePath, true, token).execute(client);
        if (result.isSuccess()) {
            // update metadata
            metadata.getFiles().put(encryptedFileName, createDecryptedFile(filename));
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
            String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
            // upload metadata
            EncryptionUtils.uploadMetadata(parent, serializedFolderMetadata, token, client, metadataExists);
            // unlock folder
            if (token != null) {
                RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parent, client, token);
                if (unlockFolderResult.isSuccess()) {
                    token = null;
                } else {
                    // TODO do better
                    throw new RuntimeException("Could not unlock folder!");
                }
            }
            RemoteOperationResult remoteFolderOperationResult = new ReadFolderRemoteOperation(encryptedRemotePath).execute(client);
            createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
            OCFile newDir = createRemoteFolderOcFile(parent, filename, createdRemoteFolder);
            getStorageManager().saveFile(newDir);
            RemoteOperationResult encryptionOperationResult = new ToggleEncryptionRemoteOperation(newDir.getLocalId(), newDir.getRemotePath(), true).execute(client);
            if (!encryptionOperationResult.isSuccess()) {
                throw new RuntimeException("Error creating encrypted subfolder!");
            }
        } else {
            // revert to sane state in case of any error
            Log_OC.e(TAG, remotePath + " hasn't been created");
        }
        return result;
    } catch (Exception e) {
        if (!EncryptionUtils.unlockFolder(parent, client, token).isSuccess()) {
            throw new RuntimeException("Could not clean up after failing folder creation!");
        }
        // remove folder
        if (encryptedRemotePath != null) {
            RemoteOperationResult removeResult = new RemoveRemoteEncryptedFileOperation(encryptedRemotePath, parent.getLocalId(), user.toPlatformAccount(), context, filename).execute(client);
            if (!removeResult.isSuccess()) {
                throw new RuntimeException("Could not clean up after failing folder creation!");
            }
        }
        // TODO do better
        return new RemoteOperationResult(e);
    } finally {
        // unlock folder
        if (token != null) {
            RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parent, client, token);
            if (!unlockFolderResult.isSuccess()) {
                // TODO do better
                throw new RuntimeException("Could not unlock folder!");
            }
        }
    }
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation) OCFile(com.owncloud.android.datamodel.OCFile) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)

Example 4 with CreateFolderRemoteOperation

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

the class CreateFolderOperation method normalCreate.

private RemoteOperationResult normalCreate(OwnCloudClient client) {
    RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
    if (result.isSuccess()) {
        RemoteOperationResult remoteFolderOperationResult = new ReadFolderRemoteOperation(remotePath).execute(client);
        createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
        saveFolderInDB();
    } else {
        Log_OC.e(TAG, remotePath + " hasn't been created");
    }
    return result;
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)

Example 5 with CreateFolderRemoteOperation

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

the class FileDataStorageManagerIT method testFolderContent.

@Test
public void testFolderContent() throws IOException {
    assertEquals(0, sut.getAllFiles().size());
    assertTrue(new CreateFolderRemoteOperation("/1/1/", true).execute(client).isSuccess());
    assertTrue(new CreateFolderRemoteOperation("/1/2/", true).execute(client).isSuccess());
    assertTrue(new UploadFileRemoteOperation(getDummyFile("/chunkedFile.txt").getAbsolutePath(), "/1/1/chunkedFile.txt", "text/plain", String.valueOf(System.currentTimeMillis() / 1000)).execute(client).isSuccess());
    assertTrue(new UploadFileRemoteOperation(getDummyFile("/chunkedFile.txt").getAbsolutePath(), "/1/1/chunkedFile2.txt", "text/plain", String.valueOf(System.currentTimeMillis() / 1000)).execute(client).isSuccess());
    File imageFile = getFile("imageFile.png");
    assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(), "/1/1/imageFile.png", "image/png", String.valueOf(System.currentTimeMillis() / 1000)).execute(client).isSuccess());
    // sync
    assertNull(sut.getFileByDecryptedRemotePath("/1/1/"));
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/1/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertEquals(3, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/1/1/"), false).size());
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File) Test(org.junit.Test)

Aggregations

CreateFolderRemoteOperation (com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation)6 Test (org.junit.Test)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)2 ReadFolderRemoteOperation (com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)2 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)2 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)2 File (java.io.File)2 Activity (android.app.Activity)1 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)1 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 OCFile (com.owncloud.android.datamodel.OCFile)1 ToggleEncryptionRemoteOperation (com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)1 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)1 UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)1 CreateShareRemoteOperation (com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation)1 FileDisplayActivity (com.owncloud.android.ui.activity.FileDisplayActivity)1 SearchEvent (com.owncloud.android.ui.events.SearchEvent)1