Search in sources :

Example 86 with OCFile

use of com.owncloud.android.datamodel.OCFile in project android by owncloud.

the class UploadFileOperation method grantFolderExistence.

/**
 * Checks the existence of the folder where the current file will be uploaded both
 * in the remote server and in the local database.
 * <p>
 * If the upload is set to enforce the creation of the folder, the method tries to
 * create it both remote and locally.
 *
 * @param pathToGrant Full remote path whose existence will be granted.
 * @return An {@link OCFile} instance corresponding to the folder where the file
 * will be uploaded.
 */
private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
    RemoteOperation checkPathExistenceOperation = new CheckPathExistenceRemoteOperation(pathToGrant, false);
    RemoteOperationResult result = checkPathExistenceOperation.execute(client);
    if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
        SyncOperation syncOp = new CreateFolderOperation(pathToGrant, true);
        result = syncOp.execute(client, getStorageManager());
    }
    if (result.isSuccess()) {
        OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
        if (parentDir == null) {
            parentDir = createLocalFolder(pathToGrant);
        }
        if (parentDir != null) {
            result = new RemoteOperationResult(ResultCode.OK);
        } else {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) CheckPathExistenceRemoteOperation(com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) CheckPathExistenceRemoteOperation(com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) SyncOperation(com.owncloud.android.operations.common.SyncOperation)

Example 87 with OCFile

use of com.owncloud.android.datamodel.OCFile 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 88 with OCFile

use of com.owncloud.android.datamodel.OCFile in project android by owncloud.

the class UploadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    mCancellationRequested.set(false);
    mUploadStarted.set(true);
    RemoteOperationResult result = null;
    File temporalFile = null, originalFile = new File(mOriginalStoragePath), expectedFile = null;
    try {
        // / check if the file continues existing before schedule the operation
        if (!originalFile.exists()) {
            Timber.d("%s not exists anymore", mOriginalStoragePath);
            return new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
        }
        // / check the existence of the parent folder for the file to upload
        String remoteParentPath = new File(getRemotePath()).getParent();
        remoteParentPath = remoteParentPath.endsWith(File.separator) ? remoteParentPath : remoteParentPath + File.separator;
        result = grantFolderExistence(remoteParentPath, client);
        if (!result.isSuccess()) {
            return result;
        }
        // / set parent local id in uploading file
        OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
        mFile.setParentId(parent.getFileId());
        // / automatic rename of file to upload in case of name collision in server
        Timber.d("Checking name collision in server");
        if (!mForceOverwrite) {
            String remotePath = RemoteFileUtils.Companion.getAvailableRemotePath(client, mRemotePath);
            mWasRenamed = !mRemotePath.equals(remotePath);
            if (mWasRenamed) {
                createNewOCFile(remotePath);
                Timber.d("File renamed as %s", remotePath);
            }
            mRemotePath = remotePath;
            mRenameUploadListener.onRenameUpload();
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
        expectedFile = new File(expectedPath);
        // / copy the file locally before uploading
        if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY && !mOriginalStoragePath.equals(expectedPath)) {
            String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
            mFile.setStoragePath(temporalPath);
            temporalFile = new File(temporalPath);
            result = copy(originalFile, temporalFile);
            if (result != null) {
                return result;
            }
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        // Get the last modification date of the file from the file system
        Long timeStampLong = originalFile.lastModified() / 1000;
        String timeStamp = timeStampLong.toString();
        // Perform the upload
        result = uploadRemoteFile(client, temporalFile, originalFile, expectedPath, expectedFile, timeStamp);
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        mUploadStarted.set(false);
        if (temporalFile != null && !originalFile.equals(temporalFile)) {
            temporalFile.delete();
        }
        if (result == null) {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
        if (result.isSuccess()) {
            Timber.i("Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
        } else {
            if (result.getException() != null) {
                if (result.isCancelled()) {
                    Timber.w("Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
                } else {
                    Timber.e(result.getException(), "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
                }
            } else {
                Timber.e("Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
            }
        }
    }
    if (result.isSuccess()) {
        saveUploadedFile(client);
    } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException)

Example 89 with OCFile

use of com.owncloud.android.datamodel.OCFile in project android by owncloud.

the class RetryDownloadJobService method onStartJob.

@Override
public boolean onStartJob(JobParameters jobParameters) {
    String accountName = jobParameters.getExtras().getString(Extras.EXTRA_ACCOUNT_NAME);
    Account account = AccountUtils.getOwnCloudAccountByName(this, accountName);
    // retrying the download
    if (account != null) {
        FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(this, account, getContentResolver());
        String fileRemotePath = jobParameters.getExtras().getString(Extras.EXTRA_REMOTE_PATH);
        Timber.d("Retrying download of %1s in %2s", fileRemotePath, accountName);
        // Get download file from database
        OCFile ocFile = fileDataStorageManager.getFileByPath(fileRemotePath);
        if (ocFile != null) {
            // Retry download
            Intent intent = new Intent(this, FileDownloader.class);
            intent.putExtra(FileDownloader.KEY_ACCOUNT, account);
            intent.putExtra(FileDownloader.KEY_FILE, ocFile);
            intent.putExtra(FileDownloader.KEY_RETRY_DOWNLOAD, true);
            Timber.d("Retry download from foreground/background, startForeground() will be called soon");
            ContextCompat.startForegroundService(this, intent);
        } else {
            Timber.w("File %1s in %2s not found in database", fileRemotePath, accountName);
        }
    } else {
        Timber.w("Account %1s was deleted, no retry will be done", accountName);
    }
    // done here, real job was delegated to another castle
    jobFinished(jobParameters, false);
    // TODO or false? what is the real effect, Google!?!?!?!?
    return true;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) Account(android.accounts.Account) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Intent(android.content.Intent)

Example 90 with OCFile

use of com.owncloud.android.datamodel.OCFile in project android by owncloud.

the class RetryUploadJobService method onStartJob.

@Override
public boolean onStartJob(JobParameters jobParameters) {
    UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(getContentResolver());
    String fileRemotePath = jobParameters.getExtras().getString(Extras.EXTRA_REMOTE_PATH);
    String accountName = jobParameters.getExtras().getString(Extras.EXTRA_ACCOUNT_NAME);
    Timber.d("Retrying upload of %1s in %2s", fileRemotePath, accountName);
    // Get upload to be retried
    OCUpload ocUpload = uploadsStorageManager.getLastUploadFor(new OCFile(fileRemotePath), accountName);
    if (ocUpload != null) {
        // Retry the upload
        TransferRequester requester = new TransferRequester();
        requester.retry(this, ocUpload, true);
    } else {
        // easy if the user deletes the upload in uploads view before recovering network
        Timber.w("No upload found in database for %1s in %2s", fileRemotePath, accountName);
    }
    // done here, real job was delegated to another castle
    jobFinished(jobParameters, false);
    // TODO or false? what is the real effect, Google!?!?!?!?
    return true;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.datamodel.OCUpload) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager)

Aggregations

OCFile (com.owncloud.android.datamodel.OCFile)307 File (java.io.File)56 Test (org.junit.Test)44 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)43 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)40 ArrayList (java.util.ArrayList)28 Intent (android.content.Intent)27 OCFileListFragment (com.owncloud.android.ui.fragment.OCFileListFragment)22 OCUpload (com.owncloud.android.db.OCUpload)20 ScreenshotTest (com.owncloud.android.utils.ScreenshotTest)20 FileFragment (com.owncloud.android.ui.fragment.FileFragment)19 User (com.nextcloud.client.account.User)17 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)16 Bundle (android.os.Bundle)13 Fragment (androidx.fragment.app.Fragment)12 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)12 FileDetailFragment (com.owncloud.android.ui.fragment.FileDetailFragment)12 Account (android.accounts.Account)11 SuppressLint (android.annotation.SuppressLint)11 PreviewTextFragment (com.owncloud.android.ui.preview.PreviewTextFragment)11