Search in sources :

Example 16 with OCUpload

use of com.owncloud.android.db.OCUpload in project android by nextcloud.

the class UploadFileOperation method encryptedUpload.

// gson cannot handle sparse arrays easily, therefore use hashmap
@SuppressLint("AndroidLintUseSparseArrays")
private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile parentFile) {
    RemoteOperationResult result = null;
    File temporalFile = null;
    File originalFile = new File(mOriginalStoragePath);
    File expectedFile = null;
    FileLock fileLock = null;
    long size;
    boolean metadataExists = false;
    String token = null;
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver());
    String privateKey = arbitraryDataProvider.getValue(getAccount().name, EncryptionUtils.PRIVATE_KEY);
    String publicKey = arbitraryDataProvider.getValue(getAccount().name, EncryptionUtils.PUBLIC_KEY);
    try {
        // check conditions
        result = checkConditions(originalFile);
        if (result != null) {
            return result;
        }
        /**
         *** E2E ****
         */
        token = EncryptionUtils.lockFolder(parentFile, client);
        // immediately store it
        mUpload.setFolderUnlockToken(token);
        uploadsStorageManager.updateUpload(mUpload);
        // Update metadata
        Pair<Boolean, DecryptedFolderMetadata> metadataPair = EncryptionUtils.retrieveMetadata(parentFile, client, privateKey, publicKey);
        metadataExists = metadataPair.first;
        DecryptedFolderMetadata metadata = metadataPair.second;
        /**
         ** E2E ****
         */
        // check name collision
        RemoteOperationResult collisionResult = checkNameCollision(client, metadata, parentFile.isEncrypted());
        if (collisionResult != null) {
            result = collisionResult;
            return collisionResult;
        }
        mFile.setDecryptedRemotePath(parentFile.getDecryptedRemotePath() + originalFile.getName());
        String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
        expectedFile = new File(expectedPath);
        result = copyFile(originalFile, expectedPath);
        if (!result.isSuccess()) {
            return result;
        }
        // Get the last modification date of the file from the file system
        String lastModifiedTimestamp = Long.toString(originalFile.lastModified() / 1000);
        Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
        /**
         *** E2E ****
         */
        // Key, always generate new one
        byte[] key = EncryptionUtils.generateKey();
        // IV, always generate new one
        byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength);
        EncryptionUtils.EncryptedFile encryptedFile = EncryptionUtils.encryptFile(mFile, key, iv);
        // new random file name, check if it exists in metadata
        String encryptedFileName = UUID.randomUUID().toString().replaceAll("-", "");
        while (metadata.getFiles().get(encryptedFileName) != null) {
            encryptedFileName = UUID.randomUUID().toString().replaceAll("-", "");
        }
        File encryptedTempFile = File.createTempFile("encFile", encryptedFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(encryptedTempFile);
        fileOutputStream.write(encryptedFile.encryptedBytes);
        fileOutputStream.close();
        /**
         *** E2E ****
         */
        FileChannel channel = null;
        try {
            channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
            fileLock = channel.tryLock();
        } catch (FileNotFoundException e) {
            // this basically means that the file is on SD card
            // try to copy file to temporary dir if it doesn't exist
            String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) + mFile.getRemotePath();
            mFile.setStoragePath(temporalPath);
            temporalFile = new File(temporalPath);
            Files.deleteIfExists(Paths.get(temporalPath));
            result = copy(originalFile, temporalFile);
            if (result.isSuccess()) {
                if (temporalFile.length() == originalFile.length()) {
                    channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
                    fileLock = channel.tryLock();
                } else {
                    result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
                }
            }
        }
        try {
            size = channel.size();
        } catch (IOException e1) {
            size = new File(mFile.getStoragePath()).length();
        }
        for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
            if (ocUpload.getUploadId() == getOCUploadId()) {
                ocUpload.setFileSize(size);
                uploadsStorageManager.updateUpload(ocUpload);
                break;
            }
        }
        // / perform the upload
        if (size > ChunkedFileUploadRemoteOperation.CHUNK_SIZE_MOBILE) {
            boolean onWifiConnection = connectivityService.getConnectivity().isWifi();
            mUploadOperation = new ChunkedFileUploadRemoteOperation(encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimeType(), mFile.getEtagInConflict(), lastModifiedTimestamp, onWifiConnection, token, creationTimestamp, mDisableRetries);
        } else {
            mUploadOperation = new UploadFileRemoteOperation(encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimeType(), mFile.getEtagInConflict(), lastModifiedTimestamp, creationTimestamp, token, mDisableRetries);
        }
        for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners) {
            mUploadOperation.addDataTransferProgressListener(mDataTransferListener);
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        result = mUploadOperation.execute(client);
        // location in the Nextcloud local folder
        if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
            result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }
        if (result.isSuccess()) {
            mFile.setDecryptedRemotePath(parentFile.getDecryptedRemotePath() + originalFile.getName());
            mFile.setRemotePath(parentFile.getRemotePath() + encryptedFileName);
            // update metadata
            DecryptedFolderMetadata.DecryptedFile decryptedFile = new DecryptedFolderMetadata.DecryptedFile();
            DecryptedFolderMetadata.Data data = new DecryptedFolderMetadata.Data();
            data.setFilename(mFile.getDecryptedFileName());
            data.setMimetype(mFile.getMimeType());
            data.setKey(EncryptionUtils.encodeBytesToBase64String(key));
            decryptedFile.setEncrypted(data);
            decryptedFile.setInitializationVector(EncryptionUtils.encodeBytesToBase64String(iv));
            decryptedFile.setAuthenticationTag(encryptedFile.authenticationTag);
            metadata.getFiles().put(encryptedFileName, decryptedFile);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
            String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
            // upload metadata
            EncryptionUtils.uploadMetadata(parentFile, serializedFolderMetadata, token, client, metadataExists);
            // unlock
            result = EncryptionUtils.unlockFolder(parentFile, client, token);
            if (result.isSuccess()) {
                token = null;
            }
        }
    } catch (FileNotFoundException e) {
        Log_OC.d(TAG, mFile.getStoragePath() + " not exists anymore");
        result = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
    } catch (OverlappingFileLockException e) {
        Log_OC.d(TAG, "Overlapping file lock exception");
        result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        mUploadStarted.set(false);
        if (fileLock != null) {
            try {
                fileLock.release();
            } catch (IOException e) {
                Log_OC.e(TAG, "Failed to unlock file with path " + mFile.getStoragePath());
            }
        }
        if (temporalFile != null && !originalFile.equals(temporalFile)) {
            temporalFile.delete();
        }
        if (result == null) {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
        logResult(result, mFile.getStoragePath(), mFile.getRemotePath());
    }
    if (result.isSuccess()) {
        handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
    } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
    }
    // unlock must be done always
    if (token != null) {
        RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parentFile, client, token);
        if (!unlockFolderResult.isSuccess()) {
            return unlockFolderResult;
        }
    }
    // delete temporal file
    if (temporalFile != null && temporalFile.exists() && !temporalFile.delete()) {
        Log_OC.e(TAG, "Could not delete temporal file " + temporalFile.getAbsolutePath());
    }
    return result;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ChunkedFileUploadRemoteOperation(com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation) OCUpload(com.owncloud.android.db.OCUpload) FileLock(java.nio.channels.FileLock) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileChannel(java.nio.channels.FileChannel) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileNotFoundException(java.io.FileNotFoundException) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException) EncryptionUtils(com.owncloud.android.utils.EncryptionUtils) RandomAccessFile(java.io.RandomAccessFile) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) SuppressLint(android.annotation.SuppressLint)

