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; }
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; }
private static String _getName(Path filePath, SyncFile syncFile) { String name = String.valueOf(filePath.getFileName()); String sanitizedFileName = FileUtil.getSanitizedFileName(syncFile.getName(), syncFile.getExtension()); if (sanitizedFileName.equals(FileUtil.getSanitizedFileName(name, syncFile.getExtension()))) { name = syncFile.getName(); } return name; }
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; } } } }
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; }
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); }
@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()); }
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); }
@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); } }
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); } }
@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()); }
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; }
public static SyncAccount addSyncAccount( String filePathName, String login, int maxConnections, String name, String password, int pollInterval, SyncSite[] syncSites, boolean trustSelfSigned, String url) throws Exception { // Sync account SyncAccount syncAccount = new SyncAccount(); syncAccount.setFilePathName(filePathName); syncAccount.setLogin(login); syncAccount.setMaxConnections(maxConnections); syncAccount.setName(name); syncAccount.setPassword(Encryptor.encrypt(password)); syncAccount.setPollInterval(pollInterval); syncAccount.setTrustSelfSigned(trustSelfSigned); syncAccount.setUrl(url); _syncAccountPersistence.create(syncAccount); // Sync file Files.createDirectories(Paths.get(filePathName)); SyncFileService.addSyncFile( null, null, filePathName, FileUtil.getFileKey(filePathName), filePathName, null, filePathName, 0, 0, syncAccount.getSyncAccountId(), SyncFile.TYPE_SYSTEM); // Sync sites if (syncSites != null) { for (SyncSite syncSite : syncSites) { String syncSiteName = syncSite.getName(); if (!FileUtil.isValidFileName(syncSiteName)) { syncSiteName = String.valueOf(syncSite.getGroupId()); } syncSite.setFilePathName(syncAccount.getFilePathName() + "/" + syncSiteName); syncSite.setSyncAccountId(syncAccount.getSyncAccountId()); SyncSiteService.update(syncSite); } } return syncAccount; }
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()); } } }
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()); } } }