FileTokenInfo.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Vinno.IUS.Common.Log;
  4. using Vinno.vCloud.Protocol.Infrastructures;
  5. namespace Vinno.vCloud.Common.Storage
  6. {
  7. public class FileTokenInfo
  8. {
  9. /// <summary>
  10. /// The token split characters
  11. /// </summary>
  12. public static string[] TokenSplitor = new string[] { "!U$" };
  13. /// <summary>
  14. /// The storage type
  15. /// </summary>
  16. public StorageType StorageType { get; }
  17. /// <summary>
  18. /// Token id for Vinno Storage service
  19. /// </summary>
  20. public string TokenId { get; }
  21. /// <summary>
  22. /// If 'StorageType' is default,this is vinno storage url, for example:cloud.xinglinghui.com:9097
  23. /// If 'StorageType' is ufile, this is a ufile download url, for example:http://download.fis.plus/Flyinsono_Windows_1.7.21.19487.exe
  24. /// </summary>
  25. public string Url { get; private set; }
  26. public FileTokenInfo(string fileTokenString)
  27. {
  28. //parse file token string, formates are
  29. //Default|cloud.xinglinghui.com:9097/TokenId
  30. //UFile{TokenSplitor}http://download.fis.plus/Flyinsono_Windows_1.7.21.19487.exe
  31. //var infors = Regex.Split(fileTokenString, TokenSplitor, RegexOptions.IgnoreCase);
  32. var infors = fileTokenString?.Split(TokenSplitor, StringSplitOptions.None);
  33. if (infors!=null&&infors.Length == 2)
  34. {
  35. if (Enum.TryParse<StorageType>(infors[0], out var type))
  36. {
  37. StorageType = type;
  38. }
  39. if (StorageType == StorageType.ObjectStorage)
  40. {
  41. Url = infors[1];
  42. var fileName = string.Empty;
  43. var values = Url.Split('_');
  44. var length = values.Length;
  45. if (length > 1)
  46. {
  47. fileName = values[length - 1];
  48. }
  49. var urlValues = fileName.Split('.');
  50. if (urlValues.Length > 1)
  51. {
  52. TokenId = urlValues[0];
  53. }
  54. }
  55. else
  56. {
  57. var subInfors = infors[1].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  58. if (subInfors.Length == 2)
  59. {
  60. Url = subInfors[0];
  61. TokenId = subInfors[1];
  62. }
  63. else
  64. {
  65. Logger.WriteLineWarn($"Invalid file token string {fileTokenString}");
  66. }
  67. }
  68. }
  69. else
  70. {
  71. Logger.WriteLineWarn($"Invalid file token string em {fileTokenString}");
  72. }
  73. }
  74. /// <summary>
  75. /// update url
  76. /// </summary>
  77. /// <param name="newUrl"></param>
  78. public void UpdateUrl(string newUrl)
  79. {
  80. Url = newUrl;
  81. }
  82. }
  83. }