Search in sources :

Example 11 with RefreshFolderOperation

use of com.owncloud.android.operations.RefreshFolderOperation in project android by nextcloud.

the class UploadIT method testCreationAndUploadTimestamp.

@Test
public void testCreationAndUploadTimestamp() throws IOException {
    File file = getDummyFile("/empty.txt");
    String remotePath = "/testFile.txt";
    OCUpload ocUpload = new OCUpload(file.getAbsolutePath(), remotePath, account.name);
    long creationTimestamp = Files.readAttributes(file.toPath(), BasicFileAttributes.class).creationTime().to(TimeUnit.SECONDS);
    // wait a bit to simulate a later upload, so we can verify if creation date is set correct
    shortSleep();
    assertTrue(new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, false, false, getStorageManager()).setRemoteFolderToBeCreated().execute(client).isSuccess());
    long uploadTimestamp = System.currentTimeMillis() / 1000;
    // RefreshFolderOperation
    assertTrue(new RefreshFolderOperation(getStorageManager().getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, getStorageManager(), user, targetContext).execute(client).isSuccess());
    List<OCFile> files = getStorageManager().getFolderContent(getStorageManager().getFileByDecryptedRemotePath("/"), false);
    OCFile ocFile = files.get(0);
    assertEquals(remotePath, ocFile.getRemotePath());
    assertEquals(creationTimestamp, ocFile.getCreationTimestamp());
    assertTrue(uploadTimestamp - 10 < ocFile.getUploadTimestamp() || uploadTimestamp + 10 > ocFile.getUploadTimestamp());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) Test(org.junit.Test)

Example 12 with RefreshFolderOperation

use of com.owncloud.android.operations.RefreshFolderOperation in project android by owncloud.

the class ReceiveExternalFilesActivity method startSyncFolderOperation.

private void startSyncFolderOperation(OCFile folder) {
    mSyncInProgress = true;
    // perform folder synchronization
    SyncOperation synchFolderOp = new RefreshFolderOperation(folder, false, getAccount(), getApplicationContext());
    synchFolderOp.execute(getStorageManager(), this, null, null);
}
Also used : SyncOperation(com.owncloud.android.operations.common.SyncOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation)

Example 13 with RefreshFolderOperation

use of com.owncloud.android.operations.RefreshFolderOperation in project android by nextcloud.

the class FileSyncAdapter method synchronizeFolder.

/**
 *  Synchronizes the list of files contained in a folder identified with its remote path.
 *
 *  Fetches the list and properties of the files contained in the given folder, including their
 *  properties, and updates the local database with them.
 *
 *  Enters in the child folders to synchronize their contents also, following a recursive
 *  depth first strategy.
 *
 *  @param folder                   Folder to synchronize.
 */
private void synchronizeFolder(OCFile folder) {
    if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult)) {
        return;
    }
    // folder synchronization
    RefreshFolderOperation synchFolderOp = new RefreshFolderOperation(folder, mCurrentSyncTime, true, false, getStorageManager(), getUser(), getContext());
    RemoteOperationResult result = synchFolderOp.execute(getClient());
    // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
    sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result);
    // check the result of synchronizing the folder
    if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
        if (result.getCode() == ResultCode.SYNC_CONFLICT) {
            mConflictsFound += synchFolderOp.getConflictsFound();
            mFailsInFavouritesFound += synchFolderOp.getFailsInKeptInSyncFound();
        }
        if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
            mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
        }
        if (result.isSuccess()) {
            // synchronize children folders
            List<OCFile> children = synchFolderOp.getChildren();
            // beware of the 'hidden' recursion here!
            syncChildren(children);
        }
    } else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
        // in failures, the statistics for the global result are updated
        if (RemoteOperationResult.ResultCode.UNAUTHORIZED.equals(result.getCode())) {
            mSyncResult.stats.numAuthExceptions++;
        } else if (result.getException() instanceof DavException) {
            mSyncResult.stats.numParseExceptions++;
        } else if (result.getException() instanceof IOException) {
            mSyncResult.stats.numIoExceptions++;
        }
        mFailedResultsCounter++;
        mLastFailedResult = result;
    }
// else, ResultCode.FILE_NOT_FOUND is ignored, remote folder was
// removed from other thread or other client during the synchronization,
// before this thread fetched its contents
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) DavException(org.apache.jackrabbit.webdav.DavException) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) IOException(java.io.IOException)

Example 14 with RefreshFolderOperation

use of com.owncloud.android.operations.RefreshFolderOperation in project android by nextcloud.

the class DocumentsStorageProvider method createFile.

private String createFile(Document targetFolder, String displayName, String mimeType) throws FileNotFoundException {
    User user = targetFolder.getUser();
    // create dummy file
    File tempDir = new File(FileStorageUtils.getTemporalPath(user.getAccountName()));
    if (!tempDir.exists() && !tempDir.mkdirs()) {
        throw new FileNotFoundException("Temp folder could not be created: " + tempDir.getAbsolutePath());
    }
    File emptyFile = new File(tempDir, displayName);
    if (emptyFile.exists() && !emptyFile.delete()) {
        throw new FileNotFoundException("Previous file could not be deleted");
    }
    try {
        if (!emptyFile.createNewFile()) {
            throw new FileNotFoundException("File could not be created");
        }
    } catch (IOException e) {
        throw getFileNotFoundExceptionWithCause("File could not be created", e);
    }
    String newFilePath = targetFolder.getRemotePath() + displayName;
    // FIXME we need to update the mimeType somewhere else as well
    // perform the upload, no need for chunked operation as we have a empty file
    OwnCloudClient client = targetFolder.getClient();
    RemoteOperationResult result = new UploadFileRemoteOperation(emptyFile.getAbsolutePath(), newFilePath, mimeType, "", String.valueOf(System.currentTimeMillis() / 1000), FileUtil.getCreationTimestamp(emptyFile), false).execute(client);
    if (!result.isSuccess()) {
        Log_OC.e(TAG, result.toString());
        throw new FileNotFoundException("Failed to upload document with path " + newFilePath);
    }
    Context context = getNonNullContext();
    RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), false, false, true, targetFolder.getStorageManager(), user, context).execute(client);
    if (!updateParent.isSuccess()) {
        Log_OC.e(TAG, updateParent.toString());
        throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId());
    }
    Document newFile = new Document(targetFolder.getStorageManager(), newFilePath);
    context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
    return newFile.getDocumentId();
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 15 with RefreshFolderOperation

use of com.owncloud.android.operations.RefreshFolderOperation 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)

Aggregations

RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)19 OCFile (com.owncloud.android.datamodel.OCFile)8 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)8 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)5 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)5 File (java.io.File)5 Test (org.junit.Test)5 Context (android.content.Context)4 UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)4 ContentValues (android.content.ContentValues)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)3 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 AsyncTask (android.os.AsyncTask)2 User (com.nextcloud.client.account.User)2 CreateFolderRemoteOperation (com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation)2 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)2 SyncOperation (com.owncloud.android.operations.common.SyncOperation)2 IOException (java.io.IOException)2