MainWindow.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO.Packaging;
  5. using System.Linq;
  6. using System.Text;
  7. using Newtonsoft.Json;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using Vinno.IUS.Common.Network.Leaf;
  19. using Vinno.IUS.Common.Network.Tcp;
  20. using Vinno.IUS.Common.Network.Transfer;
  21. using Vinno.vCloud.Common.Storage;
  22. using Vinno.vCloud.Common.Storage.Download;
  23. using Vinno.vCloud.Common.Storage.ObjectStorageInfo;
  24. using Vinno.vCloud.Common.Storage.Upload;
  25. using Vinno.vCloud.Protocol.Initializers;
  26. using Vinno.vCloud.Protocol.Messages.Client.Chat;
  27. using Vinno.vCloud.Protocol.Messages.Client.Storage;
  28. using StorageInfo = Vinno.vCloud.Common.Storage.Upload.StorageInfo;
  29. using System.IO;
  30. using Vinno.IUS.Common.Log;
  31. using System.Diagnostics;
  32. using System.Threading;
  33. namespace Storage
  34. {
  35. /// <summary>
  36. /// MainWindow.xaml 的交互逻辑
  37. /// </summary>
  38. public partial class MainWindow : Window
  39. {
  40. private readonly string _filePath;
  41. private ClientLeaf _clientLeaf;
  42. private readonly object processLock = new object();
  43. private object _storageInfoLocker = new object();
  44. private StorageInfo _storageInfo;
  45. private CancellationTokenSource _cancel;
  46. private string serverAddress = "192.168.6.103";
  47. private IList<string> _tokens;
  48. public MainWindow()
  49. {
  50. InitializeComponent();
  51. _filePath = "D:\\LargeFiles2\\Test.zip";
  52. _tokens = new List<string>();
  53. TokenId.Text = "";
  54. Count.Text = "";
  55. var tcpCreator = new TcpCreator(serverAddress, 9096);
  56. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
  57. ClientTagsInitializer.Initialize();
  58. _storageInfo = GetStorageInfo();
  59. UploadHelper.GetAuthorization = GetAuthentication;
  60. }
  61. private void Button_Click(object sender, RoutedEventArgs e)
  62. {
  63. _tokens = new List<string>();
  64. var tokenText = TokenId.Text;
  65. var tokenId = TokenId.Text + ".dat.part";
  66. var count = int.Parse(Count.Text);
  67. for (int i = 0; i < count; i++)
  68. {
  69. var token = string.Empty;
  70. if (_storageInfo.StorageType == Vinno.vCloud.Protocol.Infrastructures.StorageType.ObjectStorage)
  71. {
  72. token = "1" + FileTokenInfo.TokenSplitor[0] + @"http://flyinsono-bj-1300984704.cos.ap-beijing.myqcloud.com/" + tokenId + i.ToString();
  73. }
  74. else
  75. {
  76. token = "0" + FileTokenInfo.TokenSplitor[0] + serverAddress+":9097/" + tokenText;
  77. }
  78. _tokens.Add(token);
  79. }
  80. Task.Run(()=>
  81. {
  82. var watch = new Stopwatch();
  83. watch.Start();
  84. _cancel = new CancellationTokenSource();
  85. DownloadHelper.GetLargeFile(_tokens, _filePath, OnProgress,_cancel);
  86. watch.Stop();
  87. Dispatcher.Invoke(() =>
  88. {
  89. TextBlock3.Text = "Spend time: " + watch.ElapsedMilliseconds.ToString() + " ms";
  90. });
  91. });
  92. }
  93. private void OnProgress(double s)
  94. {
  95. lock (processLock)
  96. {
  97. Dispatcher.Invoke(() =>
  98. {
  99. TextBlock.Text = $"{Math.Round(s * 100, 3)}%";
  100. });
  101. }
  102. }
  103. private void Button_Click_1(object sender, RoutedEventArgs e)
  104. {
  105. OpenFileDialog fileDialog = new OpenFileDialog();
  106. fileDialog.Multiselect = true;
  107. fileDialog.Title = "请选择文件";
  108. fileDialog.Filter = "所有文件(*.*)|*.*";
  109. if (fileDialog.ShowDialog() == true)
  110. {
  111. string[] names = fileDialog.FileNames;
  112. TextBlock2.Text = names[0];
  113. }
  114. }
  115. private void Button_Click_2(object sender, RoutedEventArgs e)
  116. {
  117. var filePath = TextBlock2.Text;
  118. var t = Task.Run(()=>
  119. {
  120. var watch = new Stopwatch();
  121. watch.Start();
  122. var storageInfo = GetStorageInfo();
  123. var result = UploadHelper.UploadLargeFile(storageInfo,filePath, "D://UploadFilesCache",OnProgress);
  124. watch.Stop();
  125. Dispatcher.Invoke(() =>
  126. {
  127. TextBlock3.Text = "Spend time: " + watch.ElapsedMilliseconds.ToString() + " ms";
  128. });
  129. });
  130. //t.Dispose();
  131. }
  132. private string GetAuthentication(string fileName)
  133. {
  134. try
  135. {
  136. var authorization = string.Empty;
  137. var getAuthorizationRequest = new GetAuthorizationRequest();
  138. getAuthorizationRequest.FileName = fileName;
  139. var storageResult = _clientLeaf.Send(getAuthorizationRequest);
  140. var getAuthorizationResult = GetAuthorizationResult.Convert(storageResult);
  141. if (getAuthorizationResult != null)
  142. {
  143. authorization = getAuthorizationResult.Authorization;
  144. }
  145. return authorization;
  146. }
  147. catch (Exception e)
  148. {
  149. throw new Exception("get authentication form server error");
  150. }
  151. }
  152. private StorageInfo GetStorageInfo()
  153. {
  154. lock (_storageInfoLocker)
  155. {
  156. if (_storageInfo == null)
  157. {
  158. using (var request = MessagePool.GetMessage<GetStorageServerRequest2>())
  159. {
  160. var result = _clientLeaf.Send(request);
  161. var getDisplayServerInfoResult = GetStorageServerResult2.Convert(result);
  162. if (getDisplayServerInfoResult != null)
  163. {
  164. var url = getDisplayServerInfoResult.ServerUrl;
  165. var storageType = getDisplayServerInfoResult.StorageType;
  166. var config = getDisplayServerInfoResult.Config;
  167. _storageInfo = new StorageInfo()
  168. {
  169. Url = url,
  170. StorageType = storageType,
  171. Config = config
  172. };
  173. NodeMapping.NodeMappingInilization(_storageInfo.StorageNodeItems);
  174. }
  175. }
  176. }
  177. return _storageInfo;
  178. }
  179. }
  180. /// <summary>
  181. /// 合并文件
  182. /// </summary>
  183. /// <param name="filePaths">要合并的文件列表</param>
  184. /// <param name="combineFile">合并后的文件路径带文件名</param>
  185. static bool CombineFiles(List<string> filePaths, string combineFile, Action<double> progress = null)
  186. {
  187. try
  188. {
  189. var fileDictionary = new Dictionary<int, string>();
  190. foreach (var f in filePaths)
  191. {
  192. if (int.TryParse(f.Substring(f.LastIndexOf("part") + 4), out int key))
  193. {
  194. fileDictionary.Add(key, f);
  195. }
  196. else
  197. {
  198. Logger.WriteLineError($"TryParse part error in {f}");
  199. return false;
  200. }
  201. }
  202. var count = filePaths.Count();
  203. var progressValue = 0.0;
  204. using (FileStream CombineStream = new FileStream(combineFile, FileMode.OpenOrCreate))
  205. {
  206. using (BinaryWriter CombineWriter = new BinaryWriter(CombineStream))
  207. {
  208. for (var i = 0; i < count; i++)
  209. {
  210. var file = fileDictionary.FirstOrDefault(x => x.Key == i).Value;
  211. using (FileStream fileStream = new FileStream(file, FileMode.Open))
  212. {
  213. using (BinaryReader fileReader = new BinaryReader(fileStream))
  214. {
  215. byte[] TempBytes = fileReader.ReadBytes((int)fileStream.Length);
  216. CombineWriter.Write(TempBytes);
  217. }
  218. }
  219. progressValue += 1.0 / count ;
  220. progress?.Invoke(progressValue);
  221. }
  222. return true;
  223. }
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. Logger.WriteLineInfo($"CombineFiles exception:{ex}");
  229. return false;
  230. }
  231. }
  232. private void Button_Click_3(object sender, RoutedEventArgs e)
  233. {
  234. var t=Task.Run(()=>
  235. {
  236. var targetFilesPath = "D:\\UploadFilesCache\\Cutfile";
  237. var files = Directory.GetFiles(targetFilesPath);
  238. CombineFiles(files.ToList(), "D:\\UploadFilesCache\\Test.zip", OnProgress);
  239. });
  240. t.Dispose();
  241. }
  242. private void Button_Click_4(object sender, RoutedEventArgs e)
  243. {
  244. _cancel?.Cancel();
  245. _cancel?.Dispose();
  246. }
  247. }
  248. }