Exemplo n.º 1
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;
  }
Exemplo n.º 2
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;
  }