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;
  }
  protected static void doDeleteSyncFile(SyncFile syncFile, boolean notify) throws SQLException {

    if (syncFile.isFile()) {
      final Path filePath = IODeltaUtil.getChecksumsFilePath(syncFile);

      Runnable runnable =
          new Runnable() {

            @Override
            public void run() {
              try {
                Files.deleteIfExists(filePath);
              } catch (IOException ioe) {
                _logger.error(ioe.getMessage(), ioe);
              }
            }
          };

      ExecutorService executorService = SyncEngine.getExecutorService();

      executorService.execute(runnable);
    }

    _syncFilePersistence.delete(syncFile, notify);
  }
  @Override
  protected void doHandleResponse(HttpResponse httpResponse) throws Exception {

    InputStream inputStream = null;

    try {
      SyncFile syncFile = (SyncFile) getParameterValue("syncFile");

      Path filePath = Paths.get(syncFile.getFilePathName());

      HttpEntity httpEntity = httpResponse.getEntity();

      inputStream = httpEntity.getContent();

      Path tempFilePath = Files.createTempFile(String.valueOf(syncFile.getSyncFileId()), ".tmp");

      if (Files.exists(filePath)) {
        Files.copy(filePath, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
      }

      if ((Boolean) getParameterValue("patch")) {
        IODeltaUtil.patch(tempFilePath, inputStream);
      } else {
        Files.copy(inputStream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
      }

      syncFile.setFileKey(FileUtil.getFileKey(tempFilePath));
      syncFile.setState(SyncFile.STATE_SYNCED);
      syncFile.setUiEvent((Integer) getParameterValue("uiEvent"));

      SyncFileService.update(syncFile);

      Files.move(
          tempFilePath,
          filePath,
          StandardCopyOption.ATOMIC_MOVE,
          StandardCopyOption.REPLACE_EXISTING);
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  public static SyncFile addSyncFile(
      String changeLog,
      String checksum,
      String description,
      String filePathName,
      String mimeType,
      String name,
      long parentFolderId,
      long repositoryId,
      int state,
      long syncAccountId,
      String type)
      throws Exception {

    SyncFile syncFile = new SyncFile();

    syncFile.setChangeLog(changeLog);
    syncFile.setChecksum(checksum);
    syncFile.setDescription(description);
    syncFile.setFilePathName(filePathName);
    syncFile.setLocalSyncTime(System.currentTimeMillis());
    syncFile.setMimeType(mimeType);
    syncFile.setName(name);
    syncFile.setParentFolderId(parentFolderId);
    syncFile.setRepositoryId(repositoryId);
    syncFile.setState(state);
    syncFile.setSyncAccountId(syncAccountId);
    syncFile.setType(type);

    _syncFilePersistence.create(syncFile);

    FileUtil.writeFileKey(Paths.get(filePathName), String.valueOf(syncFile.getSyncFileId()));

    IODeltaUtil.checksums(syncFile);

    return syncFile;
  }