ZarehD/AspNetStatic

[Feature Request] Can we do something when processing resource?

Closed this issue · 3 comments

Is your feature request related to a problem? Please describe.

I am trying to use this library to generate static pages for vintage devices.

Everything is fine but I met a problem.

I need to convert WebP images to JPG images for WebP images can't be displayed on some devices. But there's no way to do that when using BinResource.

Describe the solution you'd like

We can add a middleware, so that we can modify resource's content before AspNetStatic copys resource to the output directory.

For example:

var options = new StaticContentResourceOptions()
{
    OnProcessResource = (StaticContentResourceContext context) =>
    {
        if (context.Url.EndWith(".webp"))
        {
            Stream resContent = context.ResourceContent;
            // Do something here...
        }
    }
};
app.UseStaticContentResourceProcessing(options);

Describe alternatives you've considered

Instead of using BinResource, I manually modified file and copied it to the output directory. But this is inconvenient.

Additional context

No

ZarehD commented

Thanks for the feedback, @Baka632.

The infrastructure for this actually exists in the "content optimization" feature already, but it's disabled for BinResource entry types. Once I finish enabling it, all you'll need to do is register (in the DI container) an implementation of a simple new interface (IBinOptimizer) that incorporates your conversion logic (i.e. from WebP to JPG), and set the OptimizerType for the BinResource entry to OptimizerType.Bin.

I'll update this ticket when the release for this gets pushed out.

ZarehD commented

Good news; I was able to finish this tonight! The new release (v0.20.0) has been pushed to Nuget.

P.S. I'm closing the ticket, but please feel free to let me know if you have any feedback on the release.

Everything goes fine, thanks!