コード例 #1
0
  protected void moveFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (FileUtil.notExists(targetFilePath)
        || sanitizeFileName(targetFilePath)
        || isInErrorState(targetFilePath)) {

      return;
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null)
        || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

      queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

      return;
    }

    Path sourceFilePath = Paths.get(syncWatchEvent.getPreviousFilePathName());

    SyncFile sourceSyncFile = SyncFileService.fetchSyncFile(sourceFilePath.toString());

    SyncFile targetSyncFile = SyncFileService.fetchSyncFile(targetFilePath.toString());

    if ((sourceSyncFile == null) || (targetSyncFile != null)) {
      if (Files.isDirectory(targetFilePath)) {
        addFolder(syncWatchEvent);
      } else {
        addFile(syncWatchEvent);
      }

      return;
    } else if (isPendingTypePK(sourceSyncFile)) {
      queueSyncWatchEvent(sourceSyncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String fileType = sourceSyncFile.getType();

    if (fileType.equals(SyncFile.TYPE_FILE)) {
      SyncFileService.moveFileSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, sourceSyncFile);
    } else {
      SyncFileService.moveFolderSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, sourceSyncFile);
    }

    renameFile(syncWatchEvent);
  }
コード例 #2
0
ファイル: FileUtil.java プロジェクト: sara-liu/liferay-portal
  public static boolean isIgnoredFilePath(Path filePath) {
    String fileName = String.valueOf(filePath.getFileName());

    if (_syncFileIgnoreNames.contains(fileName)
        || MSOfficeFileUtil.isTempCreatedFile(filePath)
        || (PropsValues.SYNC_FILE_IGNORE_HIDDEN && isHidden(filePath))
        || Files.isSymbolicLink(filePath)
        || fileName.endsWith(".lnk")) {

      return true;
    }

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

    if (syncFile == null) {
      return isIgnoredFilePath(filePath.getParent());
    }

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

      return true;
    }

    return false;
  }
コード例 #3
0
ファイル: FileUtil.java プロジェクト: sara-liu/liferay-portal
  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;
        }
      }
    }
  }
コード例 #4
0
  protected void renameFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path sourceFilePath = Paths.get(syncWatchEvent.getPreviousFilePathName());

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

    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (sanitizeFileName(targetFilePath)) {
      return;
    }

    if (syncFile == null) {
      if (Files.isDirectory(targetFilePath)) {
        addFolder(syncWatchEvent);
      } else {
        addFile(syncWatchEvent);
      }

      return;
    } else if (isPendingTypePK(syncFile)) {
      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String fileType = syncFile.getType();

    if (fileType.equals(SyncFile.TYPE_FILE)) {
      SyncFileService.renameFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      SyncFileService.renameFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
    }
  }
コード例 #5
0
  private SyncFile _getSyncFile(FullHttpRequest fullHttpRequest) {
    String[] pathArray = StringUtils.split(fullHttpRequest.uri(), "/");

    if (pathArray.length != 4) {
      return null;
    }

    String lanServerUuid = pathArray[0];
    long repositoryId = GetterUtil.getLong(pathArray[1]);
    long typePK = GetterUtil.getLong(pathArray[2]);
    long versionId = GetterUtil.getLong(pathArray[3]);

    if (lanServerUuid.isEmpty() || (repositoryId == 0) || (typePK == 0) || (versionId == 0)) {

      return null;
    }

    List<SyncAccount> syncAccounts = SyncAccountService.findSyncAccounts(lanServerUuid);

    for (SyncAccount syncAccount : syncAccounts) {
      SyncFile syncFile =
          SyncFileService.fetchSyncFile(
              repositoryId, syncAccount.getSyncAccountId(), typePK, versionId);

      if ((syncFile != null) && (syncFile.getState() == SyncFile.STATE_SYNCED)) {

        return syncFile;
      }
    }

    return null;
  }
コード例 #6
0
  public static SyncFile unsyncFolder(String filePathName) throws Exception {
    SyncFile syncFile = SyncFileService.fetchSyncFile(filePathName);

    if (syncFile == null) {
      return addSyncFile(
          null, null, null, filePathName, null, null, 0, 0, SyncFile.STATE_UNSYNCED, 0, null);
    }

    setStatuses(syncFile, SyncFile.STATE_UNSYNCED, SyncFile.UI_EVENT_NONE);

    return syncFile;
  }
