Search in sources :

Example 1 with RemoteFile

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

the class FileDataStorageManagerIT method testPhotoSearch.

/**
 * This test creates an image, does a photo search (now returned image is not yet in file hierarchy), then root
 * folder is refreshed and it is verified that the same image file is used in database
 */
@Test
public void testPhotoSearch() throws IOException {
    String remotePath = "/imageFile.png";
    VirtualFolderType virtualType = VirtualFolderType.GALLERY;
    assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(1, sut.getAllFiles().size());
    File imageFile = getFile("imageFile.png");
    assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(), remotePath, "image/png", String.valueOf(System.currentTimeMillis() / 1000)).execute(client).isSuccess());
    assertNull(sut.getFileByDecryptedRemotePath(remotePath));
    // search
    SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("image/%", PHOTO_SEARCH, false, capability);
    RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
    TestCase.assertTrue(searchResult.isSuccess());
    TestCase.assertEquals(1, searchResult.getResultData().size());
    OCFile ocFile = FileStorageUtils.fillOCFile(searchResult.getResultData().get(0));
    sut.saveFile(ocFile);
    List<ContentValues> contentValues = new ArrayList<>();
    ContentValues cv = new ContentValues();
    cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
    cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
    contentValues.add(cv);
    sut.saveVirtuals(contentValues);
    assertEquals(remotePath, ocFile.getRemotePath());
    assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
    assertEquals(2, sut.getAllFiles().size());
    // update root
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertEquals(1, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
    assertEquals(2, sut.getAllFiles().size());
    assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0), sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
}
Also used : ContentValues(android.content.ContentValues) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File) Test(org.junit.Test)

Example 2 with RemoteFile

use of com.owncloud.android.lib.resources.files.model.RemoteFile 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 3 with RemoteFile

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

the class SynchronizeFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    mTransferWasRequested = false;
    if (mLocalFile == null) {
        // Get local file from the DB
        mLocalFile = getStorageManager().getFileByPath(mRemotePath);
    }
    if (!mLocalFile.isDown()) {
        // / easy decision
        requestForDownload(mLocalFile);
        result = new RemoteOperationResult(ResultCode.OK);
    } else {
        // / local copy in the device -> need to think a bit more before do anything
        if (mServerFile == null) {
            ReadFileRemoteOperation operation = new ReadFileRemoteOperation(mRemotePath);
            result = operation.execute(client);
            if (result.isSuccess()) {
                mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
            } else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
                return result;
            }
        }
        if (mServerFile != null) {
            // / check changes in server and local file
            boolean serverChanged;
            if (TextUtils.isEmpty(mLocalFile.getEtag())) {
                // file uploaded (null) or downloaded ("") before upgrade to version 1.8.0; check the old condition
                serverChanged = mServerFile.getModificationTimestamp() != mLocalFile.getModificationTimestampAtLastSyncForData();
            } else {
                serverChanged = !mServerFile.getEtag().equals(mLocalFile.getEtag());
            }
            boolean localChanged = mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData();
            // if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
            if (localChanged && serverChanged) {
                result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
                getStorageManager().saveConflict(mLocalFile, mServerFile.getEtag());
            } else if (localChanged) {
                if (mSyncFileContents && mAllowUploads) {
                    requestForUpload(mLocalFile);
                // the local update of file properties will be done by the FileUploader
                // service when the upload finishes
                } else {
                    // NOTHING TO DO HERE: updating the properties of the file in the server
                    // without uploading the contents would be stupid;
                    // So, an instance of SynchronizeFileOperation created with
                    // syncFileContents == false is completely useless when we suspect
                    // that an upload is necessary (for instance, in FileObserverService).
                    Log_OC.d(TAG, "Nothing to do here");
                }
                result = new RemoteOperationResult(ResultCode.OK);
            } else if (serverChanged) {
                mLocalFile.setRemoteId(mServerFile.getRemoteId());
                if (mSyncFileContents) {
                    // local, not server; we won't to keep
                    requestForDownload(mLocalFile);
                // the value of favorite!
                // the update of local data will be done later by the FileUploader
                // service when the upload finishes
                } else {
                    // TODO CHECK: is this really useful in some point in the code?
                    mServerFile.setFavorite(mLocalFile.isFavorite());
                    mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
                    mServerFile.setStoragePath(mLocalFile.getStoragePath());
                    mServerFile.setParentId(mLocalFile.getParentId());
                    mServerFile.setEtag(mLocalFile.getEtag());
                    getStorageManager().saveFile(mServerFile);
                }
                result = new RemoteOperationResult(ResultCode.OK);
            } else {
                // nothing changed, nothing to do
                result = new RemoteOperationResult(ResultCode.OK);
            }
            // safe blanket: sync'ing a not in-conflict file will clean wrong conflict markers in ancestors
            if (result.getCode() != ResultCode.SYNC_CONFLICT) {
                getStorageManager().saveConflict(mLocalFile, null);
            }
        } else {
            // remote file does not exist, deleting local copy
            boolean deleteResult = getStorageManager().removeFile(mLocalFile, true, true);
            if (deleteResult) {
                result = new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
            } else {
                Log_OC.e(TAG, "Removal of local copy failed (remote file does not exist any longer).");
            }
        }
    }
    Log_OC.i(TAG, "Synchronizing " + mUser.getAccountName() + ", file " + mLocalFile.getRemotePath() + ": " + result.getLogMessage());
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Example 4 with RemoteFile

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

