private void visitEntry(final Entry entry, DropboxEntryVisitor visitor) { if (null != entry.contents) { logger.debug(entry.path + "contents size: " + entry.contents.size()); for (Entry child : entry.contents) { if (!child.isDeleted) { if (!child.isDir) { visitor.visitFile(child); } else { try { visitor.preVisitDirectory(child); } catch (IOException e) { logger.error(e.getMessage()); } try { Entry childWithContents = api.metadata( child.path, 0, MetaHandler.getHash(child.path), true, MetaHandler.getRev(child.path)); visitEntry(childWithContents, visitor); } catch (DropboxException e) { logger.error(e.getMessage()); } visitor.postVisitDirectory(child); } } } } }
private void syncStorage() { try { logger.debug("Synching storage."); final String rootDir = "/"; Entry folderEntry = api.metadata(rootDir, 0, MetaHandler.getHash(rootDir), true, MetaHandler.getRev(rootDir)); // TODO handle removed entries visitEntry( folderEntry, new DropboxEntryVisitor() { @Override public void preVisitDirectory(Entry entry) throws IOException { if (!MetaHandler.isStored(entry.path)) { logger.debug("Creating local dir: " + entry.path); Path filePath = FileSystems.getDefault().getPath(startDir.toString(), entry.path.substring(1)); WatchKey key = filePath .getParent() .register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.remove(key); key.cancel(); filePath.toFile().mkdir(); key = filePath .getParent() .register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.put(key, filePath.getParent()); MetaHandler.setFile(entry.path, filePath.toFile(), entry.rev); } } @Override public void visitFile(Entry entry) { if (!MetaHandler.isStored(entry.path)) downloadFile(entry.path); else if (!MetaHandler.isCurrent(entry.path, entry.rev)) { Path fullPath = FileSystems.getDefault().getPath(startDir.toString(), entry.path.substring(1)); try { logger.debug("Uploading file " + entry.path); uploadFile(entry.path, fullPath); } catch (Exception e) { logger.error("Error uploading file: " + e.getMessage()); } } } }); } catch (DropboxException ex) { logger.error("Error syching root dir: " + ex.getMessage()); } }