Example 17 with OCUpload

use of com.owncloud.android.db.OCUpload in project android by nextcloud.

the class UploadFileOperation method run.

@Override
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
protected RemoteOperationResult run(OwnCloudClient client) {
    mCancellationRequested.set(false);
    mUploadStarted.set(true);
    for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
        if (ocUpload.getUploadId() == getOCUploadId()) {
            ocUpload.setFileSize(0);
            uploadsStorageManager.updateUpload(ocUpload);
            break;
        }
    }
    String remoteParentPath = new File(getRemotePath()).getParent();
    remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ? remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
    OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
    // in case of a fresh upload with subfolder, where parent does not exist yet
    if (parent == null && (mFolderUnlockToken == null || mFolderUnlockToken.isEmpty())) {
        // try to create folder
        RemoteOperationResult result = grantFolderExistence(remoteParentPath, client);
        if (!result.isSuccess()) {
            return result;
        }
        parent = getStorageManager().getFileByPath(remoteParentPath);
        if (parent == null) {
            return new RemoteOperationResult(false, "Parent folder not found", HttpStatus.SC_NOT_FOUND);
        }
    }
    // parent file is not null anymore:
    // - it was created on fresh upload or
    // - resume of encrypted upload, then parent file exists already as unlock is only for direct parent
    mFile.setParentId(parent.getFileId());
    // the parent folder should exist as it is a resume of a broken upload
    if (mFolderUnlockToken != null && !mFolderUnlockToken.isEmpty()) {
        UnlockFileRemoteOperation unlockFileOperation = new UnlockFileRemoteOperation(parent.getLocalId(), mFolderUnlockToken);
        RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client);
        if (!unlockFileOperationResult.isSuccess()) {
            return unlockFileOperationResult;
        }
    }
    // check if any parent is encrypted
    encryptedAncestor = FileStorageUtils.checkEncryptionStatus(parent, getStorageManager());
    mFile.setEncrypted(encryptedAncestor);
    if (encryptedAncestor) {
        Log_OC.d(TAG, "encrypted upload");
        return encryptedUpload(client, parent);
    } else {
        Log_OC.d(TAG, "normal upload");
        return normalUpload(client);
    }
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RandomAccessFile(java.io.RandomAccessFile) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation)

