Search in sources :

Example 1 with DownloadFileRemoteOperation

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

the class DownloadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // / perform the download
    synchronized (cancellationRequested) {
        if (cancellationRequested.get()) {
            return new RemoteOperationResult(new OperationCancelledException());
        }
    }
    RemoteOperationResult result;
    File newFile;
    boolean moved;
    // / download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());
    String tmpFolder = getTmpFolder();
    downloadOperation = new DownloadFileRemoteOperation(file.getRemotePath(), tmpFolder);
    Iterator<OnDatatransferProgressListener> listener = dataTransferListeners.iterator();
    while (listener.hasNext()) {
        downloadOperation.addDatatransferProgressListener(listener.next());
    }
    result = downloadOperation.execute(client);
    if (result.isSuccess()) {
        modificationTimestamp = downloadOperation.getModificationTimestamp();
        etag = downloadOperation.getEtag();
        newFile = new File(getSavePath());
        if (!newFile.getParentFile().exists() && !newFile.getParentFile().mkdirs()) {
            Log_OC.e(TAG, "Unable to create parent folder " + newFile.getParentFile().getAbsolutePath());
        }
        // decrypt file
        if (file.isEncrypted()) {
            FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.getContentResolver());
            OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath());
            DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, context, user.toPlatformAccount());
            if (metadata == null) {
                return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
            }
            byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getEncrypted().getKey());
            byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getInitializationVector());
            byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getAuthenticationTag());
            try {
                byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
                try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
                    fileOutputStream.write(decryptedBytes);
                }
            } catch (Exception e) {
                return new RemoteOperationResult(e);
            }
        }
        moved = tmpFile.renameTo(newFile);
        newFile.setLastModified(file.getModificationTimestamp());
        if (!moved) {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
        }
    }
    Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
    return result;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) DownloadFileRemoteOperation(com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) OCFile(com.owncloud.android.datamodel.OCFile) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileOutputStream(java.io.FileOutputStream) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Example 2 with DownloadFileRemoteOperation

use of com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation in project android-library by nextcloud.

the class MainActivity method startDownload.

private void startDownload() {
    File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
    downFolder.mkdir();
    File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
    File fileToUpload = upFolder.listFiles()[0];
    String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
    DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(remotePath, downFolder.getAbsolutePath());
    downloadOperation.addDatatransferProgressListener(this);
    downloadOperation.execute(mClient, this, mHandler);
}
Also used : DownloadFileRemoteOperation(com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File)

Aggregations

DownloadFileRemoteOperation (com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation)2 File (java.io.File)2 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 OCFile (com.owncloud.android.datamodel.OCFile)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 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)1 FileOutputStream (java.io.FileOutputStream)1