Search in sources :

Example 1 with ReadFileRemoteOperation

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

the class FileDataStorageManager method saveFileWithParent.

/**
 * traverses a files parent tree to be able to store a file with its parents. Throws a
 * RemoteOperationFailedException in case the parent can't be retrieved.
 *
 * @param ocFile  the file
 * @param context the app context
 * @return the parent file
 */
public OCFile saveFileWithParent(OCFile ocFile, Context context) {
    if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) {
        String remotePath = ocFile.getRemotePath();
        String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName()));
        OCFile parentFile = getFileByPath(parentPath);
        OCFile returnFile;
        if (parentFile == null) {
            // remote request
            ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath);
            // TODO Deprecated
            RemoteOperationResult result = operation.execute(getUser().toPlatformAccount(), context);
            if (result.isSuccess()) {
                OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                returnFile = saveFileWithParent(remoteFolder, context);
            } else {
                Exception exception = result.getException();
                String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / " + result.getLogMessage();
                if (exception != null) {
                    throw new RemoteOperationFailedException(message, exception);
                } else {
                    throw new RemoteOperationFailedException(message);
                }
            }
        } else {
            returnFile = saveFileWithParent(parentFile, context);
        }
        ocFile.setParentId(returnFile.getFileId());
        saveFile(ocFile);
    }
    return ocFile;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RemoteOperationFailedException(com.owncloud.android.operations.RemoteOperationFailedException) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RemoteException(android.os.RemoteException) JsonSyntaxException(com.google.gson.JsonSyntaxException) RemoteOperationFailedException(com.owncloud.android.operations.RemoteOperationFailedException) OperationApplicationException(android.content.OperationApplicationException)

Example 2 with ReadFileRemoteOperation

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

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());
    }
    if (file == null) {
        // this can happen e.g. when the file gets deleted during upload
        return;
    }
    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
    String path;
    if (encryptedAncestor) {
        path = file.getParentRemotePath() + mFile.getEncryptedFileName();
    } else {
        path = getRemotePath();
    }
    ReadFileRemoteOperation operation = new ReadFileRemoteOperation(path);
    RemoteOperationResult result = operation.execute(client);
    if (result.isSuccess()) {
        updateOCFile(file, (RemoteFile) result.getData().get(0));
        file.setLastSyncDateForProperties(syncDate);
    } else {
        Log_OC.e(TAG, "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.setUpdateThumbnailNeeded(true);
    getStorageManager().saveFile(file);
    getStorageManager().saveConflict(file, null);
    if (MimeTypeUtil.isMedia(file.getMimeType())) {
        FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
    }
    // generate new Thumbnail
    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), user);
    task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)

Example 3 with ReadFileRemoteOperation

use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation 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 ReadFileRemoteOperation

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

the class EndToEndRandomIT method uploadFile.

private void uploadFile(int i) throws IOException {
    String fileName = RandomString.make(5) + ".txt";
    File file;
    if (new Random().nextBoolean()) {
        file = createFile(fileName, new Random().nextInt(50000));
    } else {
        file = createFile(fileName, 500000 + new Random().nextInt(50000));
    }
    String remotePath = currentFolder.getRemotePath() + fileName;
    Log_OC.d(this, "[" + i + "/" + actionCount + "] " + "Upload file to: " + currentFolder.getDecryptedRemotePath() + fileName);
    OCUpload ocUpload = new OCUpload(file.getAbsolutePath(), remotePath, account.name);
    uploadOCUpload(ocUpload);
    shortSleep();
    OCFile parentFolder = getStorageManager().getFileByEncryptedRemotePath(new File(ocUpload.getRemotePath()).getParent() + "/");
    String uploadedFileName = new File(ocUpload.getRemotePath()).getName();
    String decryptedPath = parentFolder.getDecryptedRemotePath() + uploadedFileName;
    OCFile uploadedFile = getStorageManager().getFileByDecryptedRemotePath(decryptedPath);
    verifyStoragePath(uploadedFile);
    // verify storage path
    refreshFolder(currentFolder.getRemotePath());
    uploadedFile = getStorageManager().getFileByDecryptedRemotePath(decryptedPath);
    verifyStoragePath(uploadedFile);
    // verify that encrypted file is on server
    assertTrue(new ReadFileRemoteOperation(currentFolder.getRemotePath() + uploadedFile.getEncryptedFileName()).execute(client).isSuccess());
    // verify that unencrypted file is not on server
    assertFalse(new ReadFileRemoteOperation(currentFolder.getDecryptedRemotePath() + fileName).execute(client).isSuccess());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) Random(java.util.Random) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RandomString(net.bytebuddy.utility.RandomString) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 5 with ReadFileRemoteOperation

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

the class ConflictsResolveActivity method onStart.

@Override
protected void onStart() {
    super.onStart();
    if (getAccount() == null) {
        finish();
        return;
    }
    if (newFile == null) {
        Log_OC.e(TAG, "No file received");
        finish();
        return;
    }
    if (existingFile == null) {
        // fetch info of existing file from server
        ReadFileRemoteOperation operation = new ReadFileRemoteOperation(newFile.getRemotePath());
        new Thread(() -> {
            try {
                RemoteOperationResult result = operation.execute(getAccount(), this);
                if (result.isSuccess()) {
                    existingFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                    existingFile.setLastSyncDateForProperties(System.currentTimeMillis());
                    startDialog();
                } else {
                    Log_OC.e(TAG, "ReadFileRemoteOp returned failure with code: " + result.getHttpCode());
                    showErrorAndFinish();
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "Error when trying to fetch remote file", e);
                showErrorAndFinish();
            }
        }).start();
    } else {
        startDialog();
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)

Aggregations

ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)10 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)9 OCFile (com.owncloud.android.datamodel.OCFile)7 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)3 Activity (android.app.Activity)1 OperationApplicationException (android.content.OperationApplicationException)1 Bitmap (android.graphics.Bitmap)1 RemoteException (android.os.RemoteException)1 Fragment (androidx.fragment.app.Fragment)1 FragmentTransaction (androidx.fragment.app.FragmentTransaction)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 User (com.nextcloud.client.account.User)1 ShareActivityBinding (com.owncloud.android.databinding.ShareActivityBinding)1 ThumbnailsCacheManager (com.owncloud.android.datamodel.ThumbnailsCacheManager)1 OCUpload (com.owncloud.android.db.OCUpload)1 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)1 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)1 SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)1 OCShare (com.owncloud.android.lib.resources.shares.OCShare)1 ShareType (com.owncloud.android.lib.resources.shares.ShareType)1