private void downloadAndDecryptMultiChunks(Set<MultiChunkEntry> unknownMultiChunks) throws StorageException, IOException { logger.log(Level.INFO, "- Downloading and extracting multichunks ..."); TransferManager transferManager = config.getConnection().createTransferManager(); // TODO [medium] Check existing files by checksum and do NOT download them if they exist // locally, or copy them for (MultiChunkEntry multiChunkEntry : unknownMultiChunks) { File localEncryptedMultiChunkFile = config.getCache().getEncryptedMultiChunkFile(multiChunkEntry.getId()); File localDecryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkEntry.getId()); MultiChunkRemoteFile remoteMultiChunkFile = new MultiChunkRemoteFile( localEncryptedMultiChunkFile .getName()); // TODO [low] Make MultiChunkRemoteFile class, or something like that logger.log( Level.INFO, " + Downloading multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); transferManager.download(remoteMultiChunkFile, localEncryptedMultiChunkFile); result.downloadedMultiChunks.add(multiChunkEntry); logger.log( Level.INFO, " + Decrypting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); InputStream multiChunkInputStream = config .getTransformer() .createInputStream(new FileInputStream(localEncryptedMultiChunkFile)); OutputStream decryptedMultiChunkOutputStream = new FileOutputStream(localDecryptedMultiChunkFile); // TODO [medium] Calculate checksum while writing file, to verify correct content FileUtil.appendToOutputStream(multiChunkInputStream, decryptedMultiChunkOutputStream); decryptedMultiChunkOutputStream.close(); multiChunkInputStream.close(); logger.log( Level.FINE, " + Locally deleting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ..."); localEncryptedMultiChunkFile.delete(); } transferManager.disconnect(); }
private List<File> downloadUnknownRemoteDatabases( TransferManager transferManager, List<RemoteFile> unknownRemoteDatabases) throws StorageException { logger.log(Level.INFO, "Downloading unknown databases."); List<File> unknownRemoteDatabasesInCache = new ArrayList<File>(); for (RemoteFile remoteFile : unknownRemoteDatabases) { File unknownRemoteDatabaseFileInCache = config.getCache().getDatabaseFile(remoteFile.getName()); logger.log( Level.INFO, "- Downloading {0} to local cache at {1}", new Object[] {remoteFile.getName(), unknownRemoteDatabaseFileInCache}); transferManager.download( new DatabaseRemoteFile(remoteFile.getName()), unknownRemoteDatabaseFileInCache); unknownRemoteDatabasesInCache.add(unknownRemoteDatabaseFileInCache); result.getDownloadedUnknownDatabases().add(remoteFile.getName()); } return unknownRemoteDatabasesInCache; }