Search in sources :

Example 1 with StoreMetadataOperation

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

the class UploadFileOperation method encryptedUpload.

// gson cannot handle sparse arrays easily, therefore use hashmap
@SuppressLint("AndroidLintUseSparseArrays")
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
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 ****
         */
        // Lock folder
        LockFileOperation lockFileOperation = new LockFileOperation(parentFile.getLocalId());
        RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client, true);
        if (lockFileOperationResult.isSuccess()) {
            token = (String) lockFileOperationResult.getData().get(0);
            // immediately store it
            mUpload.setFolderUnlockToken(token);
            uploadsStorageManager.updateUpload(mUpload);
        } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            throw new Exception("Forbidden! Please try again later.)");
        } else {
            throw new Exception("Unknown error!");
        }
        // Update metadata
        GetMetadataOperation getMetadataOperation = new GetMetadataOperation(parentFile.getLocalId());
        RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client, true);
        DecryptedFolderMetadata metadata;
        if (getMetadataOperationResult.isSuccess()) {
            metadataExists = true;
            // decrypt metadata
            String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
            });
            metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
        } else if (getMetadataOperationResult.getHttpCode() == HttpStatus.SC_NOT_FOUND) {
            // new metadata
            metadata = new DecryptedFolderMetadata();
            metadata.setMetadata(new DecryptedFolderMetadata.Metadata());
            metadata.getMetadata().setMetadataKeys(new HashMap<>());
            String metadataKey = EncryptionUtils.encodeBytesToBase64String(EncryptionUtils.generateKey());
            String encryptedMetadataKey = EncryptionUtils.encryptStringAsymmetric(metadataKey, publicKey);
            metadata.getMetadata().getMetadataKeys().put(0, encryptedMetadataKey);
        } else {
            // TODO error
            throw new Exception("something wrong");
        }
        /**
         *** E2E ****
         */
        // check name collision
        checkNameCollision(client, metadata, parentFile.isEncrypted());
        String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
        expectedFile = new File(expectedPath);
        result = copyFile(originalFile, expectedPath);
        if (result != null) {
            return result;
        }
        // Get the last modification date of the file from the file system
        Long timeStampLong = originalFile.lastModified() / 1000;
        String timeStamp = timeStampLong.toString();
        /**
         *** 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("-", "");
        }
        mFile.setEncryptedFileName(encryptedFileName);
        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.getTemporalPath(mAccount.name) + mFile.getRemotePath();
            mFile.setStoragePath(temporalPath);
            temporalFile = new File(temporalPath);
            Files.deleteIfExists(Paths.get(temporalPath));
            result = copy(originalFile, temporalFile);
            if (result == null) {
                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 (mChunked && (size > ChunkedUploadRemoteFileOperation.CHUNK_SIZE)) {
            mUploadOperation = new ChunkedUploadRemoteFileOperation(mContext, encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
        } else {
            mUploadOperation = new UploadRemoteFileOperation(encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
        }
        Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
        while (listener.hasNext()) {
            mUploadOperation.addDatatransferProgressListener(listener.next());
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        result = mUploadOperation.execute(client, true);
        // location in the Nextcloud local folder
        if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
            result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }
        if (result.isSuccess()) {
            // upload metadata
            DecryptedFolderMetadata.DecryptedFile decryptedFile = new DecryptedFolderMetadata.DecryptedFile();
            DecryptedFolderMetadata.Data data = new DecryptedFolderMetadata.Data();
            data.setFilename(mFile.getFileName());
            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
            RemoteOperationResult uploadMetadataOperationResult;
            if (metadataExists) {
                // update metadata
                UpdateMetadataOperation storeMetadataOperation = new UpdateMetadataOperation(parentFile.getLocalId(), serializedFolderMetadata, token);
                uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
            } else {
                // store metadata
                StoreMetadataOperation storeMetadataOperation = new StoreMetadataOperation(parentFile.getLocalId(), serializedFolderMetadata);
                uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
            }
            if (!uploadMetadataOperationResult.isSuccess()) {
                throw new Exception();
            }
        }
    } 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);
        }
        if (result.isSuccess()) {
            Log_OC.i(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
        } else {
            if (result.getException() != null) {
                if (result.isCancelled()) {
                    Log_OC.w(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
                } else {
                    Log_OC.e(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage(), result.getException());
                }
            } else {
                Log_OC.e(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
            }
        }
    }
    if (result.isSuccess()) {
        handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
        RemoteOperationResult unlockFolderResult = unlockFolder(parentFile, client, token);
        if (!unlockFolderResult.isSuccess()) {
            return unlockFolderResult;
        }
    } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) FileNotFoundException(java.io.FileNotFoundException) OCUpload(com.owncloud.android.db.OCUpload) ChunkedUploadRemoteFileOperation(com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation) FileLock(java.nio.channels.FileLock) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) ChunkedUploadRemoteFileOperation(com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation) UploadRemoteFileOperation(com.owncloud.android.lib.resources.files.UploadRemoteFileOperation) OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileChannel(java.nio.channels.FileChannel) UpdateMetadataOperation(com.owncloud.android.lib.resources.files.UpdateMetadataOperation) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) StoreMetadataOperation(com.owncloud.android.lib.resources.files.StoreMetadataOperation) IOException(java.io.IOException) LockFileOperation(com.owncloud.android.lib.resources.files.LockFileOperation) GetMetadataOperation(com.owncloud.android.lib.resources.files.GetMetadataOperation) 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) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) RequiresApi(android.support.annotation.RequiresApi) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 RequiresApi (android.support.annotation.RequiresApi)1 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)1 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OCUpload (com.owncloud.android.db.OCUpload)1 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)1 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 ChunkedUploadRemoteFileOperation (com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation)1 GetMetadataOperation (com.owncloud.android.lib.resources.files.GetMetadataOperation)1 LockFileOperation (com.owncloud.android.lib.resources.files.LockFileOperation)1 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)1 StoreMetadataOperation (com.owncloud.android.lib.resources.files.StoreMetadataOperation)1 UpdateMetadataOperation (com.owncloud.android.lib.resources.files.UpdateMetadataOperation)1 UploadRemoteFileOperation (com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)1 EncryptionUtils (com.owncloud.android.utils.EncryptionUtils)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1