Esempio n. 1
0
  public static SyncFile renameFileSyncFile(Path filePath, long syncAccountId, SyncFile syncFile)
      throws Exception {

    // Local sync file

    String name = _getName(filePath, syncFile);
    String sourceFileName = syncFile.getName();
    long sourceVersionId = syncFile.getVersionId();

    syncFile.setFilePathName(filePath.toString());
    syncFile.setName(name);

    update(syncFile);

    // Remote sync file

    if ((syncFile.getState() != SyncFile.STATE_ERROR)
        && (syncFile.getState() != SyncFile.STATE_UNSYNCED)) {

      FileEventUtil.updateFile(
          filePath,
          syncAccountId,
          syncFile,
          null,
          name,
          syncFile.getChecksum(),
          sourceFileName,
          sourceVersionId,
          syncFile.getChecksum());
    }

    return syncFile;
  }
Esempio n. 2
0
  public static SyncFile addFolderSyncFile(
      Path filePath, long parentFolderId, long repositoryId, long syncAccountId) throws Exception {

    // Local sync file

    if (Files.notExists(filePath)) {
      return null;
    }

    String name = String.valueOf(filePath.getFileName());

    SyncFile syncFile =
        addSyncFile(
            null,
            null,
            null,
            filePath.toString(),
            Files.probeContentType(filePath),
            name,
            parentFolderId,
            repositoryId,
            SyncFile.STATE_SYNCED,
            syncAccountId,
            SyncFile.TYPE_FOLDER);

    // Remote sync file

    FileEventUtil.addFolder(parentFolderId, repositoryId, syncAccountId, name, syncFile);

    return syncFile;
  }
Esempio n. 3
0
  public static SyncFile addFileSyncFile(
      Path filePath, long folderId, long repositoryId, long syncAccountId) throws Exception {

    // Local sync file

    if (Files.notExists(filePath)) {
      return null;
    }

    String checksum = FileUtil.getChecksum(filePath);
    String name = String.valueOf(filePath.getFileName());
    String mimeType = Files.probeContentType(filePath);

    SyncFile syncFile =
        addSyncFile(
            null,
            checksum,
            null,
            filePath.toString(),
            mimeType,
            name,
            folderId,
            repositoryId,
            SyncFile.STATE_SYNCED,
            syncAccountId,
            SyncFile.TYPE_FILE);

    // Remote sync file

    FileEventUtil.addFile(
        filePath, folderId, repositoryId, syncAccountId, checksum, name, mimeType, syncFile);

    return syncFile;
  }
Esempio n. 4
0
  public static SyncFile moveFileSyncFile(
      Path filePath, long folderId, long syncAccountId, SyncFile syncFile) throws Exception {

    // Local sync file

    SyncFile targetSyncFile = fetchSyncFile(filePath.toString());

    if (targetSyncFile != null) {
      deleteSyncFile(targetSyncFile, false);
    }

    syncFile.setFilePathName(filePath.toString());
    syncFile.setParentFolderId(folderId);

    update(syncFile);

    // Remote sync file

    if ((syncFile.getState() != SyncFile.STATE_ERROR)
        && (syncFile.getState() != SyncFile.STATE_UNSYNCED)) {

      FileEventUtil.moveFile(folderId, syncAccountId, syncFile);
    }

    return syncFile;
  }
Esempio n. 5
0
  public static SyncFile resyncFolder(SyncFile syncFile) throws Exception {
    setStatuses(syncFile, SyncFile.STATE_SYNCED, SyncFile.UI_EVENT_NONE);

    // Remote

    FileEventUtil.resyncFolder(syncFile.getSyncAccountId(), syncFile);

    return syncFile;
  }
Esempio n. 6
0
  public static SyncFile updateFileSyncFile(Path filePath, long syncAccountId, SyncFile syncFile)
      throws Exception {

    // Local sync file

    if (FileUtil.getFileKey(filePath) != syncFile.getSyncFileId()) {
      FileUtil.writeFileKey(filePath, String.valueOf(syncFile.getSyncFileId()));
    }

    Path deltaFilePath = null;

    String name = _getName(filePath, syncFile);
    String sourceChecksum = syncFile.getChecksum();
    String sourceFileName = syncFile.getName();
    long sourceVersionId = syncFile.getVersionId();
    String targetChecksum = FileUtil.getChecksum(filePath);

    if (!FileUtil.checksumsEqual(sourceChecksum, targetChecksum)
        && !IODeltaUtil.isIgnoredFilePatchingExtension(syncFile)) {

      deltaFilePath = Files.createTempFile(String.valueOf(filePath.getFileName()), ".tmp");

      deltaFilePath =
          IODeltaUtil.delta(filePath, IODeltaUtil.getChecksumsFilePath(syncFile), deltaFilePath);

      IODeltaUtil.checksums(syncFile);
    }

    syncFile.setChecksum(targetChecksum);
    syncFile.setFilePathName(filePath.toString());
    syncFile.setName(name);

    update(syncFile);

    // Remote sync file

    if ((syncFile.getState() != SyncFile.STATE_ERROR)
        && (syncFile.getState() != SyncFile.STATE_UNSYNCED)) {

      FileEventUtil.updateFile(
          filePath,
          syncAccountId,
          syncFile,
          deltaFilePath,
          name,
          sourceChecksum,
          sourceFileName,
          sourceVersionId,
          targetChecksum);
    }

    return syncFile;
  }
