Browse Source

Add `UseStaticFilesForPatterns` extension method to `IApplicationBuilder`

pull/21559/head
Berkan Sasmaz 2 years ago
parent
commit
6a008282d3
  1. 20
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs
  2. 40
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/StaticFiles/AbpStaticFileProvider.cs

20
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;
@ -125,6 +126,25 @@ public static class AbpApplicationBuilderExtensions
{
return app.UseMiddleware<AbpDynamicClaimsMiddleware>();
}
/// <summary>
/// Configures the application to serve static files that match the specified filename patterns.
/// </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.

40
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;
/// <param name="fileProvider">The file provider to be used to get the files.</param>
/// <param name="fileNames">The file names to get from the file provider. Supports glob patterns. See https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing.</param>
public AbpStaticFileProvider(IReadOnlyList<string> 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;
}
}
Loading…
Cancel
Save