Simplify in-memory file provider usage by initializing with byte[], not Stream

This commit is contained in:
Steve Sanderson 2017-12-14 15:48:01 +00:00
parent 64770bfb23
commit c72f9a1977
7 changed files with 26 additions and 27 deletions

View File

@ -16,7 +16,7 @@ namespace Microsoft.Blazor.Build.Core.FileSystem
{ {
} }
private static IEnumerable<(string, Stream)> ComputeContents( private static IEnumerable<(string, byte[])> ComputeContents(
string rootAssemblyName, string rootAssemblyName,
ReferencedAssemblyResolver resolver) ReferencedAssemblyResolver resolver)
{ {
@ -25,7 +25,7 @@ namespace Microsoft.Blazor.Build.Core.FileSystem
return foundAssemblies.Values.Select(assembly => ( return foundAssemblies.Values.Select(assembly => (
$"/{assembly.Definition.Name.Name}.dll", $"/{assembly.Definition.Name.Name}.dll",
(Stream)new MemoryStream(assembly.Data))); assembly.Data));
} }
private static void AddWithReferencesRecursive( private static void AddWithReferencesRecursive(

View File

@ -17,14 +17,13 @@ namespace Microsoft.Blazor.Build.Core.FileSystem
{ {
} }
private static IEnumerable<(string, Stream)> ComputeContents(string htmlTemplate, string assemblyName, IEnumerable<IFileInfo> binFiles) private static IEnumerable<(string, byte[])> ComputeContents(string htmlTemplate, string assemblyName, IEnumerable<IFileInfo> binFiles)
{ {
if (htmlTemplate != null) if (htmlTemplate != null)
{ {
var html = GetIndexHtmlContents(htmlTemplate, assemblyName, binFiles); var html = GetIndexHtmlContents(htmlTemplate, assemblyName, binFiles);
var htmlBytes = Encoding.UTF8.GetBytes(html); var htmlBytes = Encoding.UTF8.GetBytes(html);
var htmlStream = new MemoryStream(htmlBytes); yield return ("/index.html", htmlBytes);
yield return ("/index.html", htmlStream);
} }
} }

View File

@ -16,14 +16,24 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
{ {
} }
private static IEnumerable<(string, Stream)> ReadEmbeddedResources( private static IEnumerable<(string, byte[])> ReadEmbeddedResources(
Assembly assembly, string resourceNamePrefix) Assembly assembly, string resourceNamePrefix)
{ {
return assembly.GetManifestResourceNames() return assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(resourceNamePrefix, StringComparison.Ordinal)) .Where(name => name.StartsWith(resourceNamePrefix, StringComparison.Ordinal))
.Select(name => ( .Select(name => (
name.Substring(resourceNamePrefix.Length), name.Substring(resourceNamePrefix.Length),
assembly.GetManifestResourceStream(name))); ReadManifestResource(assembly, name)));
}
private static byte[] ReadManifestResource(Assembly assembly, string name)
{
using (var ms = new MemoryStream())
using (var resourceStream = assembly.GetManifestResourceStream(name))
{
resourceStream.CopyTo(ms);
return ms.ToArray();
}
} }
} }
} }

View File

@ -19,13 +19,13 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
public static IFileInfo ForExistingDirectory(string path) public static IFileInfo ForExistingDirectory(string path)
=> new InMemoryFileInfo(path, isDir: true, exists: true); => new InMemoryFileInfo(path, isDir: true, exists: true);
public static IFileInfo ForExistingFile(string path, Stream dataStream, DateTimeOffset lastModified) public static IFileInfo ForExistingFile(string path, byte[] data, DateTimeOffset lastModified)
=> new InMemoryFileInfo(path, isDir: false, exists: true, dataStream: dataStream, lastModified: lastModified); => new InMemoryFileInfo(path, isDir: false, exists: true, data: data, lastModified: lastModified);
public static IFileInfo ForNonExistingFile(string path) public static IFileInfo ForNonExistingFile(string path)
=> new InMemoryFileInfo(path, isDir: false, exists: false); => new InMemoryFileInfo(path, isDir: false, exists: false);
private InMemoryFileInfo(string physicalPath, bool isDir, bool exists, Stream dataStream = null, DateTimeOffset lastModified = default(DateTimeOffset)) private InMemoryFileInfo(string physicalPath, bool isDir, bool exists, byte[] data = null, DateTimeOffset lastModified = default(DateTimeOffset))
{ {
_exists = exists; _exists = exists;
_isDirectory = isDir; _isDirectory = isDir;
@ -48,15 +48,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
} }
} }
if (dataStream != null) _fileData = data;
{
using (var ms = new MemoryStream())
{
dataStream.CopyTo(ms);
_fileData = ms.ToArray();
dataStream.Dispose();
}
}
} }
public bool Exists => _exists; public bool Exists => _exists;

View File

@ -20,7 +20,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
// It's convenient to use forward slash, because it matches URL conventions // It's convenient to use forward slash, because it matches URL conventions
public const char DirectorySeparatorChar = '/'; public const char DirectorySeparatorChar = '/';
public InMemoryFileProvider(IEnumerable<(string, Stream)> contents) : this( public InMemoryFileProvider(IEnumerable<(string, byte[])> contents) : this(
contents.Select(pair => InMemoryFileInfo contents.Select(pair => InMemoryFileInfo
.ForExistingFile(pair.Item1, pair.Item2, DateTime.Now))) .ForExistingFile(pair.Item1, pair.Item2, DateTime.Now)))
{ {

View File

@ -13,9 +13,8 @@ namespace Microsoft.Blazor.Common.Test
{ {
public class CompositeMountedFileProviderTest public class CompositeMountedFileProviderTest
{ {
private (string, Stream) TestItem(string name) => TestItem(name, Array.Empty<byte>()); private (string, byte[]) TestItem(string name) => (name, Array.Empty<byte>());
private (string, Stream) TestItem(string name, string data) => TestItem(name, Encoding.UTF8.GetBytes(data)); private (string, byte[]) TestItem(string name, string data) => (name, Encoding.UTF8.GetBytes(data));
private (string, Stream) TestItem(string name, byte[] data) => (name, new MemoryStream(data));
private IFileProvider TestFileProvider(params string[] paths) => new InMemoryFileProvider(paths.Select(TestItem)); private IFileProvider TestFileProvider(params string[] paths) => new InMemoryFileProvider(paths.Select(TestItem));
[Fact] [Fact]

View File

@ -11,8 +11,7 @@ namespace Microsoft.Blazor.Common.Test
{ {
public class InMemoryFileProviderTest public class InMemoryFileProviderTest
{ {
private (string, Stream) TestItem(string name) => TestItem(name, Array.Empty<byte>()); private (string, byte[]) TestItem(string name) => (name, Array.Empty<byte>());
private (string, Stream) TestItem(string name, byte[] data) => (name, new MemoryStream(data));
[Fact] [Fact]
public void RequiresPathsToStartWithSlash() public void RequiresPathsToStartWithSlash()
@ -38,8 +37,8 @@ namespace Microsoft.Blazor.Common.Test
// Arrange // Arrange
var instance = new InMemoryFileProvider(new[] var instance = new InMemoryFileProvider(new[]
{ {
TestItem("/dirA/item", Encoding.UTF8.GetBytes("Contents of /dirA/item")), ("/dirA/item", Encoding.UTF8.GetBytes("Contents of /dirA/item")),
TestItem("/dirB/item", Encoding.UTF8.GetBytes("Contents of /dirB/item")) ("/dirB/item", Encoding.UTF8.GetBytes("Contents of /dirB/item"))
}); });
// Act // Act