|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Frozen; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.Components; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Describes a mapping of static assets to their corresponding unique URLs. |
| 11 | +/// </summary> |
| 12 | +public sealed class ResourceAssetCollection : IReadOnlyList<ResourceAsset> |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// An empty <see cref="ResourceAssetCollection"/>. |
| 16 | + /// </summary> |
| 17 | + public static readonly ResourceAssetCollection Empty = new([]); |
| 18 | + |
| 19 | + private readonly FrozenDictionary<string, ResourceAsset> _uniqueUrlMappings; |
| 20 | + private readonly FrozenSet<string> _contentSpecificUrls; |
| 21 | + private readonly IReadOnlyList<ResourceAsset> _resources; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of <see cref="ResourceAssetCollection"/> |
| 25 | + /// </summary> |
| 26 | + /// <param name="resources">The list of resources available.</param> |
| 27 | + public ResourceAssetCollection(IReadOnlyList<ResourceAsset> resources) |
| 28 | + { |
| 29 | + var mappings = new Dictionary<string, ResourceAsset>(StringComparer.OrdinalIgnoreCase); |
| 30 | + var contentSpecificUrls = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| 31 | + _resources = resources; |
| 32 | + foreach (var resource in resources) |
| 33 | + { |
| 34 | + foreach (var property in resource.Properties ?? []) |
| 35 | + { |
| 36 | + if (property.Name.Equals("label", StringComparison.OrdinalIgnoreCase)) |
| 37 | + { |
| 38 | + if (mappings.TryGetValue(property.Value, out var value)) |
| 39 | + { |
| 40 | + throw new InvalidOperationException($"The static asset '{property.Value}' is already mapped to {value.Url}."); |
| 41 | + } |
| 42 | + mappings[property.Value] = resource; |
| 43 | + contentSpecificUrls.Add(resource.Url); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + _uniqueUrlMappings = mappings.ToFrozenDictionary(); |
| 49 | + _contentSpecificUrls = contentSpecificUrls.ToFrozenSet(); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets the unique content-based URL for the specified static asset. |
| 54 | + /// </summary> |
| 55 | + /// <param name="key">The asset name.</param> |
| 56 | + /// <returns>The unique URL if availabe, the same <paramref name="key"/> if not available.</returns> |
| 57 | + public string this[string key] => _uniqueUrlMappings.TryGetValue(key, out var value) ? value.Url : key; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Determines whether the specified path is a content-specific URL. |
| 61 | + /// </summary> |
| 62 | + /// <param name="path">The path to check.</param> |
| 63 | + /// <returns><c>true</c> if the path is a content-specific URL; otherwise, <c>false</c>.</returns> |
| 64 | + public bool IsContentSpecificUrl(string path) => _contentSpecificUrls.Contains(path); |
| 65 | + |
| 66 | + // IReadOnlyList<ResourceAsset> implementation |
| 67 | + ResourceAsset IReadOnlyList<ResourceAsset>.this[int index] => _resources[index]; |
| 68 | + int IReadOnlyCollection<ResourceAsset>.Count => _resources.Count; |
| 69 | + IEnumerator<ResourceAsset> IEnumerable<ResourceAsset>.GetEnumerator() => _resources.GetEnumerator(); |
| 70 | + IEnumerator IEnumerable.GetEnumerator() => _resources.GetEnumerator(); |
| 71 | +} |
0 commit comments