12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Text.RegularExpressions;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Protocol.Infrastructures;
- namespace Vinno.vCloud.Common.Storage
- {
- public class FileTokenInfo
- {
- /// <summary>
- /// The token split characters
- /// </summary>
- public static string[] TokenSplitor = new string[] { "!U$" };
- /// <summary>
- /// The storage type
- /// </summary>
- public StorageType StorageType { get; }
- /// <summary>
- /// Token id for Vinno Storage service
- /// </summary>
- public string TokenId { get; }
- /// <summary>
- /// If 'StorageType' is default,this is vinno storage url, for example:cloud.xinglinghui.com:9097
- /// If 'StorageType' is ufile, this is a ufile download url, for example:http://download.fis.plus/Flyinsono_Windows_1.7.21.19487.exe
- /// </summary>
- public string Url { get; private set; }
- public FileTokenInfo(string fileTokenString)
- {
- //parse file token string, formates are
- //Default|cloud.xinglinghui.com:9097/TokenId
- //UFile{TokenSplitor}http://download.fis.plus/Flyinsono_Windows_1.7.21.19487.exe
- //var infors = Regex.Split(fileTokenString, TokenSplitor, RegexOptions.IgnoreCase);
- var infors = fileTokenString?.Split(TokenSplitor, StringSplitOptions.None);
- if (infors!=null&&infors.Length == 2)
- {
- if (Enum.TryParse<StorageType>(infors[0], out var type))
- {
- StorageType = type;
- }
- if (StorageType == StorageType.ObjectStorage)
- {
- Url = infors[1];
- var fileName = string.Empty;
- var values = Url.Split('_');
- var length = values.Length;
- if (length > 1)
- {
- fileName = values[length - 1];
- }
- var urlValues = fileName.Split('.');
- if (urlValues.Length > 1)
- {
- TokenId = urlValues[0];
- }
- }
- else
- {
- var subInfors = infors[1].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- if (subInfors.Length == 2)
- {
- Url = subInfors[0];
- TokenId = subInfors[1];
- }
- else
- {
- Logger.WriteLineWarn($"Invalid file token string {fileTokenString}");
- }
- }
- }
- else
- {
- Logger.WriteLineWarn($"Invalid file token string em {fileTokenString}");
- }
- }
- /// <summary>
- /// update url
- /// </summary>
- /// <param name="newUrl"></param>
- public void UpdateUrl(string newUrl)
- {
- Url = newUrl;
- }
- }
- }
|