-
Notifications
You must be signed in to change notification settings - Fork 21
Add provider-independent middleware and CDN media URL provider #11
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
Conversation
Just a thought: it makes more sense to directly implement the
This way you only need to implement a single In the future, we might replace the |
Hello @ronaldbarendse , I love your idea to access the storage through the |
I've pulled down this branch and included it in my project and it works perfectly for my use case! I migrated a site from WordPress to Umbraco about a year ago, but not all the content came across into Umbraco's media. The existing Startup.cs public void ConfigureServices(IServiceCollection services)
{
services
.AddAzureBlobMediaFileSystem()
.AddAzureBlobFileSystem("WpContent", "~/wp-content");
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseAzureBlobMediaFileSystem();
u.UseAzureBlobMediaCustomFileSystem("WpContent");
});
} AzureBlobMediaFileSystemExtensions.cs public static IUmbracoApplicationBuilderContext UseAzureBlobMediaCustomFileSystem(this IUmbracoApplicationBuilderContext builder, string fileSystemName)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
UseAzureBlobMediaCustomFileSystem(builder.AppBuilder, fileSystemName);
return builder;
}
public static IApplicationBuilder UseAzureBlobMediaCustomFileSystem(this IApplicationBuilder app, string fileSystemName)
{
if (app == null) throw new ArgumentNullException(nameof(app));
var fileSystem = app.ApplicationServices.GetRequiredService<IAzureBlobFileSystemProvider>().GetFileSystem(fileSystemName);
var options = app.ApplicationServices.GetRequiredService<IOptionsFactory<AzureBlobFileSystemOptions>>().Create(fileSystemName);
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
var requestPath = hostingEnvironment.ToAbsolute(options.VirtualPath);
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new FileSystemFileProvider(fileSystem, requestPath),
RequestPath = requestPath,
OnPrepareResponse = ctx =>
{
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MustRevalidate = true,
MaxAge = TimeSpan.FromDays(7)
};
}
});
return app;
} Apologies for the big code block but hopefully this helps someone! |
This is now obsoleted by the work done in #36. |
This builds on top of PR #9 and moves provider-independent code to the new
Umbraco.StorageProviders
project.It also adds an abstraction/wrapper to make Umbraco's
IFileSystem
(read/write) compatible with the ASP.NET CoreIFileProvider
(read-only), so it can be used by the built-inStaticFileMiddleware
to serve files.Adding support for a new storage provider would then only require implementing the
IFileSystem
and optionally add some helper methods to easily configure it.We're also taking advantage of all features already available in the
StaticFileMiddleware
(like compression and altering the response, e.g. to set cache control headers).I'm creating this as a draft, because there's still some things to improve and test:
Umbraco:Storage:Media:Cdn
and the Azure Blob Storage version adds the configuration fromUmbraco:Storage:AzureBlob:Media:Cdn
on top of that and configures the CDN URL to use the container name as path. I'm not sure if the naming and cascading make sense though...UseStaticFiles()
can be make more generic and should expose the options, so it's actually possible to alter the response headers, etc.AzureBlobFileSystemMiddleware
, but this is something that needs more extensive testing.