Skip to content

Commit

Permalink
test embed resource all
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Apr 30, 2020
1 parent d0ad05f commit 7253107
Show file tree
Hide file tree
Showing 39 changed files with 78 additions and 4,919 deletions.
@@ -1,8 +1,11 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -29,7 +32,7 @@ public static IServiceCollection AddBlockly(this IServiceCollection serviceColle
{
serviceCollection.AddSingleton<GenerateBlocklyFilesHostedService>();
serviceCollection.AddHostedService(p => p.GetService<GenerateBlocklyFilesHostedService>());

return serviceCollection;
}
/// <summary>
Expand Down Expand Up @@ -67,5 +70,52 @@ private static async Task GetBlocklyFilesHostedServices(HttpContext context, str
var mem = new Memory<byte>(Encoding.UTF8.GetBytes(blocklyDefinitions));
await context.Response.BodyWriter.WriteAsync(mem);
}
private static void mapFile(string dirName, IFileProvider provider, IApplicationBuilder appBuilder)
{
var folder = provider.GetDirectoryContents(dirName);
foreach (var item in folder)
{
if (item.IsDirectory)
{
mapFile(dirName +"/" + item.Name,provider, appBuilder);
continue;
}
string map = (dirName + "/" + item.Name).Substring("blocklyFiles".Length);
appBuilder.Map(map, app =>
{
var f = item;
app.Run(async cnt =>
{
//TODO: find from extension
//cnt.Response.ContentType = "text/html";
using var stream = new MemoryStream();
using var cs = f.CreateReadStream();
byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
byte[] result = stream.ToArray();
// TODO: do something with the result
var m = new Memory<byte>(result);
await cnt.Response.BodyWriter.WriteAsync(m);
});
});
}

}
/// <summary>
/// Uses the cli.
/// </summary>
/// <param name="appBuilder">The application builder.</param>
public static void UseBlocklyCli(this IApplicationBuilder appBuilder)
{
var manifestEmbeddedProvider =
new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());

mapFile("blocklyFiles", manifestEmbeddedProvider, appBuilder);
}
}
}
9 changes: 9 additions & 0 deletions src/NetCore2Blockly/NetCore2Blockly/NetCore2Blockly.csproj
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Copyright>MIT</Copyright>
<RepositoryUrl>https://github.com/ignatandrei/NETCoreBlockly</RepositoryUrl>
Expand All @@ -25,8 +26,16 @@

<ItemGroup>
<None Remove="NetCore2Blockly.xml" />
<None Remove="blocklyFiles\testhtml" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="blocklyFiles\**\*">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.3" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/NetCore2Blockly/NetCore2Blockly/NetCore2Blockly.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/NetCore2Blockly/NetCore2Blockly/blocklyFiles/blockly.html
@@ -0,0 +1,2 @@
this file will be replaced in the CD process
by the real blockly file
6 changes: 4 additions & 2 deletions src/NetCore2Blockly/TestBlocklyHtml/Startup.cs
Expand Up @@ -70,10 +70,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
}

//just developer testing! do not use in production
app.UseFileServer(enableDirectoryBrowsing: true);


//TODO: put this in the real application
// or copy the blockly.html files and others from wwwroot
//app.UseBlocklyCli();

//this is not necessary to be added
app.UseSwagger();
Expand Down
1 change: 1 addition & 0 deletions src/NetCore2Blockly/TestBlocklyHtml/wwwroot/index.html
@@ -0,0 +1 @@
Please navigate to blockly.html

0 comments on commit 7253107

Please sign in to comment.