Esempio n. 7
0
  public static SyncFile checkOutSyncFile(long syncAccountId, SyncFile syncFile) throws Exception {

    // Local sync file

    update(syncFile);

    // Remote sync file

    FileEventUtil.checkOutFile(syncAccountId, syncFile);

    return syncFile;
  }
Esempio n. 8
0
  public static SyncFile updateFolderSyncFile(Path filePath, long syncAccountId, SyncFile syncFile)
      throws Exception {

    // Local sync file

    updateSyncFile(filePath, syncFile.getParentFolderId(), syncFile);

    // Remote sync file

    if ((syncFile.getState() != SyncFile.STATE_ERROR)
        && (syncFile.getState() != SyncFile.STATE_UNSYNCED)) {

      FileEventUtil.updateFolder(filePath, syncAccountId, syncFile);
    }

    return syncFile;
  }
Esempio n. 9
0
  public static void fireDeleteEvents(Path filePath) throws IOException {
    long startTime = System.currentTimeMillis();

    Files.walkFileTree(
        filePath,
        new SimpleFileVisitor<Path>() {

          @Override
          public FileVisitResult preVisitDirectory(
              Path filePath, BasicFileAttributes basicFileAttributes) {

            SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

            if (syncFile == null) {
              syncFile = SyncFileService.fetchSyncFile(FileKeyUtil.getFileKey(filePath));
            }

            if (syncFile != null) {
              syncFile.setLocalSyncTime(System.currentTimeMillis());

              SyncFileService.update(syncFile);
            }

            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path filePath, BasicFileAttributes basicFileAttributes) {

            SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

            if (syncFile == null) {
              syncFile = SyncFileService.fetchSyncFile(FileKeyUtil.getFileKey(filePath));
            }

            if (syncFile != null) {
              syncFile.setLocalSyncTime(System.currentTimeMillis());

              SyncFileService.update(syncFile);
            }

            return FileVisitResult.CONTINUE;
          }
        });

    List<SyncFile> deletedSyncFiles = SyncFileService.findSyncFiles(filePath.toString(), startTime);

    for (SyncFile deletedSyncFile : deletedSyncFiles) {
      if (deletedSyncFile.getTypePK() == 0) {
        SyncFileService.deleteSyncFile(deletedSyncFile, false);

        continue;
      }

      if (deletedSyncFile.isFolder()) {
        FileEventUtil.deleteFolder(deletedSyncFile.getSyncAccountId(), deletedSyncFile);
      } else {
        FileEventUtil.deleteFile(deletedSyncFile.getSyncAccountId(), deletedSyncFile);
      }
    }
  }
  @Override
  protected void doHandleResponse(HttpResponse httpResponse) throws Exception {

    final Session session = SessionManager.getSession(getSyncAccountId());

    Header header = httpResponse.getFirstHeader("Sync-JWT");

    if (header != null) {
      session.addHeader("Sync-JWT", header.getValue());
    }

    Map<String, DownloadFileHandler> handlers =
        (Map<String, DownloadFileHandler>) getParameterValue("handlers");

    InputStream inputStream = null;

    try {
      HttpEntity httpEntity = httpResponse.getEntity();

      inputStream =
          new CountingInputStream(httpEntity.getContent()) {

            @Override
            protected synchronized void afterRead(int n) {
              session.incrementDownloadedBytes(n);

              super.afterRead(n);
            }
          };

      ZipInputStream zipInputStream = new ZipInputStream(inputStream);

      ZipEntry zipEntry = null;

      while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        String zipEntryName = zipEntry.getName();

        if (zipEntryName.equals("errors.json")) {
          JsonNode rootJsonNode = JSONUtil.readTree(zipInputStream);

          Iterator<Map.Entry<String, JsonNode>> fields = rootJsonNode.fields();

          while (fields.hasNext()) {
            Map.Entry<String, JsonNode> field = fields.next();

            Handler<Void> handler = handlers.remove(field.getKey());

            JsonNode valueJsonNode = field.getValue();

            JsonNode exceptionJsonNode = valueJsonNode.get("exception");

            handler.handlePortalException(exceptionJsonNode.textValue());
          }

          break;
        }

        DownloadFileHandler downloadFileHandler = handlers.remove(zipEntryName);

        SyncFile syncFile = (SyncFile) downloadFileHandler.getParameterValue("syncFile");

        if (downloadFileHandler.isUnsynced(syncFile)) {
          continue;
        }

        if (_logger.isTraceEnabled()) {
          _logger.trace(
              "Handling response {} file path {}",
              DownloadFileHandler.class.getSimpleName(),
              syncFile.getFilePathName());
        }

        try {
          downloadFileHandler.copyFile(
              syncFile,
              Paths.get(syncFile.getFilePathName()),
              new CloseShieldInputStream(zipInputStream),
              false);
        } catch (Exception e) {
          if (!isEventCancelled()) {
            _logger.error(e.getMessage(), e);

            downloadFileHandler.removeEvent();

            FileEventUtil.downloadFile(getSyncAccountId(), syncFile, false);
          }
        } finally {
          downloadFileHandler.removeEvent();
        }
      }
    } catch (Exception e) {
      if (!isEventCancelled()) {
        _logger.error(e.getMessage(), e);

        retryEvent();
      }
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }