using System;
using System.Text.RegularExpressions;
using Vinno.IUS.Common.Log;
using Vinno.vCloud.Protocol.Infrastructures;
namespace Vinno.vCloud.Common.Storage
{
public class FileTokenInfo
{
///
/// The token split characters
///
public static string[] TokenSplitor = new string[] { "!U$" };
///
/// The storage type
///
public StorageType StorageType { get; }
///
/// Token id for Vinno Storage service
///
public string TokenId { get; }
///
/// 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
///
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(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}");
}
}
///
/// update url
///
///
public void UpdateUrl(string newUrl)
{
Url = newUrl;
}
}
}