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
30 changes: 26 additions & 4 deletions src/WebOptimizer.Core/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace WebOptimizer
internal class Asset : IAsset
{
internal const string PhysicalFilesKey = "PhysicalFiles";
internal const string AllFilesCacheKey = nameof(WebOptimizer) + "_AllFilesCache";
private static readonly object _allFilesCacheKeySync = new object();
private readonly object _sync = new object();

public Asset(string route, string contentType, IAssetPipeline pipeline, IEnumerable<string> sourceFiles)
Expand Down Expand Up @@ -53,7 +55,7 @@ public async Task<byte[]> ExecuteAsync(HttpContext context, IWebOptimizerOptions
var env = (IWebHostEnvironment)context.RequestServices.GetService(typeof(IWebHostEnvironment));
var config = new AssetContext(context, this, options);

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

DateTime lastModified = DateTime.MinValue;

Expand Down Expand Up @@ -85,8 +87,24 @@ 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, HttpContext context)
{
ConcurrentDictionary<IFileProvider, IReadOnlyList<string>> virtualFilePathsCache;

lock (_allFilesCacheKeySync)
{
if (context.Items.TryGetValue(AllFilesCacheKey, out var item)
&& item is ConcurrentDictionary<IFileProvider, IReadOnlyList<string>> dict)
{
virtualFilePathsCache = dict;
}
else
{
virtualFilePathsCache = new ConcurrentDictionary<IFileProvider, IReadOnlyList<string>>();
context.Items[AllFilesCacheKey] = virtualFilePathsCache;
}
}

var files = new List<string>();

if (asset.SourceFiles.Any())
Expand All @@ -104,7 +122,11 @@ public static IEnumerable<string> ExpandGlobs(IAsset asset, IWebHostEnvironment
}
else
{
var virtualFilePaths = provider.GetAllFiles("/");
IReadOnlyList<string> virtualFilePaths =
virtualFilePathsCache
.GetOrAdd(
provider,
provider => provider.GetAllFiles("/"));

var matcher = new Matcher();
matcher.AddInclude(outSourceFile);
Expand Down Expand Up @@ -180,7 +202,7 @@ public string GenerateCacheKey(HttpContext context, IWebOptimizerOptions options

if (!Items.ContainsKey(PhysicalFilesKey))
{
physicalFiles = ExpandGlobs(this, env);
physicalFiles = ExpandGlobs(this, env, context);
}
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 @@ -99,7 +99,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, CurrentViewContext.HttpContext);

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 @@ -100,7 +100,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, CurrentViewContext.HttpContext);

foreach (string file in sourceFiles)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public void CdnUrl_RouteIsAsset_TagHelperBundlingDisabled_Success(string cdnUrl,
context.Setup(c => c.RequestServices.GetService(typeof(IMemoryCache)))
.Returns(cache.Object);
context.SetupGet(c => c.Request.PathBase).Returns(pathBase);
context.SetupGet(c => c.Items).Returns(new Dictionary<object, object>());

var options = new WebOptimizerOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public void CdnUrl_RouteIsAsset_TagHelperBundlingDisabled_Success(string cdnUrl,
context.Setup(c => c.RequestServices.GetService(typeof(IMemoryCache)))
.Returns(cache.Object);
context.SetupGet(c => c.Request.PathBase).Returns(pathBase);
context.SetupGet(c => c.Items).Returns(new Dictionary<object, object>());

var options = new WebOptimizerOptions
{
Expand Down