예제 #1
0
  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());
    }
  }
예제 #2
0
  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());
    }
  }
예제 #3
0
  @Override
  public void run() {
    logger.debug("Register root " + startDir.toString());

    try {
      registerAll(startDir);
    } catch (IOException ex) {
      logger.error(ex.getMessage());
      return;
    }

    if (isInterrupted()) return;

    VOSync.debug("Sync local db with drive");

    DbPool.goSql(
        "Synching the local db with drive",
        "select NAME from FILES",
        new SqlWorker<Boolean>() {
          @Override
          public Boolean go(Connection conn, PreparedStatement stmt) throws SQLException {
            ResultSet resSet = stmt.executeQuery();
            while (resSet.next()) {
              try {
                String fileName = resSet.getString(1);
                Path filePath =
                    FileSystems.getDefault().getPath(startDir.toString(), fileName.substring(1));
                if (!filePath.toFile().exists()) {
                  logger.debug(
                      "Deleting file " + fileName + " existing in DB and not present on disk");
                  api.delete(fileName);
                  MetaHandler.delete(fileName);
                }
              } catch (DropboxException ex) {
              }
            }
            resSet.close();
            return true;
          }
        });

    if (isInterrupted()) return;

    VOSync.debug("Sync storage");

    syncStorage();

    logger.debug("Start watching");

    while (!isInterrupted()) {
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        System.err.println("WatchKey " + key.toString() + " not recognized!");
        continue;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        Kind<?> kind = event.kind();

        // TBD - provide example of how OVERFLOW event is handled
        if (kind == OVERFLOW) {
          continue;
        }

        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);
        Path relativeDir = startDir.relativize(child);
        String fileRelPath = "/" + fixPath(relativeDir.toString());

        // print out event
        logger.debug(event.kind().name() + ":" + child + " " + name + " " + key);

        try {
          if (Files.exists(child, new LinkOption[] {}) && Files.isHidden(child)) {
            logger.error(
                "Skipping hidden file " + child.getFileName()); // skip OS generated catalog files
          } else {
            if (event.kind() == ENTRY_CREATE) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                registerAll(child);
              }
            } else if (event.kind() == ENTRY_DELETE) {
              logger.debug("Deleting " + fileRelPath);
              api.delete(fileRelPath);
              MetaHandler.delete(fileRelPath);
              logger.debug("Deleted!");
            } else if (event.kind() == ENTRY_MODIFY) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                // logger.debug("Renewing dir: "+relativeDir.toString());
                // TODO update folder date
                // MetaHandler.setFile(fileRelPath, child, rev);
              }
            }
          }
        } catch (IOException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        } catch (DropboxException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        }
      }

      boolean valid = key.reset();

      if (!valid) keys.remove(key);
    }
  }