コード例 #7
0
  protected void doRun() throws Exception {
    SyncWatchEvent lastSyncWatchEvent = SyncWatchEventService.getLastSyncWatchEvent(_syncAccountId);

    if (lastSyncWatchEvent == null) {
      return;
    }

    long delta = System.currentTimeMillis() - lastSyncWatchEvent.getTimestamp();

    if (delta <= 500) {
      SyncEngineUtil.fireSyncEngineStateChanged(
          _syncAccountId, SyncEngineUtil.SYNC_ENGINE_STATE_PROCESSING);

      return;
    }

    if (_logger.isTraceEnabled()) {
      _logger.trace("Processing Sync watch events");
    }

    _pendingTypePKSyncFileIds.clear();

    List<SyncWatchEvent> syncWatchEvents = null;

    if (OSDetector.isApple()) {
      syncWatchEvents = SyncWatchEventService.findBySyncAccountId(_syncAccountId);
    } else {
      syncWatchEvents =
          SyncWatchEventService.findBySyncAccountId(_syncAccountId, "eventType", true);
    }

    for (SyncWatchEvent syncWatchEvent : syncWatchEvents) {
      processSyncWatchEvent(syncWatchEvent);
    }

    for (Map.Entry<String, List<SyncWatchEvent>> entry : _dependentSyncWatchEventsMaps.entrySet()) {

      SyncFile syncFile = SyncFileService.fetchSyncFile(entry.getKey());

      if ((syncFile != null) && (syncFile.getTypePK() > 0)) {
        for (SyncWatchEvent syncWatchEvent : entry.getValue()) {
          processSyncWatchEvent(syncWatchEvent);
        }
      }
    }

    SyncEngineUtil.fireSyncEngineStateChanged(
        _syncAccountId, SyncEngineUtil.SYNC_ENGINE_STATE_PROCESSED);

    _processedSyncWatchEventIds.clear();
  }
コード例 #8
0
  protected String getFileType(String eventType, Path filePath) {
    if (eventType.equals(SyncWatchEvent.EVENT_TYPE_DELETE)) {
      SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

      if (syncFile != null) {
        return syncFile.getType();
      }
    }

    if (Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)) {
      return SyncFile.TYPE_FOLDER;
    }

    return SyncFile.TYPE_FILE;
  }
コード例 #9
0
  protected long getRepositoryId(Path filePath) {
    while (true) {
      filePath = filePath.getParent();

      if (filePath == null) {
        return 0;
      }

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

      if (syncFile != null) {
        return syncFile.getRepositoryId();
      }
    }
  }
コード例 #10
0
  protected void modifyFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path filePath = Paths.get(syncWatchEvent.getFilePathName());

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

    if (syncFile == null) {
      return;
    } else if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    } else if (!FileUtil.isModified(syncFile)) {
      return;
    }

    SyncFileService.updateFileSyncFile(filePath, _syncAccountId, syncFile);
  }
コード例 #11
0
  protected void deleteFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path filePath = Paths.get(syncWatchEvent.getFilePathName());

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

    if ((syncFile == null) || !FileUtil.notExists(Paths.get(syncFile.getFilePathName()))) {

      return;
    } else if ((syncFile.getState() == SyncFile.STATE_ERROR)
        || (syncFile.getState() == SyncFile.STATE_UNSYNCED)) {

      SyncFileService.deleteSyncFile(syncFile, false);

      return;
    } else if (syncFile.getState() == SyncFile.STATE_IN_PROGRESS) {
      Set<Event> events = FileEventManager.getEvents(syncFile.getSyncFileId());

      for (Event event : events) {
        event.cancel();
      }

      if (isPendingTypePK(syncFile)) {
        SyncFileService.deleteSyncFile(syncFile);

        return;
      }
    } else if (isPendingTypePK(syncFile)) {
      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String type = syncFile.getType();

    if (type.equals(SyncFile.TYPE_FILE)) {
      FileEventUtil.deleteFile(_syncAccountId, syncFile);
    } else {
      FileEventUtil.deleteFolder(_syncAccountId, syncFile);
    }
  }
コード例 #12
0
  protected boolean isInErrorState(Path filePath) {
    while (true) {
      if (filePath == null) {
        return false;
      }

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

      if (syncFile != null) {
        if (syncFile.isSystem()) {
          break;
        }

        if (syncFile.getState() == SyncFile.STATE_ERROR) {
          return true;
        }
      }

      filePath = filePath.getParent();
    }

    return false;
  }
