123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO.Packaging;
- using System.Linq;
- using System.Text;
- using Newtonsoft.Json;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Common.Storage;
- using Vinno.vCloud.Common.Storage.Download;
- using Vinno.vCloud.Common.Storage.ObjectStorageInfo;
- using Vinno.vCloud.Common.Storage.Upload;
- using Vinno.vCloud.Protocol.Initializers;
- using Vinno.vCloud.Protocol.Messages.Client.Chat;
- using Vinno.vCloud.Protocol.Messages.Client.Storage;
- using StorageInfo = Vinno.vCloud.Common.Storage.Upload.StorageInfo;
- using System.IO;
- using Vinno.IUS.Common.Log;
- using System.Diagnostics;
- using System.Threading;
- namespace Storage
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private readonly string _filePath;
- private ClientLeaf _clientLeaf;
- private readonly object processLock = new object();
- private object _storageInfoLocker = new object();
- private StorageInfo _storageInfo;
- private CancellationTokenSource _cancel;
- private string serverAddress = "192.168.6.103";
- private IList<string> _tokens;
- public MainWindow()
- {
- InitializeComponent();
- _filePath = "D:\\LargeFiles2\\Test.zip";
- _tokens = new List<string>();
- TokenId.Text = "";
- Count.Text = "";
- var tcpCreator = new TcpCreator(serverAddress, 9096);
- _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
- ClientTagsInitializer.Initialize();
- _storageInfo = GetStorageInfo();
- UploadHelper.GetAuthorization = GetAuthentication;
-
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- _tokens = new List<string>();
- var tokenText = TokenId.Text;
- var tokenId = TokenId.Text + ".dat.part";
- var count = int.Parse(Count.Text);
- for (int i = 0; i < count; i++)
- {
- var token = string.Empty;
- if (_storageInfo.StorageType == Vinno.vCloud.Protocol.Infrastructures.StorageType.ObjectStorage)
- {
- token = "1" + FileTokenInfo.TokenSplitor[0] + @"http://flyinsono-bj-1300984704.cos.ap-beijing.myqcloud.com/" + tokenId + i.ToString();
- }
- else
- {
- token = "0" + FileTokenInfo.TokenSplitor[0] + serverAddress+":9097/" + tokenText;
- }
- _tokens.Add(token);
- }
- Task.Run(()=>
- {
- var watch = new Stopwatch();
- watch.Start();
- _cancel = new CancellationTokenSource();
- DownloadHelper.GetLargeFile(_tokens, _filePath, OnProgress,_cancel);
- watch.Stop();
- Dispatcher.Invoke(() =>
- {
- TextBlock3.Text = "Spend time: " + watch.ElapsedMilliseconds.ToString() + " ms";
- });
- });
- }
- private void OnProgress(double s)
- {
- lock (processLock)
- {
- Dispatcher.Invoke(() =>
- {
- TextBlock.Text = $"{Math.Round(s * 100, 3)}%";
- });
- }
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- OpenFileDialog fileDialog = new OpenFileDialog();
- fileDialog.Multiselect = true;
- fileDialog.Title = "请选择文件";
- fileDialog.Filter = "所有文件(*.*)|*.*";
- if (fileDialog.ShowDialog() == true)
- {
- string[] names = fileDialog.FileNames;
- TextBlock2.Text = names[0];
- }
- }
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- var filePath = TextBlock2.Text;
- var t = Task.Run(()=>
- {
- var watch = new Stopwatch();
- watch.Start();
- var storageInfo = GetStorageInfo();
- var result = UploadHelper.UploadLargeFile(storageInfo,filePath, "D://UploadFilesCache",OnProgress);
- watch.Stop();
- Dispatcher.Invoke(() =>
- {
- TextBlock3.Text = "Spend time: " + watch.ElapsedMilliseconds.ToString() + " ms";
- });
-
- });
- //t.Dispose();
- }
- private string GetAuthentication(string fileName)
- {
- try
- {
- var authorization = string.Empty;
- var getAuthorizationRequest = new GetAuthorizationRequest();
- getAuthorizationRequest.FileName = fileName;
- var storageResult = _clientLeaf.Send(getAuthorizationRequest);
- var getAuthorizationResult = GetAuthorizationResult.Convert(storageResult);
- if (getAuthorizationResult != null)
- {
- authorization = getAuthorizationResult.Authorization;
- }
- return authorization;
- }
- catch (Exception e)
- {
- throw new Exception("get authentication form server error");
- }
- }
- private StorageInfo GetStorageInfo()
- {
- lock (_storageInfoLocker)
- {
- if (_storageInfo == null)
- {
- using (var request = MessagePool.GetMessage<GetStorageServerRequest2>())
- {
- var result = _clientLeaf.Send(request);
- var getDisplayServerInfoResult = GetStorageServerResult2.Convert(result);
- if (getDisplayServerInfoResult != null)
- {
- var url = getDisplayServerInfoResult.ServerUrl;
- var storageType = getDisplayServerInfoResult.StorageType;
- var config = getDisplayServerInfoResult.Config;
- _storageInfo = new StorageInfo()
- {
- Url = url,
- StorageType = storageType,
- Config = config
- };
- NodeMapping.NodeMappingInilization(_storageInfo.StorageNodeItems);
- }
- }
- }
- return _storageInfo;
- }
- }
- /// <summary>
- /// 合并文件
- /// </summary>
- /// <param name="filePaths">要合并的文件列表</param>
- /// <param name="combineFile">合并后的文件路径带文件名</param>
- static bool CombineFiles(List<string> filePaths, string combineFile, Action<double> progress = null)
- {
- try
- {
- var fileDictionary = new Dictionary<int, string>();
- foreach (var f in filePaths)
- {
- if (int.TryParse(f.Substring(f.LastIndexOf("part") + 4), out int key))
- {
- fileDictionary.Add(key, f);
- }
- else
- {
- Logger.WriteLineError($"TryParse part error in {f}");
- return false;
- }
- }
- var count = filePaths.Count();
- var progressValue = 0.0;
- using (FileStream CombineStream = new FileStream(combineFile, FileMode.OpenOrCreate))
- {
- using (BinaryWriter CombineWriter = new BinaryWriter(CombineStream))
- {
- for (var i = 0; i < count; i++)
- {
- var file = fileDictionary.FirstOrDefault(x => x.Key == i).Value;
- using (FileStream fileStream = new FileStream(file, FileMode.Open))
- {
- using (BinaryReader fileReader = new BinaryReader(fileStream))
- {
- byte[] TempBytes = fileReader.ReadBytes((int)fileStream.Length);
- CombineWriter.Write(TempBytes);
- }
- }
- progressValue += 1.0 / count ;
- progress?.Invoke(progressValue);
- }
- return true;
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineInfo($"CombineFiles exception:{ex}");
- return false;
- }
- }
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- var t=Task.Run(()=>
- {
- var targetFilesPath = "D:\\UploadFilesCache\\Cutfile";
- var files = Directory.GetFiles(targetFilesPath);
- CombineFiles(files.ToList(), "D:\\UploadFilesCache\\Test.zip", OnProgress);
- });
- t.Dispose();
- }
- private void Button_Click_4(object sender, RoutedEventArgs e)
- {
- _cancel?.Cancel();
- _cancel?.Dispose();
- }
- }
- }
|