public IActionResult FileTreeFiles(string dir) { if (string.IsNullOrEmpty(dir)) { dir = "/"; } if (!dir.StartsWith(LyniconSystem.Instance.Settings.FileManagerRoot)) { return(new HttpStatusCodeResult(403, "Cannot access this directory")); } IDirectoryContents di = hosting.WebRootFileProvider.GetDirectoryContents(dir); var output = new { dir = !di.Exists ? null : dir + (dir[dir.Length - 1] == '/' ? "" : "/"), dirs = !di.Exists ? null : di.Where(fi => fi.IsDirectory) .OrderBy(cdi => cdi.Name) .Select(cdi => new { name = cdi.Name }).ToArray(), files = !di.Exists ? null : di.Where(fi => !fi.IsDirectory) .OrderBy(cfi => cfi.Name) .Select(cfi => new { name = cfi.Name, ext = cfi.Name.LastAfter("."), size = cfi.Length }).OrderBy(inf => inf.name).ToArray() }; return(Json(output)); }
public IActionResult FileTreeFolders(string dir) { if (string.IsNullOrEmpty(dir)) { dir = "/"; } if (!dir.StartsWith(LyniconSystem.Instance.Settings.FileManagerRoot)) { return(new HttpStatusCodeResult(403, "Cannot access this directory")); } IDirectoryContents di = hosting.WebRootFileProvider.GetDirectoryContents(dir); var output = di .Where(fi => fi.IsDirectory) .OrderBy(cdi => cdi.Name) .Select(cdi => new { data = new { title = cdi.Name }, state = "closed", attr = new { title = dir + cdi.Name + "/" } }).ToArray(); return(Json(output)); }
public List <Widget> RegisterThemeWidgets(IMvcBuilder mvcBuilder, IServiceCollection services, IServiceProvider serviceProvider, IDirectoryContents themes) { var widgets = new List <Widget>(); foreach (var themeFolder in themes.Where(x => x.IsDirectory)) { if (ThemeHelper.ActiveTheme.ThemeId == themeFolder.Name) { var assembly = _themeDlls.Where(x => x.ManifestModule.Name == themeFolder.Name + ".dll").FirstOrDefault(); if (assembly != null) { var widgetTypeList = assembly.GetTypes().Where(x => typeof(Widget).IsAssignableFrom(x)).ToList(); foreach (var widgetType in widgetTypeList) { //var widgetInstance = (IWidget)Activator.CreateInstance(widgetType); var widgetInstance = (Widget)serviceProvider.GetService(widgetType); widgets.Add(widgetInstance); ThemeHelper.ActiveTheme.Widgets.Add(widgetInstance); GlobalContext.Widgets.Add(widgetInstance); GlobalContext.WidgetTypes.Add(widgetInstance.WidgetId, widgetType); } } } } return(widgets); }
private async Task <DocumentBase> GetDocumentFromMetadataAsync(IFileInfo item, IDirectoryContents contents, string basePath) { DocumentBase doc; var fileName = item.Name.Replace(".md", ".metadata.json"); // _logger.LogInformation($"fileName: {fileName}"); var metadataFile = contents.Where(x => x.Name == fileName).FirstOrDefault(); if (metadataFile != null) { using (var stream = metadataFile.CreateReadStream()) using (var sr = new StreamReader(stream)) { var content = await sr.ReadToEndAsync(); doc = this._options.Value.Factory.Create(content); } doc.FileName = item.Name; doc.MetadataMissing = false; } else { doc = this._options.Value.Factory.Create(); doc.Title = item.Name; doc.FileName = item.Name; doc.MetadataMissing = true; } doc.Path = basePath.Replace("\\", "/"); return(doc); }
public bool AddRSSInfo(RSSViewModel vm) { //pubdate format from http://www.dotnetperls.com/pubdate IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); if (!contents.Any(c => c.Name == "feed.xml")) { if (!CreateXMLFile()) { return(false); } } XDocument xdoc; DateTime now = DateTime.Now; if (!XmlFilePopulated()) { xdoc = new XDocument( new XElement("rss", new XAttribute("version", "2.0"), new XElement("channel", new XElement("title", vm.Title), new XElement("description", vm.Description), new XElement("link", vm.Link), new XElement("pubDate", now.ToString("ddd',' d MMM yyyy HH':'mm':'ss") + " " + now.ToString("zzzz").Replace(":", "")), new XElement("lastBuildDate", now.ToString("ddd',' d MMM yyyy HH':'mm':'ss") + " " + now.ToString("zzzz").Replace(":", "")), new XElement("ttl", vm.TimeToLive)))); } else { xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); xdoc.Descendants("title").FirstOrDefault()?.SetValue(vm.Title); xdoc.Descendants("description").FirstOrDefault()?.SetValue(vm.Description); xdoc.Descendants("link").FirstOrDefault()?.SetValue(vm.Link); xdoc.Descendants("lastBuildDate").FirstOrDefault()?.SetValue(now.ToString("ddd',' d MMM yyyy HH':'mm':'ss") + " " + now.ToString("zzzz").Replace(":", "")); xdoc.Descendants("ttl").FirstOrDefault()?.SetValue(vm.TimeToLive); } try { using (var fileStream = new FileStream(_appEnvironment.ContentRootPath + "\\wwwroot\\content\\feed.xml", FileMode.Create)) { xdoc.Save(fileStream); } } catch (Exception e) { return(false); } return(true); }
private IEnumerable <IFileInfo> FilterViewFiles(IDirectoryContents directory) { return(directory .Where(f => !f.IsDirectory && !Contains(f.Name, "_ViewStart") && !Contains(f.Name, "_ViewImports") && f.Name.EndsWith(FILE_EXTENSION, StringComparison.OrdinalIgnoreCase))); }
public void RegisterThemes(IMvcBuilder mvcBuilder, IServiceCollection services, IServiceProvider serviceProvider, IDirectoryContents themes) { _themeDlls = new List <Assembly>(); foreach (var themeFolder in themes.Where(x => x.IsDirectory)) { try { var binFolder = new DirectoryInfo(Path.Combine(themeFolder.PhysicalPath, "bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories)) { Assembly assembly; try { assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName); } catch (FileLoadException ex) { continue; } catch (BadImageFormatException ex) { continue; } if (assembly.FullName.Contains(themeFolder.Name)) { _themeDlls.Add(assembly); if (ThemeHelper.ActiveTheme.Folder == themeFolder.Name) { mvcBuilder.AddApplicationPart(assembly); var widgetTypeList = assembly.GetTypes().Where(x => typeof(Widget).IsAssignableFrom(x)).ToList(); foreach (var widgetType in widgetTypeList) { services.AddTransient(widgetType); } } } } } catch (Exception ex) { throw new Exception("Could not load theme from " + themeFolder); } } mvcBuilder.AddRazorOptions(o => { foreach (var theme in _themeDlls) { o.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(theme.Location)); } }); }
private bool GetTemplateDictionary( IDirectoryContents templateDirectory, out IDictionary <string, TemplateData> templates ) { bool correct = true; templates = new Dictionary <string, TemplateData>(StringComparer.InvariantCultureIgnoreCase); foreach (var subdir in templateDirectory.Where(c => c.IsDirectory)) { try { if (subdir.Name.Contains("_")) { logger.LogError("The name of the template directory '{SubdirectoryName}' contains a underscore.", subdir.Name); correct = false; } else { using var subDirFileProvider = new PhysicalFileProvider(subdir.PhysicalPath); foreach ( var file in subDirFileProvider.GetDirectoryContents("") .Where(f => !f.IsDirectory && f.Name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) ) { try { var match = Regex.Match(file.Name, @"^(.*?_((?:-|\+)?(?:0|[0-9]\d*)))\.xml$", RegexOptions.IgnoreCase); if (match.Success) { var key = $"{subdir.Name}_{match.Groups[1].Value}"; using var fileStream = file.CreateReadStream(); templates[key] = GetTemplateData(key, match.Groups[2].Value, fileStream); } else { logger.LogError("The file name of {subDirName}\\{fileName} doesn't end with an unix timestamp", subdir.Name, file.Name); correct = false; } } catch (Exception e) { logger.LogError(e, "Error in template file {subDirName}\\{fileName}", subdir.Name, file.Name); correct = false; } } } } catch (Exception e) { logger.LogError(e, "Error in template directory {subDirName}\\.", subdir.Name); correct = false; } } return(correct); }
public List <IModule> LoadModules(IDirectoryContents moduleRootFolder) { foreach (var moduleFolder in moduleRootFolder.Where(x => x.IsDirectory)) { try { var binFolder = new DirectoryInfo(Path.Combine(moduleFolder.PhysicalPath, "bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories)) { Assembly assembly; try { assembly = NccAssemblyLoader.LoadFromFileName(file.FullName); } catch (FileLoadException ex) { continue; } catch (BadImageFormatException ex) { continue; } if (assembly.FullName.Contains(moduleFolder.Name)) { if (modules.Count(x => x.Folder == moduleFolder.Name && x.Path == moduleFolder.PhysicalPath) == 0) { modules.Add(new Module { Folder = moduleFolder.Name, Assembly = assembly, Path = moduleFolder.PhysicalPath }); } } else { DependencyContextLoader.Default.Load(assembly); } } LoadModuleDependencies(moduleFolder); } catch (Exception ex) { throw new Exception("Could not load module from " + moduleFolder); } } GlobalContext.SetModuleDependencies(_moduleDependencies); return(modules); }
public void LoadModules(IDirectoryContents moduleRootFolder, ILogger logger) { _logger = logger; foreach (var moduleFolder in moduleRootFolder.Where(x => x.IsDirectory).ToList()) { try { var binFolder = new DirectoryInfo(Path.Combine(moduleFolder.PhysicalPath, "bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories)) { Assembly assembly; try { assembly = NccAssemblyLoader.LoadFromFileName(file.FullName); } catch (FileLoadException ex) { _logger.LogWarning(ex.Message, ex); continue; } catch (BadImageFormatException ex) { _logger.LogWarning(ex.Message, ex); continue; } if (assembly.FullName.Contains(moduleFolder.Name)) { if (modules.Count(x => x.Folder == moduleFolder.Name && x.Path == moduleFolder.PhysicalPath) == 0) { modules.Add(new Module { ExecutionOrder = 100, ModuleName = moduleFolder.Name, Folder = moduleFolder.Name, Assembly = assembly, Path = moduleFolder.PhysicalPath }); } } else { DependencyContextLoader.Default.Load(assembly); } } } catch (Exception ex) { _logger.LogWarning("Could not load module from " + moduleFolder); _logger.LogError(ex.Message, ex); //throw new Exception("Could not load module from " + moduleFolder); } } }
public ViewResult Index() { var samplePaths = _paths.Where(p => p.Name.EndsWith(".java")).ToList(); var source = System.IO.File.ReadAllText(samplePaths.Single(s => s.Name == DefaultSample).PhysicalPath); var vm = new HomeViewModel { SampleNames = samplePaths.Select(p => p.Name).ToList(), Item = Parse(source, 1000) }; return(View(vm)); }
/// <summary> /// Gets a directory listing from the embedded files in the Hood assembly. /// WARNING, this should only be used for loading files in known definite locations. as the /// file provider uses a flat structure, sub-directories will be returned along with the files. /// </summary> /// <param name="basePath">The base path in the form ~/path/of/the/file.extension</param> /// <returns></returns> public static string[] GetFiles(string basePath) { basePath = ReWritePath(basePath); EmbeddedFileProvider provider = GetProvider(Engine.Services.Resolve <IConfiguration>()); if (provider == null) { return(new List <string>().ToArray()); } IDirectoryContents contents = provider.GetDirectoryContents(""); System.Collections.Generic.IEnumerable <IFileInfo> dir = contents.Where(p => p.Name.StartsWith(basePath)); return(dir.Select(f => f.Name.Replace(basePath, "")).ToArray()); }
public static void RegisterThemes(IMvcBuilder mvcBuilder, IServiceCollection services, IDirectoryContents themes) { var themeDlls = new List <Assembly>(); foreach (var themeFolder in themes.Where(x => x.IsDirectory)) { try { var binFolder = new DirectoryInfo(Path.Combine(themeFolder.PhysicalPath, "bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories)) { Assembly assembly; try { assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName); } catch (FileLoadException ex) { continue; } catch (BadImageFormatException ex) { continue; } if (assembly.FullName.Contains(themeFolder.Name)) { themeDlls.Add(assembly); } } } catch (Exception ex) { throw new Exception("Could not load module from " + themeFolder); } } mvcBuilder.AddRazorOptions(o => { foreach (var module in themeDlls) { o.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(module.Location)); } }); }
public void PopulateRSSInfoViewModel(RSSViewModel vm) { IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); if (!contents.Any(c => c.Name == "feed.xml")) { CreateXMLFile(); vm.RSSCreated = false; return; } if (!XmlFilePopulated()) { return; } XDocument xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); if (xdoc != null) { vm.Title = xdoc.Descendants("title").FirstOrDefault()?.Value; vm.Description = xdoc.Descendants("description").FirstOrDefault()?.Value; vm.Link = xdoc.Descendants("link").FirstOrDefault()?.Value; string pubDate = xdoc.Descendants("pubDate").FirstOrDefault()?.Value; DateTime pubDateTime; if (DateTime.TryParse(pubDate, out pubDateTime)) { vm.PublishedDate = pubDateTime; } string updateDate = xdoc.Descendants("lastBuildDate").FirstOrDefault()?.Value; DateTime updateDateTime; if (DateTime.TryParse(updateDate, out updateDateTime)) { vm.UpdatedDate = updateDateTime; } int ttl; if (int.TryParse(xdoc.Descendants("ttl").FirstOrDefault()?.Value, out ttl)) { vm.TimeToLive = ttl; } vm.RSSCreated = true; } }
public void PopulateRSSItemsViewModel(RSSItemsViewModel vm) { List <RSSItemViewModel> vmList = new List <RSSItemViewModel>(); IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); if (!contents.Any(c => c.Name == "feed.xml")) { if (!CreateXMLFile()) { return; } } if (!XmlFilePopulated()) { return; } XDocument xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); if (xdoc != null) { List <XElement> elements = xdoc.Descendants("item").ToList(); foreach (var element in elements) { RSSItemViewModel tempVm = new RSSItemViewModel(); tempVm.Title = element.Descendants("title").FirstOrDefault()?.Value; tempVm.Description = element.Descendants("description").FirstOrDefault()?.Value; tempVm.Link = element.Descendants("link").FirstOrDefault()?.Value; string pubDate = element.Descendants("pubDate").FirstOrDefault()?.Value; DateTime pubDateTime; if (DateTime.TryParse(pubDate, out pubDateTime)) { tempVm.PublishedDate = pubDateTime; } tempVm.GUID = element.Descendants("guid").FirstOrDefault()?.Value; vmList.Add(tempVm); } } vm.RSSItems = vmList; }
public void PopulateEditRSSItemViewModel(EditRSSItemViewModel vm, string id) { RSSItemViewModel itemVm = new RSSItemViewModel(); if (string.IsNullOrEmpty(id)) { return; } IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); if (!contents.Any(c => c.Name == "feed.xml")) { if (!CreateXMLFile()) { return; } } if (!XmlFilePopulated()) { return; } XDocument xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); if (xdoc != null) { var eleList = xdoc.Descendants("item").ToList(); var item = eleList.Where(c => c.Element("guid").Value == id).FirstOrDefault(); if (item != null) { itemVm.Title = item.Descendants("title").FirstOrDefault()?.Value; itemVm.Description = item.Descendants("description").FirstOrDefault()?.Value; itemVm.Link = item.Descendants("link").FirstOrDefault()?.Value; itemVm.GUID = item.Descendants("guid").FirstOrDefault()?.Value; } } vm.RSSItem = itemVm; }
public List <IModule> LoadModules(IDirectoryContents moduleRootFolder) { foreach (var moduleFolder in moduleRootFolder.Where(x => x.IsDirectory)) { try { var binFolder = new DirectoryInfo(Path.Combine(moduleFolder.PhysicalPath, "bin")); if (!binFolder.Exists) { continue; } foreach (var file in binFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories)) { Assembly assembly; try { assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName); } catch (FileLoadException ex) { continue; } catch (BadImageFormatException ex) { continue; } if (assembly.FullName.Contains(moduleFolder.Name)) { modules.Add(new Module { Folder = moduleFolder.Name, Assembly = assembly, Path = moduleFolder.PhysicalPath }); } } } catch (Exception ex) { throw new Exception("Could not load module from " + moduleFolder); } } return(modules); }
private IEnumerable <IFileInfo> GetLanguageFiles() { var result = new List <IFileInfo>(); if (_fileSourceFolder is not null && _fileSourceFolder.Exists) { result.AddRange( new PhysicalDirectoryContents(_fileSourceFolder.FullName) .Where(x => !x.IsDirectory && x.Name.EndsWith(".xml")) ); } if (_directoryContents.Exists) { result.AddRange( _directoryContents .Where(x => !x.IsDirectory && x.Name.EndsWith(".xml")) ); } return(result); }
public IActionResult Index() { string webRootPath = _hostingEnvironment.WebRootPath;// + "\\Widges"; IFileProvider provider = new PhysicalFileProvider(webRootPath); IDirectoryContents contents = provider.GetDirectoryContents(""); // the applicationRoot contents var result = contents.Where(l => l.Name == "Widgets").FirstOrDefault(); string wtg = result.PhysicalPath.Substring(result.PhysicalPath.Length - 8); IFileInfo fileInfo = provider.GetFileInfo(wtg); // a file under applicationRoot NOT needed right now; var wid = provider.GetDirectoryContents(wtg); List <string> availableWidgets = new List <string>(); foreach (IFileInfo Widget in wid) { availableWidgets.Add(Widget.Name); } string contentRootPath = _hostingEnvironment.ContentRootPath; Response.Cookies.Append("Name", "Jason Groarke"); //Creates cookies here; return(View(availableWidgets)); }
private bool XmlFilePopulated() { IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); try { XDocument xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); if (xdoc != null) { if (xdoc.Descendants("title").Count() > 0) { return(true); } } } catch (Exception e) { return(false); } return(false); }
public static IList <IFileCultureInfo> FindFiles( string directory, string extension, ICultureExpression requestedCulture, IHostingEnvironment env) { string filePath; string targetName; string fileNameWithoutCulture; string nameNoExt; string culture; string filter; string relativeParent; int subLength; int lastIndex; string endsWithFilter = $".{extension}"; IEnumerable <IFileInfo> files; IEnumerable <IFileInfo> dirs; IFileProvider provider = env.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents(directory); List <IFileCultureInfo> result = new List <IFileCultureInfo>(); SortedSet <string> models = new SortedSet <string>(); if (requestedCulture.IsAllRegion) { // find by name // **.en.ext culture = requestedCulture.Language; filter = $".{culture}.{extension}"; files = contents.Where(x => !x.IsDirectory && x.Name.EndsWith(filter)); subLength = filter.Length; foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{directory}/{targetName}"; // Path.Combine(parentPath, fileName) fileNameWithoutCulture = targetName.Substring(0, targetName.Length - subLength); result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, requestedCulture)); models.Add(fileNameWithoutCulture); } // try find directory named with language // en/**.ext IFileInfo dir = contents.FirstOrDefault(x => x.IsDirectory && x.Name.Equals(culture)); if (dir != null) { relativeParent = $"{directory}/{culture}"; IDirectoryContents directoryContents = provider.GetDirectoryContents(relativeParent); files = directoryContents.Where(x => !x.IsDirectory && x.Name.EndsWith(endsWithFilter)); foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{relativeParent}/{targetName}";// Path.Combine(relatedParent, fileName) fileNameWithoutCulture = Path.GetFileNameWithoutExtension(targetName); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, requestedCulture)); models.Add(fileNameWithoutCulture); } } } // find the regions having the same language // **.en-**.ext filter = $@"\.{culture}-[a-zA-Z]{{2}}\.{extension}$"; Regex reg = new Regex(filter); files = contents.Where(x => !x.IsDirectory && reg.IsMatch(x.Name)); foreach (IFileInfo file in files) { targetName = file.Name; nameNoExt = Path.GetFileNameWithoutExtension(targetName); lastIndex = nameNoExt.LastIndexOf('.'); fileNameWithoutCulture = targetName.Substring(0, lastIndex); filePath = $"{directory}/{targetName}"; //Path.Combine(parentPath, fileName) culture = nameNoExt.Substring(lastIndex + 1); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, culture.ParseCultureExpression())); models.Add(fileNameWithoutCulture); } } // try find directory named with regions having the same language // en-**/**.ext filter = $"^{culture}-[a-zA-Z]{{2}}$"; reg = new Regex(filter); dirs = contents.Where(x => x.IsDirectory && reg.IsMatch(x.Name)); foreach (IFileInfo langDir in dirs) { culture = langDir.Name; ICultureExpression cultureExp = culture.ParseCultureExpression(); relativeParent = $"{directory}/{culture}"; //Path.Combine(parentPath, culture) IDirectoryContents directoryContents = provider.GetDirectoryContents(relativeParent); files = directoryContents.Where(x => !x.IsDirectory && x.Name.EndsWith(endsWithFilter)); foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{relativeParent}/{targetName}"; //Path.Combine(relatedParent, fileName) fileNameWithoutCulture = Path.GetFileNameWithoutExtension(targetName); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, cultureExp)); models.Add(fileNameWithoutCulture); } } } } else { // find by name // **.en-US.ext culture = requestedCulture.DisplayName; filter = $".{culture}.{extension}"; files = contents.Where(x => !x.IsDirectory && x.Name.EndsWith(filter)); subLength = filter.Length; foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{directory}/{targetName}"; //Path.Combine(parentPath, fileName) fileNameWithoutCulture = targetName.Substring(0, targetName.Length - subLength); result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, requestedCulture)); models.Add(fileNameWithoutCulture); } // try find directory named with culture name // en-US/**.ext dirs = contents.Where(x => x.IsDirectory && x.Name.Equals(culture)); foreach (IFileInfo langDir in dirs) { relativeParent = $"{directory}/{culture}"; //Path.Combine(parentPath, culture) IDirectoryContents directoryContents = provider.GetDirectoryContents(relativeParent); files = directoryContents.Where(x => !x.IsDirectory && x.Name.EndsWith(endsWithFilter)); foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{relativeParent}/{targetName}"; //Path.Combine(relatedParent, fileName) fileNameWithoutCulture = Path.GetFileNameWithoutExtension(targetName); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, requestedCulture)); models.Add(fileNameWithoutCulture); } } } // find the regions having the same language // **.en.ext culture = requestedCulture.Language; ICultureExpression cultureExp = culture.ParseCultureExpression(); filter = $".{culture}.{extension}"; files = contents.Where(x => !x.IsDirectory && x.Name.EndsWith(filter)); subLength = filter.Length; foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{directory}/{targetName}"; //Path.Combine(parentPath, fileName) fileNameWithoutCulture = targetName.Substring(0, targetName.Length - subLength); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, cultureExp)); models.Add(fileNameWithoutCulture); } } // try find directory named with the same language // en/**.ext relativeParent = $"{directory}/{culture}"; //Path.Combine(parentPath, culture) IFileInfo dir = contents.FirstOrDefault(x => x.IsDirectory && x.Name.Equals(culture)); if (dir != null) { IDirectoryContents directoryContents = provider.GetDirectoryContents(relativeParent); files = directoryContents.Where(x => !x.IsDirectory && x.Name.EndsWith(endsWithFilter)); foreach (IFileInfo file in files) { targetName = file.Name; filePath = $"{relativeParent}/{targetName}"; //Path.Combine(relatedParent, fileName) fileNameWithoutCulture = Path.GetFileNameWithoutExtension(targetName); if (!models.Contains(fileNameWithoutCulture)) { result.Add(new FileCultureInfo(filePath, targetName, fileNameWithoutCulture, extension, cultureExp)); models.Add(fileNameWithoutCulture); } } } } return(result); }
private bool ModifyRSSItem(ModifyType type, RSSItemViewModel vm, string id) { IFileProvider provider = _appEnvironment.ContentRootFileProvider; IDirectoryContents contents = provider.GetDirectoryContents("wwwroot\\content"); if (!contents.Any(c => c.Name == "feed.xml")) { if (!CreateXMLFile()) { return(false); } } if (!XmlFilePopulated()) { return(false); } XDocument xdoc = XDocument.Load(contents.Where(c => c.Name == "feed.xml").FirstOrDefault().CreateReadStream()); var eleList = xdoc.Descendants("item").ToList(); var ttlEle = xdoc.Descendants("ttl").LastOrDefault(); if (ttlEle == null) { return(false); } DateTime now = DateTime.Now; if (type == ModifyType.Create) { if (eleList.Count() == 0) { //No RSS Items found. xdoc.Descendants("ttl").LastOrDefault()?.AddAfterSelf(new XElement("item", new XElement("title", vm.Title), new XElement("description", vm.Description), new XElement("link", vm.Link), new XElement("guid", new XAttribute("isPermaLink", "true"), Guid.NewGuid().ToString()), new XElement("pubDate", now.ToString("ddd',' d MMM yyyy HH':'mm':'ss") + " " + now.ToString("zzzz").Replace(":", "")))); } else { xdoc.Descendants("item").LastOrDefault()?.AddAfterSelf(new XElement("item", new XElement("title", vm.Title), new XElement("description", vm.Description), new XElement("link", vm.Link), new XElement("guid", new XAttribute("isPermaLink", "true"), Guid.NewGuid().ToString()), new XElement("pubDate", now.ToString("ddd',' d MMM yyyy HH':'mm':'ss") + " " + now.ToString("zzzz").Replace(":", "")))); } } else if (type == ModifyType.Edit) { var item = eleList.Where(c => c.Element("guid").Value == id).FirstOrDefault(); if (item != null) { item.Descendants("title").FirstOrDefault()?.SetValue(vm.Title); item.Descendants("description").FirstOrDefault()?.SetValue(vm.Description); item.Descendants("link").FirstOrDefault()?.SetValue(vm.Link); } } else if (type == ModifyType.Delete) { var item = eleList.Where(c => c.Element("guid").Value == id).FirstOrDefault(); if (item != null) { item.Remove(); } } try { using (var fileStream = new FileStream(_appEnvironment.ContentRootPath + "\\wwwroot\\content\\feed.xml", FileMode.Create)) { xdoc.Save(fileStream); return(true); } } catch (Exception e) { return(false); } }
/// <summary> /// This will never return null nor contain any null entries /// </summary> public async Task <NonNullImmutableList <Post> > Get() { // The redirects set contains tuples From, To slugs (blank lines and those starting with a "#" are ignored, as are any that don't have any whitespace) const string redirectsFilename = "Redirects.txt"; var redirectsFile = _folder.FirstOrDefault(file => file.Name.Equals(redirectsFilename, StringComparison.OrdinalIgnoreCase)); IEnumerable <Tuple <string, string> > redirects; if (redirectsFile == null) { redirects = new List <Tuple <string, string> >(); } else { redirects = (await ReadFileContents(redirectsFile)) .Replace("\r\n", "\n") .Replace("\r", "\n") .Split('\n') .Select(entry => entry.Trim()) .Where(entry => (entry != "") && !entry.StartsWith("#") && entry.Any(c => char.IsWhiteSpace(c))) .Select(entry => new string(entry.Select(c => char.IsWhiteSpace(c) ? ' ' : c).ToArray())) .Select(entry => entry.Split(new[] { ' ' }, 2)) .Select(values => Tuple.Create(values[0], values[1])); } // The relatedPostRelationships set contains a map of Post Id to Ids of related Posts (in the order that they should appear) const string relatedPostsFilename = "RelatedPosts.txt"; var relatedPostsFile = _folder.FirstOrDefault(file => file.Name.Equals(relatedPostsFilename, StringComparison.OrdinalIgnoreCase)); var relatedPostRelationships = (relatedPostsFile == null) ? new Dictionary <int, ImmutableList <int> >() : await ReadRedirects(relatedPostsFile); // There is similar data in the AutoSuggestedRelatedPosts.txt file but the manually-created RelatedPosts.txt should take precedence in cases // where Post Ids appear in both const string autoSuggestedRelatedPostsFilename = "AutoSuggestedRelatedPosts.txt"; var autoSuggestedRelatedPostsFile = _folder.FirstOrDefault(file => file.Name.Equals(autoSuggestedRelatedPostsFilename, StringComparison.OrdinalIgnoreCase)); var autoSuggestedRelatedPostRelationships = (autoSuggestedRelatedPostsFile == null) ? new Dictionary <int, ImmutableList <int> >() : await ReadRedirects(autoSuggestedRelatedPostsFile); // We can use this functionality from the FullTextIndexer to generate the Post slug (it will replace accented characters, normalise whitespace, // remove punctuation and lower case the content - all we need to do then is replace spaces with hypens) var stringNormaliser = DefaultStringNormaliser.Instance; var posts = new List <Post>(); foreach (var file in _folder.Where(file => file.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))) { if (file.Name.Equals(redirectsFilename, StringComparison.InvariantCultureIgnoreCase)) { continue; } var fileSummary = TryToGetFileSummaryEntry(file.Name); if (fileSummary != null) { var fileContents = await ReadFileContents(file); var title = TryToGetTitle(fileContents); if (title != null) { // 2014-09-17 DWR: Titles such as "C# State Machines" were being converted into "c-state-machines" which isn't as descriptive as // I'd like, "c-sharp-state-machines" is better. The replacement is done for "C#" and "F#" (a space is required after the // replacement content otherwise the "sharp" gets rolled into the next word) var slugBase = title.Replace("C#", "c sharp ").Replace("F#", "f sharp "); var slug = stringNormaliser.GetNormalisedString(slugBase).Replace(' ', '-'); var redirectsForPost = new NonNullOrEmptyStringList( redirects.Where(r => r.Item2 == slug).Select(r => r.Item1) ); // On this pass, set every tag's NumberOfPosts value to one since we don't have enough data to know better. After all of the // posts have been loaded, this can be fixed before the method terminates. if (!relatedPostRelationships.TryGetValue(fileSummary.Id, out var relatedPosts)) { relatedPosts = null; } if ((relatedPosts != null) || !autoSuggestedRelatedPostRelationships.TryGetValue(fileSummary.Id, out var autoSuggestedRelatedPosts)) { // Only check the autoSuggestedRelatedPostRelationships if there was no relatedPostRelationships entry - this allows for posts // to be specified as having no suggestions (manually-specified or auto-suggested) by having an entry in the manually-specified // file that has the post id but zero suggestions. autoSuggestedRelatedPosts = null; } posts.Add(new Post( fileSummary.Id, fileSummary.PostDate, file.LastModified.DateTime, slug, redirectsForPost, title, fileSummary.IsHighlight, fileContents, relatedPosts ?? new ImmutableList <int>(), autoSuggestedRelatedPosts ?? new ImmutableList <int>(), fileSummary.Tags.Select(tag => new TagSummary(tag, 1)).ToNonNullImmutableList() )); } } } var tagCounts = posts .SelectMany(post => post.Tags) .Select(tag => tag.Tag) .GroupBy(tag => tag, StringComparer.OrdinalIgnoreCase) .ToDictionary(groupedTag => groupedTag.Key, groupedTag => groupedTag.Count(), StringComparer.OrdinalIgnoreCase); return(new NonNullImmutableList <Post>(posts.Select(post => new Post( post.Id, post.Posted, post.LastModified, post.Slug, post.RedirectFromSlugs, post.Title, post.IsHighlight, post.MarkdownContent, post.RelatedPosts, post.AutoSuggestedRelatedPosts, post.Tags.Select(tag => new TagSummary(tag.Tag, tagCounts[tag.Tag])).ToNonNullImmutableList() ) ))); }
public void ProcessDirectory(BundleContext context, IncludeDirectoryData includeDirectoryData, string directoryPath, ref Dictionary <string, BundleFile> files) { IEnumerable <IFileInfo> directoryFiles; IDirectoryContents directoryContent = context.FileProvider.GetDirectoryContents(directoryPath); if (directoryContent != null) { Regex regEx; switch (includeDirectoryData.PatternType) { // We used to be able to just call DirectoryInfo.GetFiles, // now we have to add support for * and {version} syntax on top of VPP case PatternType.Version: regEx = PatternHelper.BuildRegex(includeDirectoryData.SearchPattern); directoryFiles = directoryContent.Where(file => regEx.IsMatch(file.Name) && !file.IsDirectory); break; case PatternType.All: directoryFiles = directoryContent.Where(file => !file.IsDirectory); break; case PatternType.Exact: directoryFiles = directoryContent.Where(file => String.Equals(file.Name, includeDirectoryData.SearchPattern, StringComparison.OrdinalIgnoreCase) && !file.IsDirectory); break; case PatternType.Suffix: case PatternType.Prefix: default: regEx = PatternHelper.BuildWildcardRegex(includeDirectoryData.SearchPattern); directoryFiles = directoryContent.Where(file => regEx.IsMatch(file.Name) && !file.IsDirectory); break; } // Sort the directory files so we get deterministic order directoryFiles = directoryFiles.OrderBy(file => file, PhysicalFileComparer.Instance); //List<BundleFile> filterList = new List<BundleFile>(); //foreach (IFileInfo file in directoryFiles) //{ // filterList.Add(new BundleFile()); //} //files.AddRange(context.BundleCollection.DirectoryFilter.FilterIgnoredFiles(context, filterList)); foreach (IFileInfo file in directoryFiles) { files.Add(directoryPath + "/" + file.Name, new BundleFile(context.Parent) { PhysicalPath = file.PhysicalPath, VirtualPath = directoryPath + "/" + file.Name }); } // Need to recurse on subdirectories if requested if (includeDirectoryData.SearchSubdirectories) { foreach (IFileInfo subDir in directoryContent.Where(file => file.IsDirectory)) { ProcessDirectory(context, includeDirectoryData, directoryPath + "/" + subDir.Name, ref files); } } } }
private async Task InitCollection(string collectionName, IContentStore contentStore, ContentType translationsContentType, IServiceProvider serviceProvider) { var collection = (await contentStore.GetContentCollections(new ContentCollectionQuery { AppId = Constants.AppTextAdminAppId, Name = collectionName })).FirstOrDefault(); if (collection == null) { collection = new ContentCollection { ContentType = translationsContentType, Name = collectionName, ListDisplayField = TranslationConstants.TranslationTextFieldName, Version = 1 }; var contentCollectionSaveCommandHandler = serviceProvider.GetRequiredService <ICommandHandler <SaveContentCollectionCommand> >(); await contentCollectionSaveCommandHandler.Handle(new SaveContentCollectionCommand(Constants.AppTextAdminAppId, collection)); } // Ensure content var collectionContainsItems = (await contentStore.GetContentItems(new ContentItemQuery { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, First = 1 })).Length > 0; if (!collectionContainsItems) { // Read content from embedded json files and add to storage var contentFiles = _translationsContents.Where(tc => tc.Name.StartsWith($"Content.{collectionName}")); var initPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "apptextadmin-init") })); var saveContentItemCommandHandler = new SaveContentItemCommandHandler( serviceProvider.GetRequiredService <IContentStore>(), serviceProvider.GetRequiredService <IVersioner>(), serviceProvider.GetRequiredService <ContentItemValidator>(), initPrincipal, serviceProvider.GetRequiredService <IDispatcher>()); foreach (var contentFile in contentFiles) { _logger.LogInformation("Importing content from {0}", contentFile.Name); var language = contentFile.Name.Substring($"Content.{collectionName}".Length + 1).Replace(".json", String.Empty); using (var contentStream = contentFile.CreateReadStream()) using (var sr = new StreamReader(contentStream)) using (var jsonReader = new JsonTextReader(sr)) { var items = JArray.Load(jsonReader).Children <JObject>(); foreach (var item in items) { foreach (var prop in item.Properties()) { var saveContentItemCommand = new SaveContentItemCommand { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, LanguagesToValidate = new[] { language }, ContentKey = prop.Name }; var contentItem = (await contentStore.GetContentItems(new ContentItemQuery { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, ContentKey = prop.Name, First = 1 })).FirstOrDefault(); if (contentItem != null) { saveContentItemCommand.Id = contentItem.Id; saveContentItemCommand.Version = contentItem.Version; saveContentItemCommand.Content = contentItem.Content; } var contentFieldValue = contentItem != null ? JObject.FromObject(contentItem.Content[TranslationConstants.TranslationTextFieldName]) : new JObject(); contentFieldValue[language] = prop.Value; saveContentItemCommand.Content[TranslationConstants.TranslationTextFieldName] = contentFieldValue; var result = await saveContentItemCommandHandler.Handle(saveContentItemCommand); } } } } } }