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()); } }
/** Creates a WatchService and registers the given directory */ WatchDir(Path dir, boolean recursive) throws IOException { this.watcher = FileSystems.getDefault().newWatchService(); this.keys = new HashMap<WatchKey, Path>(); this.recursive = recursive; if (recursive) { System.out.format("Scanning %s ...\n", dir); registerAll(dir); System.out.println("Done."); } else { register(dir); } // enable trace after initial registration this.trace = true; }
public void downloadFile(String relPath) { VOSync.debug("Downloading file from storage: " + relPath); Path filePath = FileSystems.getDefault().getPath(startDir.toString(), relPath.substring(1)); try { WatchKey key = filePath.getParent().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.remove(key); key.cancel(); FileOutputStream outp = new FileOutputStream(filePath.toFile()); DropboxFileInfo info = api.getFile(relPath, null, outp, null); outp.close(); key = filePath.getParent().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.put(key, filePath.getParent()); MetaHandler.setFile(relPath, filePath.toFile(), info.getMetadata().rev); } catch (IOException ex) { logger.error("Error downloading file " + relPath + ": " + ex.getMessage()); } catch (DropboxException ex) { ex.printStackTrace(); logger.error("Error downloading file " + relPath + ": " + ex.getMessage()); } }
/** * Creates a WatchService and registers the given directory * * @throws IOException */ DirWatcher2(Path dir, DropboxAPI<WebAuthSession> api) throws IOException { this.watcher = FileSystems.getDefault().newWatchService(); this.keys = new HashMap<WatchKey, Path>(); this.startDir = dir; this.api = api; }