12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System.Collections.Generic;
- using System.Linq;
- using Vinno.IUS.Common.Log;
- namespace Vinno.vCloud.Common.Storage.ObjectStorageInfo
- {
- public static class NodeMapping
- {
- private static Dictionary<string, StorageNodeInfo> _storageNodeInfos = new Dictionary<string, StorageNodeInfo>();
- public static void NodeMappingInilization(IList<StorageNodeItem> storageNodes)
- {
- if (storageNodes == null)
- {
- return;
- }
- foreach (var storageNode in storageNodes)
- {
- var storageNodeInfo = storageNode.StorageNodeInfo;
- if (storageNodeInfo != null)
- {
- if (!_storageNodeInfos.ContainsKey(storageNodeInfo.Default))
- {
- _storageNodeInfos.Add(storageNodeInfo.Default, storageNodeInfo);
- }
- if (!_storageNodeInfos.ContainsKey(storageNodeInfo.CDN))
- {
- _storageNodeInfos.Add(storageNodeInfo.CDN, storageNodeInfo);
- }
- if (!_storageNodeInfos.ContainsKey(storageNodeInfo.Inner))
- {
- _storageNodeInfos.Add(storageNodeInfo.Inner, storageNodeInfo);
- }
- }
- }
- }
- /// <summary>
- /// Inner/Default url=> CDN url
- /// </summary>
- /// <param name="url"></param>
- public static string DoCDNMapping(string url, bool forceHttp = false)
- {
- var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
- if (key != null)
- {
- var result = url.Replace(key, _storageNodeInfos[key].CDN);
- if (result.Contains("https"))
- {
- result = result.Replace("https", "http");
- }
- // Logger.WriteLineInfo($"url:{url} DoCDNMapping result:{result}");
- return result;
- }
- return url;
- }
- /// <summary>
- /// CDN/Default url => Inner
- /// </summary>
- /// <param name="url"></param>
- public static string DoInnerMapping(string url)
- {
- var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
- if (key != null)
- {
- var result = url.Replace(key, _storageNodeInfos[key].Inner);
- // Logger.WriteLineInfo($"url:{url} DoInnerMapping result:{result}");
- return result;
- }
- return url;
- }
- /// <summary>
- /// Inner/CDN => Default
- /// </summary>
- /// <param name="url"></param>
- public static string DoDefaultMapping(string url)
- {
- var key = _storageNodeInfos.Keys.FirstOrDefault(x => url.Contains(x));
- if (key != null)
- {
- var result = url.Replace(key, _storageNodeInfos[key].Default);
- Logger.WriteLineInfo($"url:{url} DoDefaultMapping result:{result}");
- return result;
- }
- return key;
- }
- }
- }
|