Example #1
0
  public static String getNextFilePathName(String filePathName) {
    Path filePath = Paths.get(filePathName);

    Path parentFilePath = filePath.getParent();

    for (int i = 0; ; i++) {
      StringBuilder sb = new StringBuilder();

      sb.append(FilenameUtils.getBaseName(filePathName));

      if (i > 0) {
        sb.append(" (");
        sb.append(i);
        sb.append(")");
      }

      String extension = FilenameUtils.getExtension(filePathName);

      if (extension.length() > 0) {
        sb.append(".");
        sb.append(extension);
      }

      String tempFilePathName = FileUtil.getFilePathName(parentFilePath.toString(), sb.toString());

      if (SyncFileService.fetchSyncFile(tempFilePathName) == null) {
        Path tempFilePath = Paths.get(tempFilePathName);

        if (!Files.exists(tempFilePath)) {
          return tempFilePathName;
        }
      }
    }
  }
  protected boolean sanitizeFileName(Path filePath) {
    if (OSDetector.isWindows()) {
      return false;
    }

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

    String sanitizedFileName =
        FileUtil.getSanitizedFileName(fileName, FilenameUtils.getExtension(fileName));

    if (!sanitizedFileName.equals(fileName)) {
      String sanitizedFilePathName =
          FileUtil.getFilePathName(String.valueOf(filePath.getParent()), sanitizedFileName);

      sanitizedFilePathName = FileUtil.getNextFilePathName(sanitizedFilePathName);

      FileUtil.checkFilePath(filePath);

      FileUtil.moveFile(filePath, Paths.get(sanitizedFilePathName));

      return true;
    }

    return false;
  }
  @Test
  public void testWatchEvent1() {
    SyncSiteWatchEventListener syncSiteWatchEventListener =
        new SyncSiteWatchEventListener(syncAccount.getSyncAccountId());

    String sourceFilePathName = FileUtil.getFilePathName(filePathName, "test-site1", "a.txt");

    syncSiteWatchEventListener.watchEvent(
        SyncWatchEvent.EVENT_TYPE_RENAME_FROM, Paths.get(sourceFilePathName));

    String targetFilePathName = FileUtil.getFilePathName(filePathName, "test-site2", "a.txt");

    syncSiteWatchEventListener.watchEvent(
        SyncWatchEvent.EVENT_TYPE_RENAME_TO, Paths.get(targetFilePathName));

    SyncWatchEvent lastSyncWatchEvent =
        SyncWatchEventService.getLastSyncWatchEvent(syncAccount.getSyncAccountId());

    Assert.assertEquals(SyncWatchEvent.EVENT_TYPE_CREATE, lastSyncWatchEvent.getEventType());
  }
  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();

    _syncSite1 =
        SyncSiteTestUtil.addSyncSite(
            10158,
            FileUtil.getFilePathName(filePathName, "test-site1"),
            10185,
            syncAccount.getSyncAccountId());

    SyncSiteService.activateSyncSite(_syncSite1.getSyncSiteId(), true);

    _syncSite2 =
        SyncSiteTestUtil.addSyncSite(
            10158,
            FileUtil.getFilePathName(filePathName, "test-site2"),
            10186,
            syncAccount.getSyncAccountId());

    SyncSiteService.activateSyncSite(_syncSite2.getSyncSiteId(), true);
  }
  @Test
  public void testDeleteFolderSyncFile() throws Exception {
    List<SyncFile> syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(1, syncFiles.size());

    SyncFile folderSyncFileA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a"), syncAccount.getSyncAccountId());

    SyncFile folderSyncFileAA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a", "a"),
            folderSyncFileA.getTypePK(),
            syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFolderSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "b"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFolderSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "a", "a"),
        folderSyncFileAA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "b.txt"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "c.txt"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "a", "a.txt"),
        folderSyncFileAA.getTypePK(),
        syncAccount.getSyncAccountId());

    syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(8, syncFiles.size());

    SyncFileService.deleteSyncFile(folderSyncFileA);

    syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(1, syncFiles.size());
  }
  @Test
  public void testDoUpdateFolderSyncFile() throws Exception {
    SyncFile folderSyncFileA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a"), syncAccount.getSyncAccountId());

    SyncFile folderSyncFileB =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "b"), syncAccount.getSyncAccountId());

    SyncFile folderSyncFileAA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a", "a"),
            folderSyncFileA.getTypePK(),
            syncAccount.getSyncAccountId());

    SyncFile fileSyncFileAA =
        SyncFileTestUtil.addFileSyncFile(
            FileUtil.getFilePathName(filePathName, "a", "a.txt"),
            folderSyncFileA.getTypePK(),
            syncAccount.getSyncAccountId());

    SyncFileService.updateSyncFile(
        Paths.get(FileUtil.getFilePathName(filePathName, "b", "a")),
        folderSyncFileB.getTypePK(),
        folderSyncFileA);

    SyncFilePersistence syncFilePersistence = SyncFileService.getSyncFilePersistence();

    folderSyncFileAA = syncFilePersistence.queryForId(folderSyncFileAA.getTypePK());

    Assert.assertEquals(
        FileUtil.getFilePathName(filePathName, "b", "a", "a"), folderSyncFileAA.getFilePathName());

    fileSyncFileAA = syncFilePersistence.queryForId(fileSyncFileAA.getTypePK());

    Assert.assertEquals(
        FileUtil.getFilePathName(filePathName, "b", "a", "a.txt"),
        fileSyncFileAA.getFilePathName());
  }