private void uploadFile(String relPath, Path fullPath) throws DropboxException, IOException { if (!fullPath.toFile().exists()) return; /*if(api.metadata(relPath, 1, null, false, null).rev == MetaHandler.getRev(relPath)){ logger.debug("File "+relPath+" not changed"); return; }*/ VOSync.debug("Uploading " + relPath); String rev = MetaHandler.getRev(relPath); InputStream inp = new FileInputStream(fullPath.toFile()); Entry fileEntry = api.putFile(relPath, inp, fullPath.toFile().length(), rev, null); inp.close(); Path destFilePath = FileSystems.getDefault() .getPath(fullPath.toFile().getParentFile().getPath(), fileEntry.fileName()); MetaHandler.setFile(relPath, destFilePath.toFile(), fileEntry.rev); logger.debug(relPath + " put to db"); // if the file was renamed, move the file on disk and download the current from server if (!fileEntry.fileName().equals(fullPath.toFile().getName())) { logger.error(fileEntry.fileName() + " != " + fullPath.toFile().getName()); fullPath.toFile().renameTo(destFilePath.toFile()); } }
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()); } }