Skip to content

Commit 268ec9f

Browse files
authored
Merge pull request #21559 from abpframework/berkan/add-UseStaticFilesForPatterns
Add `UseStaticFilesForPatterns` extension method to `IApplicationBuilder`
2 parents 2d0871d + 41f9e0b commit 268ec9f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Volo.Abp.AspNetCore.ExceptionHandling;
1616
using Volo.Abp.AspNetCore.Security;
1717
using Volo.Abp.AspNetCore.Security.Claims;
18+
using Volo.Abp.AspNetCore.StaticFiles;
1819
using Volo.Abp.AspNetCore.Tracing;
1920
using Volo.Abp.AspNetCore.Uow;
2021
using Volo.Abp.AspNetCore.VirtualFileSystem;
@@ -126,6 +127,38 @@ public static IApplicationBuilder UseDynamicClaims(this IApplicationBuilder app)
126127
return app.UseMiddleware<AbpDynamicClaimsMiddleware>();
127128
}
128129

130+
/// <summary>
131+
/// Configures the application to serve static files that match the specified filename patterns with the WebRootFileProvider of the application.
132+
/// </summary>
133+
/// <param name="app">The <see cref="IApplicationBuilder"/> used to configure the application pipeline.</param>
134+
/// <param name="includeFileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
135+
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
136+
/// </param>
137+
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
138+
public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, params string[] includeFileNamePatterns)
139+
{
140+
return UseStaticFilesForPatterns(app, includeFileNamePatterns, app.ApplicationServices.GetRequiredService<IWebHostEnvironment>().WebRootFileProvider);
141+
}
142+
143+
/// <summary>
144+
/// Configures the application to serve static files that match the specified filename patterns with the specified file provider.
145+
/// </summary>
146+
/// <param name="app">The <see cref="IApplicationBuilder"/> used to configure the application pipeline.</param>
147+
/// <param name="includeFileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
148+
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
149+
/// </param>
150+
/// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
151+
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
152+
public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, string[] includeFileNamePatterns, IFileProvider fileProvider)
153+
{
154+
app.UseStaticFiles(new StaticFileOptions
155+
{
156+
FileProvider = new AbpStaticFileProvider(includeFileNamePatterns, fileProvider)
157+
});
158+
159+
return app;
160+
}
161+
129162
/// <summary>
130163
/// MapAbpStaticAssets is used to serve the files from the abp virtual file system embedded resources(js/css) and call the MapStaticAssets.
131164
/// </summary>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Microsoft.Extensions.FileProviders;
5+
using Microsoft.Extensions.FileSystemGlobbing;
6+
using Microsoft.Extensions.Primitives;
7+
8+
namespace Volo.Abp.AspNetCore.StaticFiles;
9+
10+
public class AbpStaticFileProvider : IFileProvider
11+
{
12+
private readonly Matcher _matcher;
13+
private readonly IFileProvider _fileProvider;
14+
15+
/// <param name="fileProvider">The file provider to be used to get the files.</param>
16+
/// <param name="fileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
17+
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
18+
/// </param>
19+
public AbpStaticFileProvider(IReadOnlyList<string> fileNamePatterns, IFileProvider fileProvider)
20+
{
21+
_fileProvider = fileProvider;
22+
_matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
23+
_matcher.AddIncludePatterns(fileNamePatterns);
24+
}
25+
26+
public IDirectoryContents GetDirectoryContents(string subpath)
27+
{
28+
return new NotFoundDirectoryContents();
29+
}
30+
31+
public IFileInfo GetFileInfo(string subpath)
32+
{
33+
return _matcher.Match(Path.GetFileName(subpath)).HasMatches ?
34+
_fileProvider.GetFileInfo(subpath) :
35+
new NotFoundFileInfo(subpath);
36+
}
37+
38+
public IChangeToken Watch(string filter)
39+
{
40+
return NullChangeToken.Singleton;
41+
}
42+
}

0 commit comments

Comments
 (0)