From 6a008282d3fef6ff6b44e2cddb2e4ba1ad7a5404 Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Fri, 6 Dec 2024 11:31:17 +0300 Subject: [PATCH 1/3] Add `UseStaticFilesForPatterns` extension method to `IApplicationBuilder` --- .../AbpApplicationBuilderExtensions.cs | 20 ++++++++++ .../StaticFiles/AbpStaticFileProvider.cs | 40 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index bd653faf557..f1dd6d4d6e4 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -15,6 +15,7 @@ using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.Security; using Volo.Abp.AspNetCore.Security.Claims; +using Volo.Abp.AspNetCore.StaticFiles; using Volo.Abp.AspNetCore.Tracing; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.AspNetCore.VirtualFileSystem; @@ -125,6 +126,25 @@ public static IApplicationBuilder UseDynamicClaims(this IApplicationBuilder app) { return app.UseMiddleware(); } + + /// + /// Configures the application to serve static files that match the specified filename patterns. + /// + /// The used to configure the application pipeline. + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + /// The + /// The instance. + public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, string[] includeFileNamePatterns, IFileProvider fileProvider) + { + app.UseStaticFiles(new StaticFileOptions + { + FileProvider = new AbpStaticFileProvider(includeFileNamePatterns, fileProvider) + }); + + return app; + } /// /// MapAbpStaticAssets is used to serve the files from the abp virtual file system embedded resources(js/css) and call the MapStaticAssets. diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs new file mode 100644 index 00000000000..48a2c36d965 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.FileSystemGlobbing; +using Microsoft.Extensions.Primitives; + +namespace Volo.Abp.AspNetCore.StaticFiles; + +public class AbpStaticFileProvider : IFileProvider +{ + private readonly Matcher _matcher; + private readonly IFileProvider _fileProvider; + + /// The file provider to be used to get the files. + /// The file names to get from the file provider. Supports glob patterns. See https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing. + public AbpStaticFileProvider(IReadOnlyList fileNames, IFileProvider fileProvider) + { + _fileProvider = fileProvider; + _matcher = new Matcher(StringComparison.OrdinalIgnoreCase); + _matcher.AddIncludePatterns(fileNames); + } + + public IDirectoryContents GetDirectoryContents(string subpath) + { + return new NotFoundDirectoryContents(); + } + + public IFileInfo GetFileInfo(string subpath) + { + return _matcher.Match(Path.GetFileName(subpath)).HasMatches ? + _fileProvider.GetFileInfo(subpath) : + new NotFoundFileInfo(subpath); + } + + public IChangeToken Watch(string filter) + { + return NullChangeToken.Singleton; + } +} \ No newline at end of file From eec8e7585524e4512476af1a83939f6a415a9338 Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Fri, 6 Dec 2024 11:34:23 +0300 Subject: [PATCH 2/3] Rename parameter --- .../Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs index 48a2c36d965..f7893932f2c 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs @@ -13,12 +13,14 @@ public class AbpStaticFileProvider : IFileProvider private readonly IFileProvider _fileProvider; /// The file provider to be used to get the files. - /// The file names to get from the file provider. Supports glob patterns. See https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing. - public AbpStaticFileProvider(IReadOnlyList fileNames, IFileProvider fileProvider) + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + public AbpStaticFileProvider(IReadOnlyList fileNamePatterns, IFileProvider fileProvider) { _fileProvider = fileProvider; _matcher = new Matcher(StringComparison.OrdinalIgnoreCase); - _matcher.AddIncludePatterns(fileNames); + _matcher.AddIncludePatterns(fileNamePatterns); } public IDirectoryContents GetDirectoryContents(string subpath) From 41f9e0b54602ad179fbfcc899df2bd0a3bc2fb54 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 6 Dec 2024 16:59:02 +0800 Subject: [PATCH 3/3] Add a extension method that using `WebRootFileProvider`. --- .../Builder/AbpApplicationBuilderExtensions.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index f1dd6d4d6e4..3cada3bf462 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -126,9 +126,22 @@ public static IApplicationBuilder UseDynamicClaims(this IApplicationBuilder app) { return app.UseMiddleware(); } - + + /// + /// Configures the application to serve static files that match the specified filename patterns with the WebRootFileProvider of the application. + /// + /// The used to configure the application pipeline. + /// The file name patterns to include when serving static files (e.g., "appsettings*.json"). + /// Supports glob patterns. See Glob patterns documentation. + /// + /// The instance. + public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, params string[] includeFileNamePatterns) + { + return UseStaticFilesForPatterns(app, includeFileNamePatterns, app.ApplicationServices.GetRequiredService().WebRootFileProvider); + } + /// - /// Configures the application to serve static files that match the specified filename patterns. + /// Configures the application to serve static files that match the specified filename patterns with the specified file provider. /// /// The used to configure the application pipeline. /// The file name patterns to include when serving static files (e.g., "appsettings*.json").