the class FileDataStorageManagerIT method testGallerySearch.

/**
 * This test creates an image and a video, does a gallery search (now returned image and video is not yet in file
 * hierarchy), then root folder is refreshed and it is verified that the same image file is used in database
 */
@Test
public void testGallerySearch() throws IOException {
    sut = new FileDataStorageManager(user, targetContext.getContentResolver().acquireContentProviderClient(ProviderMeta.ProviderTableMeta.CONTENT_URI));
    String imagePath = "/imageFile.png";
    VirtualFolderType virtualType = VirtualFolderType.GALLERY;
    assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(1, sut.getAllFiles().size());
    File imageFile = getFile("imageFile.png");
    assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(), imagePath, "image/png", String.valueOf((System.currentTimeMillis() - 10000) / 1000)).execute(client).isSuccess());
    // Check that file does not yet exist in local database
    assertNull(sut.getFileByDecryptedRemotePath(imagePath));
    String videoPath = "/videoFile.mp4";
    File videoFile = getFile("videoFile.mp4");
    assertTrue(new UploadFileRemoteOperation(videoFile.getAbsolutePath(), videoPath, "video/mpeg", String.valueOf((System.currentTimeMillis() + 10000) / 1000)).execute(client).isSuccess());
    // Check that file does not yet exist in local database
    assertNull(sut.getFileByDecryptedRemotePath(videoPath));
    // search
    SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("", GALLERY_SEARCH, false, capability);
    RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
    TestCase.assertTrue(searchResult.isSuccess());
    TestCase.assertEquals(2, searchResult.getResultData().size());
    // newest file must be video path (as sorted by recently modified)
    OCFile ocFile = FileStorageUtils.fillOCFile(searchResult.getResultData().get(0));
    sut.saveFile(ocFile);
    assertEquals(videoPath, ocFile.getRemotePath());
    List<ContentValues> contentValues = new ArrayList<>();
    ContentValues cv = new ContentValues();
    cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
    cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
    contentValues.add(cv);
    // second is image file, as older
    OCFile ocFile2 = FileStorageUtils.fillOCFile(searchResult.getResultData().get(1));
    sut.saveFile(ocFile2);
    assertEquals(imagePath, ocFile2.getRemotePath());
    ContentValues cv2 = new ContentValues();
    cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
    cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile2.getFileId());
    contentValues.add(cv2);
    sut.saveVirtuals(contentValues);
    assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
    assertEquals(3, sut.getAllFiles().size());
    // update root
    assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
    assertEquals(2, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
    assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
    assertEquals(3, sut.getAllFiles().size());
    assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0), sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
}
Also used : ContentValues(android.content.ContentValues) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ArrayList(java.util.ArrayList) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) ArrayList(java.util.ArrayList) List(java.util.List) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File) Test(org.junit.Test)

Example 5 with RemoteFile

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

the class AbstractOnServerIT method deleteAllFiles.

public static void deleteAllFiles() {
    RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
    for (Object object : result.getData()) {
        RemoteFile remoteFile = (RemoteFile) object;
        if (!remoteFile.getRemotePath().equals("/")) {
            if (remoteFile.isEncrypted()) {
                assertTrue(new ToggleEncryptionRemoteOperation(remoteFile.getLocalId(), remoteFile.getRemotePath(), false).execute(client).isSuccess());
            }
            assertTrue(new RemoveFileRemoteOperation(remoteFile.getRemotePath()).execute(client).isSuccess());
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) RemoveFileRemoteOperation(com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation)

Aggregations

RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 OCFile (com.owncloud.android.datamodel.OCFile)6 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)5 ArrayList (java.util.ArrayList)5 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)4 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)4 ContentValues (android.content.ContentValues)3 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)3 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)3 File (java.io.File)3 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)2 ToggleEncryptionRemoteOperation (com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)2 ReadFolderRemoteOperation (com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)2 UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)2 List (java.util.List)2 Test (org.junit.Test)2 Activity (android.app.Activity)1 Bitmap (android.graphics.Bitmap)1 Point (android.graphics.Point)1 Fragment (androidx.fragment.app.Fragment)1