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;
}
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);
}
Aggregations