Example 18 with OCUpload

use of com.owncloud.android.db.OCUpload in project android by nextcloud.

the class FileUploader method retryFailedUploads.

/**
 * Retry a subset of all the stored failed uploads.
 */
public static void retryFailedUploads(@NonNull final Context context, @NonNull final UploadsStorageManager uploadsStorageManager, @NonNull final ConnectivityService connectivityService, @NonNull final UserAccountManager accountManager, @NonNull final PowerManagementService powerManagementService) {
    OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
    if (failedUploads == null || failedUploads.length == 0) {
        // nothing to do
        return;
    }
    final Connectivity connectivity = connectivityService.getConnectivity();
    final boolean gotNetwork = connectivity.isConnected() && !connectivityService.isInternetWalled();
    final boolean gotWifi = connectivity.isWifi();
    final BatteryStatus batteryStatus = powerManagementService.getBattery();
    final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
    final boolean isPowerSaving = powerManagementService.isPowerSavingEnabled();
    Optional<User> uploadUser = Optional.empty();
    for (OCUpload failedUpload : failedUploads) {
        // 1. extract failed upload owner account and cache it between loops (expensive query)
        if (!uploadUser.isPresent() || !uploadUser.get().nameEquals(failedUpload.getAccountName())) {
            uploadUser = accountManager.getUser(failedUpload.getAccountName());
        }
        final boolean isDeleted = !new File(failedUpload.getLocalPath()).exists();
        if (isDeleted) {
            // 2A. for deleted files, mark as permanently failed
            if (failedUpload.getLastResult() != UploadResult.FILE_NOT_FOUND) {
                failedUpload.setLastResult(UploadResult.FILE_NOT_FOUND);
                uploadsStorageManager.updateUpload(failedUpload);
            }
        } else if (!isPowerSaving && gotNetwork && canUploadBeRetried(failedUpload, gotWifi, charging)) {
            // 2B. for existing local files, try restarting it if possible
            retryUpload(context, uploadUser.get(), failedUpload);
        }
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) User(com.nextcloud.client.account.User) BatteryStatus(com.nextcloud.client.device.BatteryStatus) Connectivity(com.nextcloud.client.network.Connectivity) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 19 with OCUpload

use of com.owncloud.android.db.OCUpload in project android by nextcloud.

the class FileUploader method startNewUpload.

/**
 * Start a new {@link UploadFileOperation}.
 */
@SuppressLint("SdCardPath")
private void startNewUpload(User user, List<String> requestedUploads, boolean onWifiOnly, boolean whileChargingOnly, NameCollisionPolicy nameCollisionPolicy, int localAction, boolean isCreateRemoteFolder, int createdBy, OCFile file, boolean disableRetries) {
    if (file.getStoragePath().startsWith("/data/data/")) {
        Log_OC.d(TAG, "Upload from sensitive path is not allowed");
        return;
    }
    OCUpload ocUpload = new OCUpload(file, user);
    ocUpload.setFileSize(file.getFileLength());
    ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
    ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
    ocUpload.setCreatedBy(createdBy);
    ocUpload.setLocalAction(localAction);
    ocUpload.setUseWifiOnly(onWifiOnly);
    ocUpload.setWhileChargingOnly(whileChargingOnly);
    ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
    UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, file, ocUpload, nameCollisionPolicy, localAction, this, onWifiOnly, whileChargingOnly, disableRetries, new FileDataStorageManager(user, getContentResolver()));
    newUpload.setCreatedBy(createdBy);
    if (isCreateRemoteFolder) {
        newUpload.setRemoteFolderToBeCreated();
    }
    newUpload.addDataTransferProgressListener(this);
    newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
    newUpload.addRenameUploadListener(this);
    Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), file.getRemotePath(), newUpload);
    if (putResult != null) {
        requestedUploads.add(putResult.first);
        // Save upload in database
        long id = mUploadsStorageManager.storeUpload(ocUpload);
        newUpload.setOCUploadId(id);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) SuppressLint(android.annotation.SuppressLint)

