Skip to content

Commit

Permalink
Add additionalMimeTypes to HostedContent (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: BELL\frederic.lachapelle <[email protected]>
  • Loading branch information
Fredolx and BELL\frederic.lachapelle authored Apr 13, 2022
1 parent 44a23ac commit d779380
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/SharpWebview/Content/HostedContent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -6,6 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
Expand All @@ -16,9 +18,10 @@ public sealed class HostedContent : IWebviewContent
{
private readonly IWebHost _webHost;

public HostedContent(int port = 0, bool activateLog = false)
public HostedContent(int port = 0, bool activateLog = false, IDictionary<string, string> additionalMimeTypes = null)
{
_webHost = WebHost.CreateDefaultBuilder()
.ConfigureServices(s => s.AddSingleton(x => new StartupParameters() { AdditionalMimeTypes = additionalMimeTypes }))
.UseStartup<Startup>()
.UseKestrel(options => options.Listen(IPAddress.Loopback, port))
.ConfigureLogging((logger) => { if(!activateLog) logger.ClearProviders(); })
Expand All @@ -37,16 +40,29 @@ public string ToWebviewUrl()

internal sealed class Startup
{
private StartupParameters startupParameters;
public Startup(StartupParameters startupParameters)
{
this.startupParameters = startupParameters;
}
public void ConfigureServices(IServiceCollection services) { }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var workingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
app.UseFileServer(new FileServerOptions
var fileServerOptions = new FileServerOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(workingDirectory, "app")),
RequestPath = "",
EnableDirectoryBrowsing = true
});
EnableDirectoryBrowsing = true,
};
if(startupParameters.AdditionalMimeTypes != null)
{
var extensionProvider = new FileExtensionContentTypeProvider();
foreach (var mimeType in startupParameters.AdditionalMimeTypes)
extensionProvider.Mappings.Add(mimeType);
fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider;
}
app.UseFileServer(fileServerOptions);
}
}
}
11 changes: 11 additions & 0 deletions src/SharpWebview/StartupParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpWebview
{
public class StartupParameters
{
public IDictionary<string, string>? AdditionalMimeTypes { get; set; }
}
}

0 comments on commit d779380

Please sign in to comment.