EmbeddedResourceManager.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using Abp.Collections.Extensions;
  4. using Abp.Dependency;
  5. using System.IO;
  6. using System.Linq;
  7. namespace Abp.Resources.Embedded
  8. {
  9. public class EmbeddedResourceManager : IEmbeddedResourceManager, ISingletonDependency
  10. {
  11. private readonly IEmbeddedResourcesConfiguration _configuration;
  12. private readonly Lazy<Dictionary<string, EmbeddedResourceItem>> _resources;
  13. public EmbeddedResourceManager(IEmbeddedResourcesConfiguration configuration)
  14. {
  15. _configuration = configuration;
  16. _resources = new Lazy<Dictionary<string, EmbeddedResourceItem>>(
  17. CreateResourcesDictionary,
  18. true
  19. );
  20. }
  21. /// <inheritdoc/>
  22. public EmbeddedResourceItem GetResource(string fullPath)
  23. {
  24. var encodedPath = EmbeddedResourcePathHelper.EncodeAsResourcesPath(fullPath);
  25. return _resources.Value.GetOrDefault(encodedPath);
  26. }
  27. public IEnumerable<EmbeddedResourceItem> GetResources(string fullPath)
  28. {
  29. var encodedPath = EmbeddedResourcePathHelper.EncodeAsResourcesPath(fullPath);
  30. if (encodedPath.Length > 0 && !encodedPath.EndsWith("."))
  31. {
  32. encodedPath = encodedPath + ".";
  33. }
  34. // We will assume that any file starting with this path, is in that directory.
  35. // NOTE: This may include false positives, but helps in the majority of cases until
  36. // https://github.com/aspnet/FileSystem/issues/184 is solved.
  37. return _resources.Value.Where(k => k.Key.StartsWith(encodedPath)).Select(d => d.Value);
  38. }
  39. private Dictionary<string, EmbeddedResourceItem> CreateResourcesDictionary()
  40. {
  41. var resources = new Dictionary<string, EmbeddedResourceItem>(StringComparer.OrdinalIgnoreCase);
  42. foreach (var source in _configuration.Sources)
  43. {
  44. source.AddResources(resources);
  45. }
  46. return resources;
  47. }
  48. }
  49. }