Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Loading content from EmbeddedResource in the assembly #31

Open
frg2089 opened this issue Jul 17, 2023 · 3 comments
Open

Comments

@frg2089
Copy link

frg2089 commented Jul 17, 2023

Do we have plans to support loading content from EmbeddedResource in the assembly?

like this https://github.com/JBildstein/SpiderEye/blob/master/Source/SpiderEye.Core/Content/EmbeddedContentProvider.cs

@Lemonify
Copy link
Contributor

Lemonify commented Dec 8, 2023

Hi, I've got this somewhat implemented for my purposes, so if you still need it, I could share it or make a PR (seems like I am in the contributing mood lately 😁). There is a single drawback, though. Embedded files must be flattened, meaning that all JS/CSS/images must be in the same directory as index.html. It's fine by me, but it should be somehow possible to configure it to accept any folder structure.

@frg2089
Copy link
Author

frg2089 commented Dec 9, 2023

Hi, I've got this somewhat implemented for my purposes, so if you still need it, I could share it or make a PR (seems like I am in the contributing mood lately 😁). There is a single drawback, though. Embedded files must be flattened, meaning that all JS/CSS/images must be in the same directory as index.html. It's fine by me, but it should be somehow possible to configure it to accept any folder structure.

I'm glad you implemented this feature. Would you mind sharing it?
Normally EmbeddedResource uses . for resource names. We can change the value of LogicalName to use /.

@Lemonify
Copy link
Contributor

Lemonify commented Dec 9, 2023

It's super similar to the HostedContent class (and could be improved by making an abstract class or merging Hosted and Embedded classes together).

Project file (.csproj) edits:
Replace EmbeddedResource Include with your frontend dist/output folder. The only drawback is that you need to setup your frontend build process to output all files in a single directory, not nested like usual.

<ItemGroup>
	<EmbeddedResource Include="dist\**\*">
		<LogicalName>%(Filename)%(Extension)</LogicalName>
	</EmbeddedResource>
</ItemGroup>

Content Provider:

public class EmbeddedContent : IWebviewContent
{
    private readonly WebApplication _webApp;

    public EmbeddedContent(Assembly embeddedAssembly, int port = 0, bool activateLog = false, IDictionary<string, string>? additionalMimeTypes = null)
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder();
        builder.WebHost.UseKestrel(options => {
            options.Listen(IPAddress.Loopback, port);
        });

        if (!activateLog)
        {
            builder.Logging.ClearProviders();
        }

        _webApp = builder.Build();

        FileServerOptions fileServerOptions = new()
        {
            FileProvider = new EmbeddedFileProvider(embeddedAssembly, ""),
            RequestPath = "",
            EnableDirectoryBrowsing = true
        };

        if (additionalMimeTypes != null)
        {
            FileExtensionContentTypeProvider extensionProvider = new();

            foreach (var mimeType in additionalMimeTypes)
            {
                extensionProvider.Mappings.Add(mimeType);
            }

            fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider;
        }

        _webApp.UseFileServer(fileServerOptions);
        _webApp.Start();
    }

    public string ToWebviewUrl()
    {
        return _webApp.Urls.First();
    }
}

How to use it with Webview:

IWebviewContent content = new EmbeddedContent(typeof(Program).Assembly);
webview.Navigate(content).Run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants