InternalResourceLoader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace FastReport.Web.Services
  10. {
  11. /// <summary>
  12. /// Internal implementation of <see cref="IResourceLoader"/>.
  13. /// Loads requested resources from embedded resources of the current assembly
  14. /// </summary>
  15. internal sealed class InternalResourceLoader : IResourceLoader
  16. {
  17. static readonly string AssemblyName;
  18. static readonly Assembly _assembly;
  19. static InternalResourceLoader()
  20. {
  21. _assembly = Assembly.GetExecutingAssembly();
  22. AssemblyName = _assembly.GetName().Name;
  23. }
  24. readonly ConcurrentDictionary<string, string> cache1 = new ConcurrentDictionary<string, string>();
  25. readonly ConcurrentDictionary<string, byte[]> cache2 = new ConcurrentDictionary<string, byte[]>();
  26. public string GetContent(string name)
  27. {
  28. if (cache1.TryGetValue(name, out string value))
  29. return value;
  30. var fullname = $"{AssemblyName}.Resources.{name}";
  31. var resourceStream = _assembly.GetManifestResourceStream(fullname);
  32. if (resourceStream == null)
  33. return null;
  34. using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
  35. {
  36. var res = reader.ReadToEnd();
  37. cache1[name] = res;
  38. return res;
  39. }
  40. }
  41. public async ValueTask<string> GetContentAsync(string name)
  42. {
  43. if (cache1.TryGetValue(name, out string value))
  44. return value;
  45. var fullname = $"{AssemblyName}.Resources.{name}";
  46. var resourceStream = _assembly.GetManifestResourceStream(fullname);
  47. if (resourceStream == null)
  48. return null;
  49. using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
  50. {
  51. var res = await reader.ReadToEndAsync();
  52. cache1[name] = res;
  53. return res;
  54. }
  55. }
  56. public byte[] GetBytes(string name)
  57. {
  58. if (cache2.TryGetValue(name, out byte[] value))
  59. return value;
  60. var fullname = $"{AssemblyName}.Resources.{name}";
  61. var resourceStream = _assembly.GetManifestResourceStream(fullname);
  62. if (resourceStream == null)
  63. return null;
  64. var buffer = new byte[resourceStream.Length];
  65. resourceStream.Read(buffer, 0, buffer.Length);
  66. cache2[name] = buffer;
  67. return buffer;
  68. }
  69. public async ValueTask<byte[]> GetBytesAsync(string name, CancellationToken cancellationToken = default)
  70. {
  71. if (cache2.TryGetValue(name, out byte[] value))
  72. return value;
  73. var fullname = $"{AssemblyName}.Resources.{name}";
  74. var resourceStream = _assembly.GetManifestResourceStream(fullname);
  75. if (resourceStream == null)
  76. return null;
  77. var buffer = new byte[resourceStream.Length];
  78. await resourceStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
  79. cache2[name] = buffer;
  80. return buffer;
  81. }
  82. }
  83. }