|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using Microsoft.AspNetCore.Components.RenderTree; |
| 3 | +using Microsoft.Extensions.FileProviders; |
| 4 | +using Microsoft.Extensions.Primitives; |
| 5 | +using Microsoft.JSInterop; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Reflection; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace BlazorEmbedLibrary |
| 14 | +{ |
| 15 | + public class BlazorFileProvider : IFileProvider |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Displays a list of the embedded files for each assembly and extra console logging. |
| 19 | + /// </summary> |
| 20 | + public bool Debug { get; set; } = false; |
| 21 | + /// <summary> |
| 22 | + /// Allows multiple Assemblies to be passed as a list e.g. Assemblies=@ListOfAssemblies (where ListOfAssemblies is List<Assembly> |
| 23 | + /// </summary> |
| 24 | + private List<Assembly> Assemblies { get; set; } |
| 25 | + |
| 26 | + private Dictionary<string, Assembly> fileMap; |
| 27 | + private Dictionary<string, Assembly> assMap; |
| 28 | + private Dictionary<string, string> nameMap; |
| 29 | + public BlazorFileProvider(List<Assembly> assemblies) |
| 30 | + { |
| 31 | + Assemblies = assemblies ?? new List<Assembly>(); |
| 32 | + fileMap = new Dictionary<string, Assembly>(); |
| 33 | + assMap = new Dictionary<string, Assembly>(); |
| 34 | + nameMap = new Dictionary<string, string>(); |
| 35 | + LoadEmbeddedResources(); |
| 36 | + } |
| 37 | + private void LoadEmbeddedResources() |
| 38 | + { |
| 39 | + foreach (var assembly in Assemblies) |
| 40 | + { |
| 41 | + string name = assembly.GetName().Name; |
| 42 | + assMap.Add(name, assembly); |
| 43 | + foreach (var item in ListEmbeddedResources(assembly)) |
| 44 | + { |
| 45 | + string key = Path.GetFileName(item.Replace(":","/")); |
| 46 | + if (nameMap.ContainsKey(key)) |
| 47 | + { |
| 48 | + DebugLog($"BFP: Duplicate resource - unable to add {key} from {name}"); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + fileMap.Add(key, assembly); |
| 53 | + nameMap.Add(key, item); |
| 54 | + DebugLog($"BFP: Mapped {name}.{item} as {key}"); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void DebugLog(string message) |
| 61 | + { |
| 62 | + if (Debug) Console.WriteLine(message); |
| 63 | + } |
| 64 | + |
| 65 | + private IEnumerable<string> ListEmbeddedResources(Assembly assembly) |
| 66 | + { |
| 67 | + var resources = assembly.GetManifestResourceNames(); |
| 68 | + DebugLog($"Got resources: {string.Join(", ", resources)}"); |
| 69 | + DebugLog($"Using assembly: {assembly.GetName().Name}"); |
| 70 | + foreach (var item in resources) |
| 71 | + { |
| 72 | + yield return item; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public IFileInfo GetFileInfo(string subpath) |
| 77 | + { |
| 78 | + DebugLog($"BFP: GetFileInfo({subpath})"); |
| 79 | + var key = Path.GetFileName(subpath); |
| 80 | + if (nameMap.ContainsKey(key)) |
| 81 | + { |
| 82 | + return new BlazorFileInfo(nameMap[key], key, fileMap[key]); |
| 83 | + } |
| 84 | + return new NotFoundFileInfo(subpath); |
| 85 | + } |
| 86 | + |
| 87 | + public IDirectoryContents GetDirectoryContents(string subpath) |
| 88 | + { |
| 89 | + DebugLog($"BFP: GetDirectoryContents({subpath})"); |
| 90 | + var parts = subpath.Split('/'); |
| 91 | + string root; |
| 92 | + if (string.IsNullOrEmpty(parts[0]) && parts.Length>2) |
| 93 | + { |
| 94 | + root = parts[1]; |
| 95 | + } else |
| 96 | + { |
| 97 | + root = parts[0]; |
| 98 | + } |
| 99 | + |
| 100 | + if (root.Equals("_content")) |
| 101 | + { |
| 102 | + var name = parts[parts.Length-1]; |
| 103 | + return new BlazorDirectoryContents(assMap[name]); |
| 104 | + |
| 105 | + } |
| 106 | + return new NotFoundDirectoryContents(); |
| 107 | + } |
| 108 | + |
| 109 | + public IChangeToken Watch(string filter) |
| 110 | + { |
| 111 | + throw new NotImplementedException(); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments