-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated README Bumped version to 0.1.0-beta-4
- Loading branch information
1 parent
ca4fa22
commit a6de699
Showing
8 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.Extensions.FileProviders; | ||
|
||
namespace BlazorEmbedLibrary | ||
{ | ||
internal class BlazorDirectoryContents : IDirectoryContents | ||
{ | ||
private readonly Assembly assembly; | ||
|
||
public BlazorDirectoryContents(Assembly assembly) | ||
{ | ||
this.assembly = assembly; | ||
} | ||
public bool Exists => true; | ||
|
||
public IEnumerator<IFileInfo> GetEnumerator() | ||
{ | ||
var resources = assembly.GetManifestResourceNames(); | ||
foreach (var item in resources) | ||
{ | ||
var name = Path.GetFileName(item.Replace(":", "/")); | ||
yield return new BlazorFileInfo(item, name, assembly); | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.FileProviders; | ||
|
||
namespace BlazorEmbedLibrary | ||
{ | ||
internal class BlazorFileInfo : IFileInfo | ||
{ | ||
private readonly string subpath; | ||
private readonly string filename; | ||
private readonly Assembly assembly; | ||
public BlazorFileInfo(string path, string name, Assembly assembly) | ||
{ | ||
this.subpath = path; | ||
this.filename = name; | ||
this.assembly = assembly; | ||
} | ||
|
||
public bool Exists => true; | ||
|
||
public long Length => assembly.GetManifestResourceStream(subpath).Length; | ||
|
||
public string PhysicalPath => $"/{filename}"; | ||
|
||
public string Name => filename; | ||
|
||
public DateTimeOffset LastModified => DateTimeOffset.FromFileTime( int.Parse(assembly.GetName().Version.ToString().Replace(".",""))); | ||
|
||
public bool IsDirectory => false; | ||
|
||
public Stream CreateReadStream() | ||
{ | ||
return assembly.GetManifestResourceStream(subpath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.RenderTree; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace BlazorEmbedLibrary | ||
{ | ||
public class BlazorFileProvider : IFileProvider | ||
{ | ||
/// <summary> | ||
/// Displays a list of the embedded files for each assembly and extra console logging. | ||
/// </summary> | ||
public bool Debug { get; set; } = false; | ||
/// <summary> | ||
/// Allows multiple Assemblies to be passed as a list e.g. Assemblies=@ListOfAssemblies (where ListOfAssemblies is List<Assembly> | ||
/// </summary> | ||
private List<Assembly> Assemblies { get; set; } | ||
|
||
private Dictionary<string, Assembly> fileMap; | ||
private Dictionary<string, Assembly> assMap; | ||
private Dictionary<string, string> nameMap; | ||
public BlazorFileProvider(List<Assembly> assemblies) | ||
{ | ||
Assemblies = assemblies ?? new List<Assembly>(); | ||
fileMap = new Dictionary<string, Assembly>(); | ||
assMap = new Dictionary<string, Assembly>(); | ||
nameMap = new Dictionary<string, string>(); | ||
LoadEmbeddedResources(); | ||
} | ||
private void LoadEmbeddedResources() | ||
{ | ||
foreach (var assembly in Assemblies) | ||
{ | ||
string name = assembly.GetName().Name; | ||
assMap.Add(name, assembly); | ||
foreach (var item in ListEmbeddedResources(assembly)) | ||
{ | ||
string key = Path.GetFileName(item.Replace(":","/")); | ||
if (nameMap.ContainsKey(key)) | ||
{ | ||
DebugLog($"BFP: Duplicate resource - unable to add {key} from {name}"); | ||
} | ||
else | ||
{ | ||
fileMap.Add(key, assembly); | ||
nameMap.Add(key, item); | ||
DebugLog($"BFP: Mapped {name}.{item} as {key}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void DebugLog(string message) | ||
{ | ||
if (Debug) Console.WriteLine(message); | ||
} | ||
|
||
private IEnumerable<string> ListEmbeddedResources(Assembly assembly) | ||
{ | ||
var resources = assembly.GetManifestResourceNames(); | ||
DebugLog($"Got resources: {string.Join(", ", resources)}"); | ||
DebugLog($"Using assembly: {assembly.GetName().Name}"); | ||
foreach (var item in resources) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
|
||
public IFileInfo GetFileInfo(string subpath) | ||
{ | ||
DebugLog($"BFP: GetFileInfo({subpath})"); | ||
var key = Path.GetFileName(subpath); | ||
if (nameMap.ContainsKey(key)) | ||
{ | ||
return new BlazorFileInfo(nameMap[key], key, fileMap[key]); | ||
} | ||
return new NotFoundFileInfo(subpath); | ||
} | ||
|
||
public IDirectoryContents GetDirectoryContents(string subpath) | ||
{ | ||
DebugLog($"BFP: GetDirectoryContents({subpath})"); | ||
var parts = subpath.Split('/'); | ||
string root; | ||
if (string.IsNullOrEmpty(parts[0]) && parts.Length>2) | ||
{ | ||
root = parts[1]; | ||
} else | ||
{ | ||
root = parts[0]; | ||
} | ||
|
||
if (root.Equals("_content")) | ||
{ | ||
var name = parts[parts.Length-1]; | ||
return new BlazorDirectoryContents(assMap[name]); | ||
|
||
} | ||
return new NotFoundDirectoryContents(); | ||
} | ||
|
||
public IChangeToken Watch(string filter) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
} | ||
} |