Example 20 with OCUpload

use of com.owncloud.android.db.OCUpload in project android by nextcloud.

the class FileUploader method retryUploads.

/**
 * Retries a list of uploads.
 */
private void retryUploads(Intent intent, User user, List<String> requestedUploads) {
    boolean onWifiOnly;
    boolean whileChargingOnly;
    OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
    onWifiOnly = upload.isUseWifiOnly();
    whileChargingOnly = upload.isWhileChargingOnly();
    UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, null, upload, upload.getNameCollisionPolicy(), upload.getLocalAction(), this, onWifiOnly, whileChargingOnly, true, new FileDataStorageManager(user, getContentResolver()));
    newUpload.addDataTransferProgressListener(this);
    newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
    newUpload.addRenameUploadListener(this);
    Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), upload.getRemotePath(), newUpload);
    if (putResult != null) {
        String uploadKey = putResult.first;
        requestedUploads.add(uploadKey);
        // Update upload in database
        upload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
        mUploadsStorageManager.updateUpload(upload);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Aggregations

OCUpload (com.owncloud.android.db.OCUpload)49 Test (org.junit.Test)29 OCFile (com.owncloud.android.datamodel.OCFile)22 File (java.io.File)15 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)8 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)8 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)7 Intent (android.content.Intent)6 SmallTest (androidx.test.filters.SmallTest)5 ScreenshotTest (com.owncloud.android.utils.ScreenshotTest)4 Account (android.accounts.Account)3 Context (android.content.Context)3 Cursor (android.database.Cursor)3 DialogFragment (androidx.fragment.app.DialogFragment)3 BatteryStatus (com.nextcloud.client.device.BatteryStatus)3 Connectivity (com.nextcloud.client.network.Connectivity)3 ConnectivityService (com.nextcloud.client.network.ConnectivityService)3 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)3 RandomAccessFile (java.io.RandomAccessFile)3 SuppressLint (android.annotation.SuppressLint)2