Search in sources :

Example 11 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 12 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)

Example 13 with RemoteFile

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

the class FetchRemoteFileTask method doInBackground.

@Override
protected String doInBackground(Void... voids) {
    SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation(fileId, FILE_ID_SEARCH, false, fileDisplayActivity.getCapabilities());
    RemoteOperationResult remoteOperationResult = searchRemoteOperation.execute(user.toPlatformAccount(), fileDisplayActivity);
    if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null) {
        if (remoteOperationResult.getData().isEmpty()) {
            return fileDisplayActivity.getString(R.string.remote_file_fetch_failed);
        }
        String remotePath = ((RemoteFile) remoteOperationResult.getData().get(0)).getRemotePath();
        ReadFileRemoteOperation operation = new ReadFileRemoteOperation(remotePath);
        RemoteOperationResult result = operation.execute(user.toPlatformAccount(), fileDisplayActivity);
        if (!result.isSuccess()) {
            Exception exception = result.getException();
            String message = "Fetching file " + remotePath + " fails with: " + result.getLogMessage();
            if (exception != null) {
                return exception.getMessage();
            } else {
                return message;
            }
        }
        RemoteFile remoteFile = (RemoteFile) result.getData().get(0);
        OCFile ocFile = FileStorageUtils.fillOCFile(remoteFile);
        FileStorageUtils.searchForLocalFileInDefaultPath(ocFile, user.getAccountName());
        ocFile = storageManager.saveFileWithParent(ocFile, fileDisplayActivity);
        // also sync folder content
        OCFile toSync;
        if (ocFile.isFolder()) {
            toSync = ocFile;
        } else {
            toSync = storageManager.getFileById(ocFile.getParentId());
        }
        long currentSyncTime = System.currentTimeMillis();
        RemoteOperation refreshFolderOperation = new RefreshFolderOperation(toSync, currentSyncTime, true, true, storageManager, user, fileDisplayActivity);
        refreshFolderOperation.execute(user.toPlatformAccount(), fileDisplayActivity);
        fileDisplayActivity.setFile(ocFile);
    } else {
        return remoteOperationResult.getLogMessage();
    }
    return "";
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Example 14 with RemoteFile

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

the class GallerySearchTask method findLastTimestamp.

private long findLastTimestamp(ArrayList<RemoteFile> remoteFiles) {
    int lastPosition = remoteFiles.size() - 1;
    if (lastPosition < 0) {
        return -1;
    }
    RemoteFile lastFile = remoteFiles.get(lastPosition);
    return lastFile.getModifiedTimestamp() / 1000;
}
Also used : RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Example 15 with RemoteFile

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

the class OCFileListAdapter method parseVirtuals.

private void parseVirtuals(List<Object> objects, SearchType searchType) {
    VirtualFolderType type;
    boolean onlyMedia = false;
    switch(searchType) {
        case FAVORITE_SEARCH:
            type = VirtualFolderType.FAVORITE;
            break;
        case GALLERY_SEARCH:
            type = VirtualFolderType.GALLERY;
            onlyMedia = true;
            int lastPosition = objects.size() - 1;
            if (lastPosition < 0) {
                lastTimestamp = -1;
                break;
            }
            RemoteFile lastFile = (RemoteFile) objects.get(lastPosition);
            lastTimestamp = lastFile.getModifiedTimestamp() / 1000;
            break;
        default:
            type = VirtualFolderType.NONE;
            break;
    }
    List<ContentValues> contentValues = new ArrayList<>();
    for (Object remoteFile : objects) {
        OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) remoteFile);
        FileStorageUtils.searchForLocalFileInDefaultPath(ocFile, user.getAccountName());
        try {
            ocFile = mStorageManager.saveFileWithParent(ocFile, activity);
            if (SearchType.GALLERY_SEARCH != searchType) {
                // also sync folder content
                if (ocFile.isFolder()) {
                    long currentSyncTime = System.currentTimeMillis();
                    RemoteOperation refreshFolderOperation = new RefreshFolderOperation(ocFile, currentSyncTime, true, false, mStorageManager, user, activity);
                    refreshFolderOperation.execute(user.toPlatformAccount(), activity);
                }
            }
            if (!onlyMedia || MimeTypeUtil.isImage(ocFile) || MimeTypeUtil.isVideo(ocFile)) {
                mFiles.add(ocFile);
            }
            ContentValues cv = new ContentValues();
            cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, type.toString());
            cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
            contentValues.add(cv);
        } catch (RemoteOperationFailedException e) {
            Log_OC.e(TAG, "Error saving file with parent" + e.getMessage(), e);
        }
    }
    preferences.setPhotoSearchTimestamp(System.currentTimeMillis());
    mStorageManager.saveVirtuals(contentValues);
}
Also used : ContentValues(android.content.ContentValues) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ArrayList(java.util.ArrayList) VirtualFolderType(com.owncloud.android.datamodel.VirtualFolderType) Point(android.graphics.Point) OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationFailedException(com.owncloud.android.operations.RemoteOperationFailedException) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Aggregations

RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)29 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)15 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 ReadFolderRemoteOperation (com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)9 CreateFolderRemoteOperation (com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation)8 OCFile (com.owncloud.android.datamodel.OCFile)6 List (java.util.List)6 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)5 CreateShareRemoteOperation (com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation)5 ShareeUser (com.owncloud.android.lib.resources.shares.ShareeUser)4 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)4 File (java.io.File)4 ContentValues (android.content.ContentValues)3 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)3 WebdavEntry (com.owncloud.android.lib.common.network.WebdavEntry)3 ToggleEncryptionRemoteOperation (com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)3 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)3 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)2 RemoveFileRemoteOperation (com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation)2