/// <summary> /// Called when [file changed]. /// </summary> /// <param name="source">The source.</param> /// <param name="e">The <see cref="FileSystemEventArgs"/> instance containing the event data.</param> private void OnFileChanged(object source, FileSystemEventArgs e) { if (string.IsNullOrEmpty(Path.GetExtension(e.FullPath))) { if (e.ChangeType == WatcherChangeTypes.Deleted) { //Remove records for files that no longer exists this.RemoveNonExistingData(); } } else { Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes changeType = Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Created; if (e.ChangeType == WatcherChangeTypes.Created) { changeType = Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Created; } else if (e.ChangeType == WatcherChangeTypes.Deleted) { changeType = Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Deleted; } else { return; } this.FileChanged(e.FullPath, changeType); } }
/// <summary> /// Takes appropriate actions based on the resource file type /// </summary> /// <param name="path">The file path.</param> /// <param name="changeType">Type of the change.</param> /// <param name="oldFilePath">The old file path.</param> protected virtual void FileChanged(string filePath, Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes changeType, string oldFilePath = "") { var virtualFilePath = this.ConvertToVirtualPath(filePath); string packageName = string.Empty; var fileName = VirtualPathUtility.GetFileName(virtualFilePath); //get the directory tree part from the virtal path var resourceDirectoryTree = VirtualPathUtility.GetDirectory(virtualFilePath).Split('/'); //the resource folder is actualy the file parent folder string resourceFolder = resourceDirectoryTree[resourceDirectoryTree.Length - 2]; string oldFileName = string.Empty; if (changeType == FileChangeTypes.Renamed) { var oldVirtualFilePath = filePath.Substring(HostingEnvironment.ApplicationPhysicalPath.Length - 1).Replace('\\', '/'); oldFileName = VirtualPathUtility.GetFileName(oldVirtualFilePath); } //get the watched directory info from the list var watchedDirInfos = this.WatchedFoldersAndPackages.Where(dirInfo => virtualFilePath.StartsWith(dirInfo.Key, StringComparison.InvariantCultureIgnoreCase)); //continue only if the directory is in the list if (watchedDirInfos.Count() > 0) { var watchedDirInfo = watchedDirInfos.First(); //if the watched directory information shows that this a package directory //extract the package name out of the virtual path if (watchedDirInfo.Value) { packageName = resourceDirectoryTree[2]; } SystemManager.RunWithElevatedPrivilegeDelegate elevDelegate = (p) => { //get the resource file manager depending on its type IFileManager resourceFilesManager = this.GetResourceFileManager(resourceFolder); if (resourceFilesManager != null) { switch (changeType) { case Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Created: resourceFilesManager.FileAdded(fileName, filePath, packageName); break; case Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Deleted: resourceFilesManager.FileDeleted(filePath); break; case Telerik.Sitefinity.Frontend.FilesMonitoring.FileChangeTypes.Renamed: resourceFilesManager.FileRenamed(fileName, oldFileName, filePath, oldFilePath, packageName); break; } } }; SystemManager.RunWithElevatedPrivilege(elevDelegate); } }