コード例 #13
0
  protected void addFolder(SyncWatchEvent syncWatchEvent) throws Exception {
    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (sanitizeFileName(targetFilePath) || isInErrorState(targetFilePath)) {

      return;
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null)
        || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

      queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

      return;
    }

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

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

      if (!verifySite(syncFile, parentSyncFile)) {
        syncFile = null;
      }
    }

    if (syncFile == null) {
      SyncFileService.addFolderSyncFile(
          targetFilePath,
          parentSyncFile.getTypePK(),
          parentSyncFile.getRepositoryId(),
          _syncAccountId);

      return;
    }

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

    if (targetFilePath.equals(sourceFilePath)) {
      FileKeyUtil.writeFileKey(targetFilePath, String.valueOf(syncFile.getSyncFileId()), true);
    } else if (FileUtil.exists(sourceFilePath)) {
      SyncFileService.addFolderSyncFile(
          targetFilePath,
          parentSyncFile.getTypePK(),
          parentSyncFile.getRepositoryId(),
          _syncAccountId);

      return;
    } else if (parentTargetFilePath.equals(sourceFilePath.getParent())) {
      if (isPendingTypePK(syncFile)) {
        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.updateFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      if (isPendingTypePK(syncFile)) {
        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.moveFolderSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, syncFile);

      Path sourceFileNameFilePath = sourceFilePath.getFileName();

      if (!sourceFileNameFilePath.equals(targetFilePath.getFileName())) {
        SyncFileService.updateFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
      }
    }

    SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(_syncAccountId);

    if (syncAccount.getState() == SyncAccount.STATE_CONNECTED) {
      SyncWatchEvent relatedSyncWatchEvent =
          SyncWatchEventService.fetchSyncWatchEvent(
              SyncWatchEvent.EVENT_TYPE_DELETE,
              syncWatchEvent.getFilePathName(),
              syncWatchEvent.getTimestamp());

      if (relatedSyncWatchEvent != null) {
        _processedSyncWatchEventIds.add(relatedSyncWatchEvent.getSyncWatchEventId());
      }
    }
  }
コード例 #14
0
  protected void addFile(SyncWatchEvent syncWatchEvent) throws Exception {
    final Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (FileUtil.notExists(targetFilePath)
        || sanitizeFileName(targetFilePath)
        || isInErrorState(targetFilePath)) {

      return;
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    final SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null)
        || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

      queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

      return;
    }

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

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

      if (!verifySite(syncFile, parentSyncFile)) {
        syncFile = null;
      }
    }

    if (syncFile == null) {
      Runnable runnable =
          new Runnable() {

            @Override
            public void run() {
              try {
                SyncSite syncSite =
                    SyncSiteService.fetchSyncSite(parentSyncFile.getRepositoryId(), _syncAccountId);

                if ((syncSite == null)
                    || !syncSite.isActive()
                    || !FileUtil.checkFilePath(targetFilePath)) {

                  return;
                }

                SyncFileService.addFileSyncFile(
                    targetFilePath,
                    parentSyncFile.getTypePK(),
                    parentSyncFile.getRepositoryId(),
                    _syncAccountId);
              } catch (Exception e) {
                if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

                  _logger.error(e.getMessage(), e);
                }
              }
            }
          };

      _executorService.execute(runnable);

      return;
    }

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

    if (targetFilePath.equals(sourceFilePath)) {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      if (FileUtil.isModified(syncFile)) {
        SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
      }
    } else if (FileUtil.exists(sourceFilePath)) {
      try {
        if ((Files.size(targetFilePath) == 0)
            || FileUtil.isModified(syncFile, targetFilePath)
            || isInErrorState(sourceFilePath)) {

          SyncFileService.addFileSyncFile(
              targetFilePath,
              parentSyncFile.getTypePK(),
              parentSyncFile.getRepositoryId(),
              _syncAccountId);
        } else {
          SyncFileService.copySyncFile(
              syncFile,
              targetFilePath,
              parentSyncFile.getTypePK(),
              parentSyncFile.getRepositoryId(),
              _syncAccountId);
        }
      } catch (Exception e) {
        if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

          _logger.error(e.getMessage(), e);
        }
      }

      return;
    } else if (parentTargetFilePath.equals(sourceFilePath.getParent())) {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.moveFileSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, syncFile);

      Path sourceFileNameFilePath = sourceFilePath.getFileName();

      if (!sourceFileNameFilePath.equals(targetFilePath.getFileName())) {
        SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
      }
    }

    SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(_syncAccountId);

    if (syncAccount.getState() == SyncAccount.STATE_CONNECTED) {
      SyncWatchEvent relatedSyncWatchEvent =
          SyncWatchEventService.fetchSyncWatchEvent(
              SyncWatchEvent.EVENT_TYPE_DELETE,
              syncWatchEvent.getFilePathName(),
              syncWatchEvent.getTimestamp());

      if (relatedSyncWatchEvent != null) {
        _processedSyncWatchEventIds.add(relatedSyncWatchEvent.getSyncWatchEventId());
      }
    }
  }