| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EmbeddedCountryFlagProvider.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.IO; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Reflection; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Geography.Resources; |
| | | 15 | | |
| | | 16 | | public sealed class EmbeddedCountryFlagProvider() : EmbeddedCountryFlagProviderBase(typeof(EmbeddedCountryFlagProvider). |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides country flags from embedded resources in an assembly. The expected resource naming convention is "fr-32.png |
| | | 20 | | /// </summary> |
| | | 21 | | public abstract class EmbeddedCountryFlagProviderBase(Assembly assembly) : ICountryFlagProvider |
| | | 22 | | { |
| | | 23 | | // key = "fr-32" |
| | 9 | 24 | | private readonly Dictionary<string, string> _index = BuildIndex(assembly); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Builds an index of available flag resources in the assembly. The index maps a key (e.g., "fr-32") to the full re |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="assembly">The assembly containing the embedded flag resources.</param> |
| | | 30 | | /// <returns>A dictionary mapping flag keys to resource names.</returns> |
| | | 31 | | private static Dictionary<string, string> BuildIndex(Assembly assembly) |
| | 9 | 32 | | => assembly.GetManifestResourceNames() |
| | 9 | 33 | | .Where(static n => n.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) |
| | 9 | 34 | | .ToDictionary( |
| | 9 | 35 | | static n => BuildKey(n), |
| | 9 | 36 | | static n => n, |
| | 9 | 37 | | StringComparer.OrdinalIgnoreCase); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Builds a key for the flag resource based on its name. The key is derived from the file name of the resource, exc |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="resourceName">The name of the resource.</param> |
| | | 43 | | /// <returns>The key for the flag resource.</returns> |
| | | 44 | | private static string BuildKey(string resourceName) |
| | | 45 | | { |
| | | 46 | | // Manifest resource names use dots as separators (not directory separators). |
| | | 47 | | // Example: MyNet.Geography.Flags.32x32.fr32.png => key: fr32 |
| | 13554 | 48 | | var withoutExtension = Path.GetFileNameWithoutExtension(resourceName); |
| | 13554 | 49 | | var lastSeparator = withoutExtension.LastIndexOf('.'); |
| | 13554 | 50 | | var file = lastSeparator >= 0 ? withoutExtension[(lastSeparator + 1)..] : withoutExtension; |
| | | 51 | | |
| | | 52 | | // expected: fr32, us64 etc. |
| | 13554 | 53 | | return file.ToLower(CultureInfo.CurrentCulture); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Opens a stream to the flag image for the specified country and size. The method constructs a key based on the co |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="country">The country for which to retrieve the flag.</param> |
| | | 60 | | /// <param name="size">The size of the flag to retrieve.</param> |
| | | 61 | | /// <returns>A stream to the flag image, or null if the flag is not available.</returns> |
| | | 62 | | public Stream? Open(Country country, FlagSize size) |
| | | 63 | | { |
| | 12 | 64 | | var key = $"{country.Alpha2}{(int)size}".ToLower(CultureInfo.CurrentCulture); |
| | | 65 | | |
| | 12 | 66 | | return !_index.TryGetValue(key, out var resource) ? null : assembly.GetManifestResourceStream(resource); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the flag image as a byte array for the specified country and size. This method internally calls the Open me |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="country">The country for which to retrieve the flag.</param> |
| | | 73 | | /// <param name="size">The size of the flag to retrieve.</param> |
| | | 74 | | /// <returns>A byte array containing the flag image, or null if the flag is not available.</returns> |
| | | 75 | | public byte[]? GetBytes(Country country, FlagSize size) |
| | | 76 | | { |
| | 6 | 77 | | using var stream = Open(country, size); |
| | 9 | 78 | | if (stream is null) return null; |
| | | 79 | | |
| | 3 | 80 | | using var ms = new MemoryStream(); |
| | 3 | 81 | | stream.CopyTo(ms); |
| | 3 | 82 | | return ms.ToArray(); |
| | 6 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |