public void GetDirectoryContents() { var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".Resources"); var files = provider.GetDirectoryContents(""); Assert.NotNull(files); Assert.Equal(2, files.Count()); Assert.False(provider.GetDirectoryContents("file").Exists); Assert.False(provider.GetDirectoryContents("file/").Exists); Assert.False(provider.GetDirectoryContents("file.txt").Exists); Assert.False(provider.GetDirectoryContents("file/txt").Exists); }
protected override IEnumerable <ISnippet> CreateItems(IServiceProvider factory) { var hostEnvironment = factory.GetRequiredService <IHostEnvironment>(); var embeddedSnippets = new List <ISnippet>(base.CreateItems(factory)); var snippetProvider = new EmbeddedFileProvider(typeof(IAssemblyProvider).Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets"); var embeddedFiles = snippetProvider.GetDirectoryContents(string.Empty) .Where(x => !x.IsDirectory && x.Name.EndsWith(".cshtml")); foreach (var file in embeddedFiles) { using var stream = new StreamReader(file.CreateReadStream()); embeddedSnippets.Add(new Snippet(file.Name, stream.ReadToEnd().Trim())); } var customSnippetsDir = new DirectoryInfo(hostEnvironment.MapPathContentRoot($"{Constants.SystemDirectories.Umbraco}/PartialViewMacros/Templates")); if (!customSnippetsDir.Exists) { return(embeddedSnippets); } var customSnippets = customSnippetsDir.GetFiles().Select(f => new Snippet(f.Name, File.ReadAllText(f.FullName))); var allSnippets = Merge(embeddedSnippets, customSnippets); return(allSnippets); }
protected override IEnumerable <ISnippet> CreateItems(IServiceProvider factory) { var embeddedSnippets = new List <ISnippet>(base.CreateItems(factory)); // Ignore these var filterNames = new List <string> { "Gallery", "ListChildPagesFromChangeableSource", "ListChildPagesOrderedByProperty", "ListImagesFromMediaFolder" }; var snippetProvider = new EmbeddedFileProvider(typeof(IAssemblyProvider).Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets"); var embeddedFiles = snippetProvider.GetDirectoryContents(string.Empty) .Where(x => !x.IsDirectory && x.Name.EndsWith(".cshtml")); foreach (var file in embeddedFiles) { if (!filterNames.Contains(Path.GetFileNameWithoutExtension(file.Name))) { using var stream = new StreamReader(file.CreateReadStream()); embeddedSnippets.Add(new Snippet(file.Name, stream.ReadToEnd().Trim())); } } return(embeddedSnippets); }
public IDirectoryContents GetDirectoryContents(string subpath) { if (subpath == null) { return(new NotFoundDirectoryContents()); } var resourcePath = subpath.Replace("-", "_") .Replace("~/", "") .Replace("/", ".") .Replace("\\", ".") .Trim(new char[] { '.' }); var allFiles = _embeddedFileProvider .GetDirectoryContents(string.Empty) .Where(f => f.Name.StartsWith(resourcePath, StringComparison.OrdinalIgnoreCase)) ; if (!allFiles.Any()) { return(new NotFoundDirectoryContents()); } var directoryContents = new EmbeddedDirectoryContents(resourcePath, allFiles); return(directoryContents); }
static void LoadThemes() { /* * This code relies on Blogifier.Web.csproj to have this line: * <EmbeddedResource Include="Views\Blogifier\**\Single.cshtml" /> * We need it to populate list of installed themes when project * deployed precompiled and file system not accessible */ try { var assembly = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Blogifier.Web.dll")); var provider = new EmbeddedFileProvider(assembly, assembly.GetName().Name); var resources = provider.GetDirectoryContents("/"); BlogSettings.BlogThemes = new List <SelectListItem>(); BlogSettings.BlogThemes.Add(new SelectListItem { Value = "Standard", Text = "Standard" }); foreach (var rsrc in resources) { var theme = rsrc.Name.Replace("Views.Blogifier.Themes.", "").Replace(".Single.cshtml", ""); BlogSettings.BlogThemes.Add(new SelectListItem { Text = theme, Value = theme }); } } catch { } }
static void LoadThemes() { try { var assembly = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BlogApplication.Core.dll")); var provider = new EmbeddedFileProvider(assembly, assembly.GetName().Name); var resources = provider.GetDirectoryContents("/"); BlogSettings.BlogThemes = new List <SelectListItem>(); BlogSettings.BlogThemes.Add(new SelectListItem { Value = "Standard", Text = "Standard" }); foreach (var rsrc in resources) { var theme = rsrc.Name.Replace("Views.BlogFile.Themes.", "").Replace(".Single.cshtml", ""); BlogSettings.BlogThemes.Add(new SelectListItem { Text = theme, Value = theme }); } } catch { } }
public AppTextAdminInitializer(ILogger <AppTextAdminInitializer> logger, IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; var embeddedProvider = new EmbeddedFileProvider(typeof(AppTextAdminInitializer).Assembly); _translationsContents = embeddedProvider.GetDirectoryContents(""); _logger = logger; }
public void GetDirectoryContents_ReturnsAllFilesInFileSystem(string path) { // Arrange var provider = new EmbeddedFileProvider(GetType().Assembly, Namespace + ".Resources"); // Act var files = provider.GetDirectoryContents(path); // Assert Assert.Collection(files.OrderBy(f => f.Name, StringComparer.Ordinal), file => Assert.Equal("File.txt", file.Name), file => Assert.Equal("ResourcesInSubdirectory.File3.txt", file.Name)); Assert.False(provider.GetDirectoryContents("file").Exists); Assert.False(provider.GetDirectoryContents("file/").Exists); Assert.False(provider.GetDirectoryContents("file.txt").Exists); Assert.False(provider.GetDirectoryContents("file/txt").Exists); }
public void GetDirInfo_with_no_matching_base_namespace() { var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, "Unknown.Namespace"); var files = provider.GetDirectoryContents(string.Empty); Assert.NotNull(files); Assert.True(files.Exists); Assert.Equal(0, files.Count()); }
public static IEnumerable <string> GetAllEmbeddedFile(Assembly assembly, string rootPath = "/") { var embeddedFileProvider = new EmbeddedFileProvider(assembly); var directoryContents = embeddedFileProvider.GetDirectoryContents("/"); rootPath = rootPath.TrimStart('/').Replace('/', '.').Replace('\\', '.'); return(directoryContents .Where(content => content.Name.StartsWith(rootPath, StringComparison.Ordinal)) .Select(content => content.Name)); }
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(snippetName)); } string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } var snippetProvider = new EmbeddedFileProvider(GetType().Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets"); IFileInfo?file = snippetProvider.GetDirectoryContents(string.Empty) .FirstOrDefault(x => x.Exists && x.Name.Equals(snippetName + ".cshtml")); // Try and get the snippet path if (file is null) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(file.CreateReadStream())) { var snippetContent = snippetFile.ReadToEnd().Trim(); // strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); // Update Model.Content to be Model when used as PartialView if (partialViewType == PartialViewType.PartialView) { snippetContent = snippetContent .Replace("Model.Content.", "Model.") .Replace("(Model.Content)", "(Model)"); } var content = $"{partialViewHeader}{Environment.NewLine}{snippetContent}"; return(content); } }
public static IEnumerable <IFileInfo> GetResources(this Assembly assembly, string name, bool ignoreCase = false) { List <IFileInfo> files = new List <IFileInfo>(); var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/").Where(x => x.Name.Contains(name, ignoreCase)); foreach (var resource in resources) { files.Add(resource); } return(files); }
public static IEnumerable <IFileInfo> GetResources(this Assembly assembly) { List <IFileInfo> files = new List <IFileInfo>(); var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/"); foreach (var resource in resources) { files.Add(resource); } return(files); }
public static IEnumerable <Stream> GetResourcesAsStream(this Assembly assembly, Regex regex) { List <Stream> files = new List <Stream>(); var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/").Where(x => regex.IsMatch(x.Name)); foreach (var resource in resources) { files.Add(resource.CreateReadStream()); } return(files); }
public void GetDirectoryContents_ReturnsEmptySequence_IfResourcesDoNotExistUnderNamespace() { // Arrange var provider = new EmbeddedFileProvider(GetType().Assembly, "Unknown.Namespace"); // Act var files = provider.GetDirectoryContents(string.Empty); // Assert Assert.NotNull(files); Assert.True(files.Exists); Assert.Empty(files); }
public static IEnumerable <string> GetResourcesAsString(this Assembly assembly) { List <string> files = new List <string>(); var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/"); foreach (var resource in resources) { var resourceName = $"{Assembly.GetExecutingAssembly().GetName().Name}.{resource.Name}"; files.Add(assembly.GetResourceAsString(resourceName)); } return(files); }
public void GetDirectoryContents_ReturnsNotFoundDirectoryContents_IfHierarchicalPathIsSpecified(string path) { // Arrange var provider = new EmbeddedFileProvider(GetType().Assembly); // Act var files = provider.GetDirectoryContents(path); // Assert Assert.NotNull(files); Assert.False(files.Exists); Assert.Empty(files); }
/// <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 IEnumerable <Stream> GetResourcesAsStream(this IEnumerable <Assembly> assemblies) { List <Stream> files = new List <Stream>(); foreach (var assembly in assemblies) { var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/"); foreach (var resource in resources) { files.Add(resource.CreateReadStream()); } } return(files); }
public static IEnumerable <IFileInfo> GetResources(this IEnumerable <Assembly> assemblies, Regex regex) { List <IFileInfo> files = new List <IFileInfo>(); foreach (var assembly in assemblies) { var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/").Where(x => regex.IsMatch(x.Name)); foreach (var resource in resources) { files.Add(resource); } } return(files); }
public static IEnumerable <Stream> GetResourcesAsStream(this IEnumerable <Assembly> assemblies, string name, bool ignoreCase = false) { List <Stream> files = new List <Stream>(); foreach (var assembly in assemblies) { var embedded = new EmbeddedFileProvider(assembly); var resources = embedded.GetDirectoryContents("/").Where(x => x.Name.Contains(name, ignoreCase)); foreach (var resource in resources) { files.Add(resource.CreateReadStream()); } } return(files); }
public static IEnumerable <Stream> GetResourcesAsStream(this Assembly assembly, string[] names, bool ignoreCase = false) { var rs = new List <IFileInfo>(); List <Stream> files = new List <Stream>(); var embedded = new EmbeddedFileProvider(assembly); foreach (var name in names) { var resources = embedded.GetDirectoryContents("/").Where(x => x.Name.Contains(name, ignoreCase)); rs.AddRange(resources); } foreach (var resource in rs) { files.Add(resource.CreateReadStream()); } return(files); }
public override void Run() { IFileProvider fileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly()); //目录 IDirectoryContents contents = fileProvider.GetDirectoryContents("/"); foreach (var item in contents) { Stream stream = item.CreateReadStream(); Console.WriteLine(item.Name); } //文件 IFileInfo fileInfo = fileProvider.GetFileInfo("emb.html"); Console.WriteLine(fileInfo.AsFormatJsonStr()); }
public IEnumerable <string> GetPartialViewSnippetNames(params string[] filterNames) { var snippetProvider = new EmbeddedFileProvider(this.GetType().Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets"); var files = snippetProvider.GetDirectoryContents(string.Empty) .Where(x => !x.IsDirectory && x.Name.EndsWith(".cshtml")) .Select(x => Path.GetFileNameWithoutExtension(x.Name)) .Except(filterNames, StringComparer.InvariantCultureIgnoreCase) .ToArray(); //Ensure the ones that are called 'Empty' are at the top var empty = files.Where(x => Path.GetFileName(x)?.InvariantStartsWith("Empty") ?? false) .OrderBy(x => x?.Length) .ToArray(); return(empty.Union(files.Except(empty)).WhereNotNull()); }
static void AddFileProviders(IServiceCollection services) { try { // services.Configure<MvcRazorRuntimeCompilationOptions>( services.AddControllersWithViews().AddRazorRuntimeCompilation(options => { foreach (var assembly in AppConfig.GetAssemblies(true)) { var fileProvider = new EmbeddedFileProvider(assembly, assembly.GetName().Name); // load themes from embedded provider var content = fileProvider.GetDirectoryContents(""); if (content.Exists) { foreach (var item in content) { if (item.Name.StartsWith("Views.Themes")) { if (AppConfig.EmbeddedThemes == null) { AppConfig.EmbeddedThemes = new List <string>(); } var ar = item.Name.Split('.'); if (ar.Length > 2 && !AppConfig.EmbeddedThemes.Contains(ar[2])) { if (assembly.GetName().Name.ToLower() != "app") { AppConfig.EmbeddedThemes.Add(ar[2]); } } } } } options.FileProviders.Add(fileProvider); } }); } catch { } }
public void Can_Load_Language_Xml_Files() { var readFilesCount = 0; var xmlDocument = new XmlDocument(); var languageProvider = new EmbeddedFileProvider(typeof(IAssemblyProvider).Assembly, "Umbraco.Cms.Core.EmbeddedResources.Lang"); var files = languageProvider.GetDirectoryContents(string.Empty) .Where(x => !x.IsDirectory && x.Name.EndsWith(".xml")); foreach (var languageFile in files) { using var stream = new StreamReader(languageFile.CreateReadStream()); // Load will throw an exception if the XML isn't valid. xmlDocument.Load(stream); readFilesCount++; } // Ensure that at least one file was read. Assert.AreNotEqual(0, readFilesCount); }
public IDirectoryContents GetDirectoryContents(string subpath) { if (subpath == null) { return(new NotFoundDirectoryContents()); } var resourcePath = EmbeddedResourcePathFormatter.ConvertFromVirtualDirectory(subpath); var allFiles = _embeddedFileProvider .GetDirectoryContents(string.Empty) .Where(f => f.Name.StartsWith(resourcePath, StringComparison.OrdinalIgnoreCase)) ; if (!allFiles.Any()) { return(new NotFoundDirectoryContents()); } var directoryContents = new EmbeddedDirectoryContents(resourcePath, allFiles); return(directoryContents); }
private void CopyFolderFromStream(string dest, Type type) { var fileProvider = new EmbeddedFileProvider(Assembly.GetAssembly(type)); var res = fileProvider.GetDirectoryContents("").ToList(); foreach (IFileInfo file in res) { var path = file.Name.Split(".").SkipLast(2).ToList(); var fileName = file.Name.Replace(string.Join(".", path), "").TrimStart('.'); string destName = Path.Combine(dest); path.ForEach(fp => { if (fp != DefaultRazorTemplate) { destName = Path.Combine(destName, fp); } }); var dirPath = destName; destName = Path.Combine(destName, fileName); if (!file.IsDirectory) { if (!File.Exists(destName)) { if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var fileString = new StreamReader(file.CreateReadStream()).ReadToEnd(); File.WriteAllText(destName, fileString, Encoding.UTF8); } } else { Directory.CreateDirectory(destName); } } }
public async Task <IActionResult> Get(string lang, string part, long?timestamp) { var tempLang = lang.Replace("-", "_"); var embeddedFileProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly()); var contents = embeddedFileProvider.GetDirectoryContents(""); var fileInfo = contents.FirstOrDefault( f => f.Name.Equals($"Translations.{tempLang}.{part}.json", StringComparison.InvariantCultureIgnoreCase)); if (fileInfo != null) { if (fileInfo.LastModified.Ticks > timestamp.GetValueOrDefault()) { using (var reader = new StreamReader(fileInfo.CreateReadStream())) { var trad = await reader.ReadToEndAsync(); trad = trad.Insert(1, "\"Part\" : \"" + part + "\","); object temp = JsonConvert.DeserializeObject(trad); return(this.Ok(new { TimeStamp = fileInfo.LastModified.Ticks, Translation = temp, Lang = lang, })); } } else { return(this.StatusCode(304)); } } else { return(this.NotFound()); } }
public IDirectoryContents GetDirectoryContents(string subpath) => _inner.GetDirectoryContents(subpath);
public void GetDirectoryContents_ReturnsAllFilesInFileSystem(string path) { // Arrange var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".Resources"); // Act var files = provider.GetDirectoryContents(path); // Assert Assert.Collection(files.OrderBy(f => f.Name, StringComparer.Ordinal), file => Assert.Equal("File.txt", file.Name), file => Assert.Equal("ResourcesInSubdirectory.File3.txt", file.Name)); Assert.False(provider.GetDirectoryContents("file").Exists); Assert.False(provider.GetDirectoryContents("file/").Exists); Assert.False(provider.GetDirectoryContents("file.txt").Exists); Assert.False(provider.GetDirectoryContents("file/txt").Exists); }
public void GetDirectoryContents_ReturnsEmptySequence_IfResourcesDoNotExistUnderNamespace() { // Arrange var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, "Unknown.Namespace"); // Act var files = provider.GetDirectoryContents(string.Empty); // Assert Assert.NotNull(files); Assert.True(files.Exists); Assert.Empty(files); }
public void GetDirectoryContents_ReturnsNotFoundDirectoryContents_IfHierarchicalPathIsSpecified(string path) { // Arrange var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace); // Act var files = provider.GetDirectoryContents(path); // Assert Assert.NotNull(files); Assert.False(files.Exists); Assert.Empty(files); }