Search in sources :

Example 1 with DownDownloadFileSyncExternalEvent

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;
}
Also used : DownDownloadFileSyncExternalEvent(org.syncany.operations.daemon.messages.DownDownloadFileSyncExternalEvent) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TreeMap(java.util.TreeMap) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) CleanupRemoteFile(org.syncany.plugins.transfer.files.CleanupRemoteFile) File(java.io.File)

Example 2 with DownDownloadFileSyncExternalEvent

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();
}
Also used : DownDownloadFileSyncExternalEvent(org.syncany.operations.daemon.messages.DownDownloadFileSyncExternalEvent) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) FileInputStream(java.io.FileInputStream)

Aggregations

File (java.io.File)2 DownDownloadFileSyncExternalEvent (org.syncany.operations.daemon.messages.DownDownloadFileSyncExternalEvent)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 TreeMap (java.util.TreeMap)1 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)1 CleanupRemoteFile (org.syncany.plugins.transfer.files.CleanupRemoteFile)1 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)1 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)1