Search in sources :

Example 1 with RemoteFile

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

the class FileStorageUtils method fillRemoteFile.

/**
     * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
     * 
     * @param ocFile    OCFile
     * @return          New RemoteFile instance representing the resource described by ocFile.
     */
public static RemoteFile fillRemoteFile(OCFile ocFile) {
    RemoteFile file = new RemoteFile(ocFile.getRemotePath());
    file.setCreationTimestamp(ocFile.getCreationTimestamp());
    file.setLength(ocFile.getFileLength());
    file.setMimeType(ocFile.getMimetype());
    file.setModifiedTimestamp(ocFile.getModificationTimestamp());
    file.setEtag(ocFile.getEtag());
    file.setPermissions(ocFile.getPermissions());
    file.setRemoteId(ocFile.getRemoteId());
    return file;
}
Also used : RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Example 2 with RemoteFile

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

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 {
        if (mServerFile == null && !mPushOnly) {
            ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
            result = operation.execute(client);
            if (result.isSuccess()) {
                mServerFile = FileStorageUtils.createOCFileFrom((RemoteFile) result.getData().get(0));
                mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
            }
        }
        if (mPushOnly || mServerFile != null) {
            // at this point, conditions should be exclusive
            /// decide if file changed in the server
            boolean serverChanged;
            if (mPushOnly) {
                serverChanged = false;
            } else if (mLocalFile.getEtag() == null || mLocalFile.getEtag().length() == 0) {
                // file uploaded (null) or downloaded ("")
                // before upgrade to version 1.8.0; this is legacy condition
                serverChanged = mServerFile.getModificationTimestamp() != mLocalFile.getModificationTimestampAtLastSyncForData();
            } else {
                serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag()));
            }
            /// decide if file changed in local device
            boolean localChanged = (mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData());
            /// decide action to perform depending upon changes
            if (localChanged && serverChanged) {
                result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
                getStorageManager().saveConflict(mLocalFile, mServerFile.getEtag());
            } else if (localChanged) {
                if (mPushOnly) {
                    // prevent accidental override of unnoticed change in server;
                    // dirty trick, more refactoring is needed, but not today;
                    // works due to {@link UploadFileOperation#L364,L367}
                    mLocalFile.setEtagInConflict(mLocalFile.getEtag());
                }
                requestForUpload(mLocalFile);
                result = new RemoteOperationResult(ResultCode.OK);
            } else if (serverChanged) {
                mLocalFile.setRemoteId(mServerFile.getRemoteId());
                requestForDownload(mLocalFile);
                // mLocalFile, not mServerFile; we want to keep the value of
                // available-offline property
                // the update of local data will be done later by the FileUploader
                // service when the upload finishes
                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);
            }
        }
    }
    Log_OC.i(TAG, "Synchronizing " + mAccount.name + ", file " + mLocalFile.getRemotePath() + ": " + result.getLogMessage());
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Example 3 with RemoteFile

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

the class SynchronizeFolderOperation method mergeRemoteFolder.

/**
     *  Synchronizes the data retrieved from the server about the contents of the target folder
     *  with the current data in the local database.
     *
     *  Grants that mFoldersToVisit is updated with fresh data after execution.
     *
     *  @param folderAndFiles   Remote folder and children files in folder
     */
private void mergeRemoteFolder(ArrayList<Object> folderAndFiles) throws OperationCancelledException {
    Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
    FileDataStorageManager storageManager = getStorageManager();
    // parse data from remote folder
    OCFile updatedFolder = FileStorageUtils.createOCFileFrom((RemoteFile) folderAndFiles.get(0));
    // NOTE: updates ETag with remote value; that's INTENDED
    updatedFolder.copyLocalPropertiesFrom(mLocalFolder);
    Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
    List<OCFile> updatedFiles = new Vector<>(folderAndFiles.size() - 1);
    mFoldersToVisit = new Vector<>(folderAndFiles.size() - 1);
    mFilesToSyncContents.clear();
    if (mCancellationRequested.get()) {
        throw new OperationCancelledException();
    }
    // get current data about local contents of the folder to synchronize
    List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
    Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
    for (OCFile file : localFiles) {
        localFilesMap.put(file.getRemotePath(), file);
    }
    // loop to synchronize every child
    OCFile remoteFile, localFile, updatedLocalFile;
    RemoteFile r;
    int foldersToExpand = 0;
    for (int i = 1; i < folderAndFiles.size(); i++) {
        /// new OCFile instance with the data from the server
        r = (RemoteFile) folderAndFiles.get(i);
        remoteFile = FileStorageUtils.createOCFileFrom(r);
        /// new OCFile instance to merge fresh data from server with local state
        updatedLocalFile = FileStorageUtils.createOCFileFrom(r);
        /// retrieve local data for the read file
        localFile = localFilesMap.remove(remoteFile.getRemotePath());
        /// add to updatedFile data about LOCAL STATE (not existing in server)
        updatedLocalFile.setLastSyncDateForProperties(mCurrentSyncTime);
        if (localFile != null) {
            updatedLocalFile.copyLocalPropertiesFrom(localFile);
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag(localFile.getEtag());
            if (!updatedLocalFile.isFolder() && remoteFile.isImage() && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
                updatedLocalFile.setNeedsUpdateThumbnail(true);
            }
        } else {
            updatedLocalFile.setParentId(mLocalFolder.getFileId());
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag("");
            // new files need to check av-off status of parent folder!
            if (updatedFolder.isAvailableOffline()) {
                updatedLocalFile.setAvailableOfflineStatus(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT);
            }
        }
        /// check and fix, if needed, local storage path
        searchForLocalFileInDefaultPath(updatedLocalFile);
        /// prepare content synchronizations
        boolean serverUnchanged = addToSyncContents(updatedLocalFile, remoteFile);
        if (updatedLocalFile.isFolder() && !serverUnchanged) {
            foldersToExpand++;
        }
        updatedFiles.add(updatedLocalFile);
    }
    // save updated contents in local database
    if (foldersToExpand == 0) {
        updatedFolder.setTreeEtag(updatedFolder.getEtag());
    // TODO - propagate up
    }
    storageManager.saveFolder(updatedFolder, updatedFiles, localFilesMap.values());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) HashMap(java.util.HashMap) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Vector(java.util.Vector) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Aggregations

RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 ReadRemoteFileOperation (com.owncloud.android.lib.resources.files.ReadRemoteFileOperation)1 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1