Browse Source

Merge pull request #21559 from abpframework/berkan/add-UseStaticFilesForPatterns

Add `UseStaticFilesForPatterns` extension method to `IApplicationBuilder`
pull/21564/head
maliming 1 year ago
committed by GitHub
parent
commit
268ec9f6b2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 33
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs
  2. 42
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs

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

@ -15,6 +15,7 @@ using Volo.Abp.AspNetCore.Auditing;
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;
@ -126,6 +127,38 @@ public static class AbpApplicationBuilderExtensions
return app.UseMiddleware<AbpDynamicClaimsMiddleware>();
}
/// <summary>
/// Configures the application to serve static files that match the specified filename patterns with the WebRootFileProvider of the application.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> used to configure the application pipeline.</param>
/// <param name="includeFileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
/// </param>
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, params string[] includeFileNamePatterns)
{
return UseStaticFilesForPatterns(app, includeFileNamePatterns, app.ApplicationServices.GetRequiredService<IWebHostEnvironment>().WebRootFileProvider);
}
/// <summary>
/// Configures the application to serve static files that match the specified filename patterns with the specified file provider.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> used to configure the application pipeline.</param>
/// <param name="includeFileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
/// </param>
/// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
public static IApplicationBuilder UseStaticFilesForPatterns(this IApplicationBuilder app, string[] includeFileNamePatterns, IFileProvider fileProvider)
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new AbpStaticFileProvider(includeFileNamePatterns, fileProvider)
});
return app;
}
/// <summary>
/// MapAbpStaticAssets is used to serve the files from the abp virtual file system embedded resources(js/css) and call the MapStaticAssets.
/// </summary>

42
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs

@ -0,0 +1,42 @@
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;
/// <param name="fileProvider">The file provider to be used to get the files.</param>
/// <param name="fileNamePatterns">The file name patterns to include when serving static files (e.g., "appsettings*.json").
/// Supports glob patterns. See <see href="https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing">Glob patterns documentation</see>.
/// </param>
public AbpStaticFileProvider(IReadOnlyList<string> fileNamePatterns, IFileProvider fileProvider)
{
_fileProvider = fileProvider;
_matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
_matcher.AddIncludePatterns(fileNamePatterns);
}
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;
}
}
Loading…
Cancel
Save