Simplify in-memory file provider usage by initializing with byte[], not Stream
This commit is contained in:
parent
64770bfb23
commit
c72f9a1977
|
|
@ -16,7 +16,7 @@ namespace Microsoft.Blazor.Build.Core.FileSystem
|
|||
{
|
||||
}
|
||||
|
||||
private static IEnumerable<(string, Stream)> ComputeContents(
|
||||
private static IEnumerable<(string, byte[])> ComputeContents(
|
||||
string rootAssemblyName,
|
||||
ReferencedAssemblyResolver resolver)
|
||||
{
|
||||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.Blazor.Build.Core.FileSystem
|
|||
|
||||
return foundAssemblies.Values.Select(assembly => (
|
||||
$"/{assembly.Definition.Name.Name}.dll",
|
||||
(Stream)new MemoryStream(assembly.Data)));
|
||||
assembly.Data));
|
||||
}
|
||||
|
||||
private static void AddWithReferencesRecursive(
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
var html = GetIndexHtmlContents(htmlTemplate, assemblyName, binFiles);
|
||||
var htmlBytes = Encoding.UTF8.GetBytes(html);
|
||||
var htmlStream = new MemoryStream(htmlBytes);
|
||||
yield return ("/index.html", htmlStream);
|
||||
yield return ("/index.html", htmlBytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
return assembly.GetManifestResourceNames()
|
||||
.Where(name => name.StartsWith(resourceNamePrefix, StringComparison.Ordinal))
|
||||
.Select(name => (
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,13 +19,13 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
|
|||
public static IFileInfo ForExistingDirectory(string path)
|
||||
=> new InMemoryFileInfo(path, isDir: true, exists: true);
|
||||
|
||||
public static IFileInfo ForExistingFile(string path, Stream dataStream, DateTimeOffset lastModified)
|
||||
=> new InMemoryFileInfo(path, isDir: false, exists: true, dataStream: dataStream, lastModified: lastModified);
|
||||
public static IFileInfo ForExistingFile(string path, byte[] data, DateTimeOffset lastModified)
|
||||
=> new InMemoryFileInfo(path, isDir: false, exists: true, data: data, lastModified: lastModified);
|
||||
|
||||
public static IFileInfo ForNonExistingFile(string path)
|
||||
=> 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;
|
||||
_isDirectory = isDir;
|
||||
|
|
@ -48,15 +48,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
|
|||
}
|
||||
}
|
||||
|
||||
if (dataStream != null)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
dataStream.CopyTo(ms);
|
||||
_fileData = ms.ToArray();
|
||||
dataStream.Dispose();
|
||||
}
|
||||
}
|
||||
_fileData = data;
|
||||
}
|
||||
|
||||
public bool Exists => _exists;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
|
|||
// It's convenient to use forward slash, because it matches URL conventions
|
||||
public const char DirectorySeparatorChar = '/';
|
||||
|
||||
public InMemoryFileProvider(IEnumerable<(string, Stream)> contents) : this(
|
||||
public InMemoryFileProvider(IEnumerable<(string, byte[])> contents) : this(
|
||||
contents.Select(pair => InMemoryFileInfo
|
||||
.ForExistingFile(pair.Item1, pair.Item2, DateTime.Now)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -13,9 +13,8 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
public class CompositeMountedFileProviderTest
|
||||
{
|
||||
private (string, Stream) TestItem(string name) => TestItem(name, Array.Empty<byte>());
|
||||
private (string, Stream) TestItem(string name, string data) => TestItem(name, Encoding.UTF8.GetBytes(data));
|
||||
private (string, Stream) TestItem(string name, byte[] data) => (name, new MemoryStream(data));
|
||||
private (string, byte[]) TestItem(string name) => (name, Array.Empty<byte>());
|
||||
private (string, byte[]) TestItem(string name, string data) => (name, Encoding.UTF8.GetBytes(data));
|
||||
private IFileProvider TestFileProvider(params string[] paths) => new InMemoryFileProvider(paths.Select(TestItem));
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -11,8 +11,7 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
public class InMemoryFileProviderTest
|
||||
{
|
||||
private (string, Stream) TestItem(string name) => TestItem(name, Array.Empty<byte>());
|
||||
private (string, Stream) TestItem(string name, byte[] data) => (name, new MemoryStream(data));
|
||||
private (string, byte[]) TestItem(string name) => (name, Array.Empty<byte>());
|
||||
|
||||
[Fact]
|
||||
public void RequiresPathsToStartWithSlash()
|
||||
|
|
@ -38,8 +37,8 @@ namespace Microsoft.Blazor.Common.Test
|
|||
// Arrange
|
||||
var instance = new InMemoryFileProvider(new[]
|
||||
{
|
||||
TestItem("/dirA/item", Encoding.UTF8.GetBytes("Contents of /dirA/item")),
|
||||
TestItem("/dirB/item", Encoding.UTF8.GetBytes("Contents of /dirB/item"))
|
||||
("/dirA/item", Encoding.UTF8.GetBytes("Contents of /dirA/item")),
|
||||
("/dirB/item", Encoding.UTF8.GetBytes("Contents of /dirB/item"))
|
||||
});
|
||||
|
||||
// Act
|
||||
|
|
|
|||
Loading…
Reference in New Issue