NodeMapping.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Vinno.IUS.Common.Log;
  4. namespace Vinno.vCloud.Common.Storage.ObjectStorageInfo
  5. {
  6. public static class NodeMapping
  7. {
  8. private static Dictionary<string, StorageNodeInfo> _storageNodeInfos = new Dictionary<string, StorageNodeInfo>();
  9. public static void NodeMappingInilization(IList<StorageNodeItem> storageNodes)
  10. {
  11. if (storageNodes == null)
  12. {
  13. return;
  14. }
  15. foreach (var storageNode in storageNodes)
  16. {
  17. var storageNodeInfo = storageNode.StorageNodeInfo;
  18. if (storageNodeInfo != null)
  19. {
  20. if (!_storageNodeInfos.ContainsKey(storageNodeInfo.Default))
  21. {
  22. _storageNodeInfos.Add(storageNodeInfo.Default, storageNodeInfo);
  23. }
  24. if (!_storageNodeInfos.ContainsKey(storageNodeInfo.CDN))
  25. {
  26. _storageNodeInfos.Add(storageNodeInfo.CDN, storageNodeInfo);
  27. }
  28. if (!_storageNodeInfos.ContainsKey(storageNodeInfo.Inner))
  29. {
  30. _storageNodeInfos.Add(storageNodeInfo.Inner, storageNodeInfo);
  31. }
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// Inner/Default url=> CDN url
  37. /// </summary>
  38. /// <param name="url"></param>
  39. public static string DoCDNMapping(string url, bool forceHttp = false)
  40. {
  41. var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
  42. if (key != null)
  43. {
  44. var result = url.Replace(key, _storageNodeInfos[key].CDN);
  45. if (result.Contains("https"))
  46. {
  47. result = result.Replace("https", "http");
  48. }
  49. // Logger.WriteLineInfo($"url:{url} DoCDNMapping result:{result}");
  50. return result;
  51. }
  52. return url;
  53. }
  54. /// <summary>
  55. /// CDN/Default url => Inner
  56. /// </summary>
  57. /// <param name="url"></param>
  58. public static string DoInnerMapping(string url)
  59. {
  60. var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
  61. if (key != null)
  62. {
  63. var result = url.Replace(key, _storageNodeInfos[key].Inner);
  64. // Logger.WriteLineInfo($"url:{url} DoInnerMapping result:{result}");
  65. return result;
  66. }
  67. return url;
  68. }
  69. /// <summary>
  70. /// Inner/CDN => Default
  71. /// </summary>
  72. /// <param name="url"></param>
  73. public static string DoDefaultMapping(string url)
  74. {
  75. var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
  76. if (key != null)
  77. {
  78. var result = url.Replace(key, _storageNodeInfos[key].Default);
  79. Logger.WriteLineInfo($"url:{url} DoDefaultMapping result:{result}");
  80. return result;
  81. }
  82. return key;
  83. }
  84. }
  85. }