Search in sources :

Example 1 with ReadRemoteFileOperation

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

the class SynchronizeFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult<RemoteFile> 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.createOCFileFromRemoteFile(result.getData());
                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: syncing a not in-conflict file will clean wrong conflict markers in ancestors
            if (result.getCode() != ResultCode.SYNC_CONFLICT) {
                getStorageManager().saveConflict(mLocalFile, null);
            }
        }
    }
    Timber.i("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 2 with ReadRemoteFileOperation

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

the class UploadFileOperation method saveUploadedFile.

/**
 * Saves a OC File after a successful upload.
 * <p>
 * A PROPFIND is necessary to keep the props in the local database
 * synchronized with the server, specially the modification time and Etag
 * (where available)
 */
private void saveUploadedFile(OwnCloudClient client) {
    OCFile file = mFile;
    if (file.fileExists()) {
        file = getStorageManager().getFileById(file.getFileId());
    }
    long syncDate = System.currentTimeMillis();
    file.setLastSyncDateForData(syncDate);
    // new PROPFIND to keep data consistent with server
    // in theory, should return the same we already have
    // TODO from the appropriate OC server version, get data from last PUT response headers, instead
    // TODO     of a new PROPFIND; the latter may fail, specially for chunked uploads
    ReadRemoteFileOperation operation = new ReadRemoteFileOperation(getRemotePath());
    RemoteOperationResult<RemoteFile> result = operation.execute(client);
    if (result.isSuccess()) {
        updateOCFile(file, result.getData());
        file.setLastSyncDateForProperties(syncDate);
    } else {
        Timber.e("Error reading properties of file after successful upload; this is gonna hurt...");
    }
    if (mWasRenamed) {
        OCFile oldFile = getStorageManager().getFileByPath(mOldFile.getRemotePath());
        if (oldFile != null) {
            oldFile.setStoragePath(null);
            getStorageManager().saveFile(oldFile);
            getStorageManager().saveConflict(oldFile, null);
        }
    // else: it was just an automatic renaming due to a name
    // coincidence; nothing else is needed, the storagePath is right
    // in the instance returned by mCurrentUpload.getFile()
    }
    file.setNeedsUpdateThumbnail(true);
    getStorageManager().saveFile(file);
    getStorageManager().saveConflict(file, null);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Example 3 with ReadRemoteFileOperation

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

the class OCFileListAdapter method parseShares.

private void parseShares(ArrayList<Object> objects) {
    List<OCShare> shares = new ArrayList<>();
    for (int i = 0; i < objects.size(); i++) {
        // check type before cast as of long running data fetch it is possible that old result is filled
        if (objects.get(i) instanceof OCShare) {
            OCShare ocShare = (OCShare) objects.get(i);
            shares.add(ocShare);
            // get ocFile from Server to have an up-to-date copy
            ReadRemoteFileOperation operation = new ReadRemoteFileOperation(ocShare.getPath());
            RemoteOperationResult result = operation.execute(mAccount, mContext);
            if (result.isSuccess()) {
                OCFile file = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                searchForLocalFileInDefaultPath(file);
                file = mStorageManager.saveFileWithParent(file, mContext);
                ShareType newShareType = ocShare.getShareType();
                if (newShareType == ShareType.PUBLIC_LINK) {
                    file.setShareViaLink(true);
                } else if (newShareType == ShareType.USER || newShareType == ShareType.GROUP || newShareType == ShareType.EMAIL || newShareType == ShareType.FEDERATED) {
                    file.setShareWithSharee(true);
                }
                mStorageManager.saveFile(file);
                if (!mFiles.contains(file)) {
                    mFiles.add(file);
                }
            } else {
                Log_OC.e(TAG, "Error in getting prop for file: " + ocShare.getPath());
            }
        }
    }
    mStorageManager.saveShares(shares);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare) ArrayList(java.util.ArrayList) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) ShareType(com.owncloud.android.lib.resources.shares.ShareType)

Example 4 with ReadRemoteFileOperation

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

the class ActivitiesListActivity method onActivityClicked.

@Override
public void onActivityClicked(RichObject richObject) {
    String path = FileUtils.PATH_SEPARATOR + richObject.getPath();
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
            swipeListRefreshLayout.setVisibility(View.GONE);
            setLoadingMessage();
        }
    });
    updateTask = new AsyncTask<String, Object, OCFile>() {

        @Override
        protected OCFile doInBackground(String... path) {
            OCFile ocFile = null;
            // always update file as it could be an old state saved in database
            ReadRemoteFileOperation operation = new ReadRemoteFileOperation(path[0]);
            RemoteOperationResult resultRemoteFileOp = operation.execute(ownCloudClient);
            if (resultRemoteFileOp.isSuccess()) {
                OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) resultRemoteFileOp.getData().get(0));
                ocFile = getStorageManager().saveFileWithParent(temp, getBaseContext());
                if (ocFile.isFolder()) {
                    // perform folder synchronization
                    RemoteOperation synchFolderOp = new RefreshFolderOperation(ocFile, System.currentTimeMillis(), false, getFileOperationsHelper().isSharedSupported(), true, getStorageManager(), getAccount(), getApplicationContext());
                    synchFolderOp.execute(ownCloudClient);
                }
            }
            return ocFile;
        }

        @Override
        protected void onPostExecute(OCFile ocFile) {
            if (!isCancelled()) {
                if (ocFile == null) {
                    Toast.makeText(getBaseContext(), R.string.file_not_found, Toast.LENGTH_LONG).show();
                    swipeEmptyListRefreshLayout.setVisibility(View.GONE);
                    swipeListRefreshLayout.setVisibility(View.VISIBLE);
                    dismissLoadingDialog();
                } else {
                    Intent showDetailsIntent;
                    if (PreviewImageFragment.canBePreviewed(ocFile)) {
                        showDetailsIntent = new Intent(getBaseContext(), PreviewImageActivity.class);
                    } else {
                        showDetailsIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
                    }
                    showDetailsIntent.putExtra(EXTRA_FILE, ocFile);
                    showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
                    startActivity(showDetailsIntent);
                }
            }
        }
    };
    updateTask.execute(path);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) RichObject(com.owncloud.android.lib.resources.activities.models.RichObject) Intent(android.content.Intent) BindString(butterknife.BindString) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Aggregations

ReadRemoteFileOperation (com.owncloud.android.lib.resources.files.ReadRemoteFileOperation)4 OCFile (com.owncloud.android.datamodel.OCFile)3 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 Intent (android.content.Intent)1 BindString (butterknife.BindString)1 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)1 RichObject (com.owncloud.android.lib.resources.activities.models.RichObject)1 OCShare (com.owncloud.android.lib.resources.shares.OCShare)1 ShareType (com.owncloud.android.lib.resources.shares.ShareType)1 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)1 ArrayList (java.util.ArrayList)1