use of org.syncany.operations.daemon.messages.DownDownloadFileSyncExternalEvent in project syncany by syncany.
the class DownOperation method downloadUnknownRemoteDatabases.
/**
* Downloads the previously identified new/unknown remote databases to the local cache
* and returns a map with the local cache files mapped to the given remote database
* files. The method additionally fires events for every database it downloads.
*/
private SortedMap<File, DatabaseRemoteFile> downloadUnknownRemoteDatabases(List<DatabaseRemoteFile> unknownRemoteDatabases) throws StorageException {
logger.log(Level.INFO, "Downloading unknown databases.");
SortedMap<File, DatabaseRemoteFile> unknownRemoteDatabasesInCache = new TreeMap<File, DatabaseRemoteFile>();
int downloadFileIndex = 0;
for (DatabaseRemoteFile remoteFile : unknownRemoteDatabases) {
File unknownRemoteDatabaseFileInCache = config.getCache().getDatabaseFile(remoteFile.getName());
DatabaseRemoteFile unknownDatabaseRemoteFile = new DatabaseRemoteFile(remoteFile.getName());
logger.log(Level.INFO, "- Downloading {0} to local cache at {1}", new Object[] { remoteFile.getName(), unknownRemoteDatabaseFileInCache });
eventBus.post(new DownDownloadFileSyncExternalEvent(config.getLocalDir().getAbsolutePath(), "database", ++downloadFileIndex, unknownRemoteDatabases.size()));
transferManager.download(unknownDatabaseRemoteFile, unknownRemoteDatabaseFileInCache);
unknownRemoteDatabasesInCache.put(unknownRemoteDatabaseFileInCache, unknownDatabaseRemoteFile);
result.getDownloadedUnknownDatabases().add(remoteFile.getName());
}
return unknownRemoteDatabasesInCache;
}
use of org.syncany.operations.daemon.messages.DownDownloadFileSyncExternalEvent in project syncany by syncany.
the class Downloader method downloadAndDecryptMultiChunks.
/**
* Downloads the given multichunks from the remote storage and decrypts them
* to the local cache folder.
*/
public void downloadAndDecryptMultiChunks(Set<MultiChunkId> unknownMultiChunkIds) throws StorageException, IOException {
logger.log(Level.INFO, "Downloading and extracting multichunks ...");
int multiChunkNumber = 0;
for (MultiChunkId multiChunkId : unknownMultiChunkIds) {
File localEncryptedMultiChunkFile = config.getCache().getEncryptedMultiChunkFile(multiChunkId);
File localDecryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkId);
MultichunkRemoteFile remoteMultiChunkFile = new MultichunkRemoteFile(multiChunkId);
multiChunkNumber++;
if (localDecryptedMultiChunkFile.exists()) {
logger.log(Level.INFO, " + Decrypted multichunk exists locally " + multiChunkId + ". No need to download it!");
} else {
eventBus.post(new DownDownloadFileSyncExternalEvent(config.getLocalDir().getAbsolutePath(), "multichunk", multiChunkNumber, unknownMultiChunkIds.size()));
logger.log(Level.INFO, " + Downloading multichunk " + multiChunkId + " ...");
transferManager.download(remoteMultiChunkFile, localEncryptedMultiChunkFile);
try {
logger.log(Level.INFO, " + Decrypting multichunk " + multiChunkId + " ...");
InputStream multiChunkInputStream = config.getTransformer().createInputStream(new FileInputStream(localEncryptedMultiChunkFile));
OutputStream decryptedMultiChunkOutputStream = new FileOutputStream(localDecryptedMultiChunkFile);
IOUtils.copy(multiChunkInputStream, decryptedMultiChunkOutputStream);
decryptedMultiChunkOutputStream.close();
multiChunkInputStream.close();
} catch (IOException e) {
// Security: Deleting the multichunk if the decryption/extraction failed is important!
// If it is not deleted, the partially decrypted multichunk will reside in the
// local cache and the next 'down' will try to use it. If this is the only
// multichunk that has been tampered with, other changes might be applied to the
// file system! See https://github.com/syncany/syncany/issues/59#issuecomment-55154793
logger.log(Level.FINE, " -> FAILED: Decryption/extraction of multichunk failed, deleting " + multiChunkId + " ...");
localDecryptedMultiChunkFile.delete();
throw new IOException("Decryption/extraction of multichunk " + multiChunkId + " failed. The multichunk might have been tampered with!", e);
} finally {
logger.log(Level.FINE, " + Locally deleting multichunk " + multiChunkId + " ...");
localEncryptedMultiChunkFile.delete();
}
}
}
transferManager.disconnect();
}
Aggregations