Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/WebOptimizer.Core/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -21,6 +22,11 @@ namespace WebOptimizer
{
internal class Asset : IAsset
{
/// <summary>
/// Private const extracted from <see cref="GlobbingUrlBuilder"/>.
/// Used to join multiple patterns for that class.
/// </summary>
internal const string PatternSeparator = ",";
private readonly ILogger<Asset> _logger;
internal const string PhysicalFilesKey = "PhysicalFiles";
private readonly object _sync = new();
Expand Down Expand Up @@ -50,9 +56,10 @@ public Asset(string route, string contentType, IEnumerable<string> sourceFiles,
public async Task<byte[]> ExecuteAsync(HttpContext context, IWebOptimizerOptions options)
{
var env = (IWebHostEnvironment)context.RequestServices.GetService(typeof(IWebHostEnvironment));
var cache = (IMemoryCache)context.RequestServices.GetService(typeof(IMemoryCache));
var config = new AssetContext(context, this, options);

IEnumerable<string> files = ExpandGlobs(this, env);
IEnumerable<string> files = ExpandGlobs(this, env, cache);

DateTime lastModified = DateTime.MinValue;

Expand Down Expand Up @@ -84,12 +91,17 @@ public async Task<byte[]> ExecuteAsync(HttpContext context, IWebOptimizerOptions
return config.Content.FirstOrDefault().Value;
}

public static IEnumerable<string> ExpandGlobs(IAsset asset, IWebHostEnvironment env)
public static IEnumerable<string> ExpandGlobs(IAsset asset, IWebHostEnvironment env, IMemoryCache cache)
{
var files = new List<string>();

if (asset.SourceFiles.Any())
{
var excludePattern =
asset.ExcludeFiles.Count == 0
? null
: string.Join(PatternSeparator, asset.ExcludeFiles);

foreach (string sourceFile in asset.SourceFiles)
{
var provider = asset.GetFileProvider(env, sourceFile, out string outSourceFile);
Expand All @@ -103,19 +115,13 @@ public static IEnumerable<string> ExpandGlobs(IAsset asset, IWebHostEnvironment
}
else
{
var virtualFilePaths = provider.GetAllFiles("/");

var matcher = new Matcher();
matcher.AddInclude(outSourceFile);
matcher.AddExcludePatterns(asset.ExcludeFiles);
PatternMatchingResult globbingResult = matcher.Match(virtualFilePaths);

IEnumerable<string> fileMatches = globbingResult.Files.Select(f => f.Path);
var globbingUrlBuilder = new GlobbingUrlBuilder(provider, cache, requestPathBase: /*context.Request.PathBase*/ null);
IEnumerable<string> fileMatches = globbingUrlBuilder.BuildUrlList(staticUrl: null, includePattern: outSourceFile, excludePattern: excludePattern);

var sourceIsRooted = outSourceFile.StartsWith('/');
if (sourceIsRooted)
if (!sourceIsRooted)
{
fileMatches = fileMatches.Select(f => "/" + f);
fileMatches = fileMatches.Select(f => f.TrimStart('/'));
}

if (!fileMatches.Any())
Expand Down Expand Up @@ -179,7 +185,7 @@ public string GenerateCacheKey(HttpContext context, IWebOptimizerOptions options

if (!Items.ContainsKey(PhysicalFilesKey))
{
physicalFiles = ExpandGlobs(this, env);
physicalFiles = ExpandGlobs(this, env, cache);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebOptimizer.Core/Taghelpers/LinkTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void WriteIndividualTags(TagHelperOutput output, IAsset asset)
attrs.Add(attr);
}

IEnumerable<string> sourceFiles = Asset.ExpandGlobs(asset, HostingEnvironment);
IEnumerable<string> sourceFiles = Asset.ExpandGlobs(asset, HostingEnvironment, Cache);

foreach (string file in sourceFiles)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebOptimizer.Core/Taghelpers/ScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void WriteIndividualTags(TagHelperOutput output, IAsset asset)
attrs.Add(attr);
}

IEnumerable<string> sourceFiles = Asset.ExpandGlobs(asset, HostingEnvironment);
IEnumerable<string> sourceFiles = Asset.ExpandGlobs(asset, HostingEnvironment, Cache);

foreach (string file in sourceFiles)
{
Expand Down
Loading