WinDownload.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Aliyun.OSS;
  2. using Aliyun.OSS.Common;
  3. using SAGY.Models;
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Forms;
  12. using SAGA.Models;
  13. using Utilities;
  14. using Application = System.Windows.Application;
  15. using Path = System.IO.Path;
  16. using StaticData = SAGA.MBI.StaticData;
  17. namespace UploadToCloud
  18. {
  19. /// <summary>
  20. /// WinLogin.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class WinDownload : Window {
  23. private readonly string m_fileName;
  24. string DirectoryName => Path.Combine(StaticData.ProjectsRoot, StaticData.ProjId);
  25. static string accessKeyId = Config.AccessKeyId;
  26. static string accessKeySecret = Config.AccessKeySecret;
  27. static string endpoint = Config.Endpoint;
  28. const string bucketName = "prod-data";
  29. static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  30. public WinDownload() {
  31. InitializeComponent();
  32. m_fileName = StaticData.ProjId + ".rar";
  33. }
  34. private void btnLogin_Click(object sender, RoutedEventArgs e) {
  35. this.btnDownload.IsEnabled = false;
  36. if (string.IsNullOrEmpty(StaticData.ProjectsRoot))
  37. SetRoot();
  38. var filePath = Path.Combine(StaticData.ProjectsRoot, m_fileName);
  39. lblpresent.Visibility = Visibility.Visible;
  40. if (IsExist()) {
  41. // GetObjectProgress(filePath);
  42. DownloadAndUppack();
  43. }
  44. else {
  45. //提示是否重新选择目录
  46. //创建本地目录
  47. CreateLocalDirectory();
  48. //修改显示信息
  49. lblTitle.Content = "未找到本地文件,正在下载云平台数据";
  50. lblTips.Content = "用于创建本地账号,并储存修改的数据";
  51. for (int i = 0; i < 100; i++) {
  52. ShowProgress(i);
  53. Thread.Sleep(100);
  54. }
  55. Start();
  56. }
  57. }
  58. private void CreateLocalDirectory() {
  59. //创建建筑体目录
  60. foreach (var buildingName in StaticData.ProjectInfo.BuildingNames) {
  61. Directory.CreateDirectory(Path.Combine(StaticData.RevitDirectory, buildingName));
  62. }
  63. //创建数据库目录
  64. Directory.CreateDirectory(StaticData.DbDirectory);
  65. }
  66. public void GetObjectProgress(string filePath) {
  67. string key = $"test/files/revit/{m_fileName}";
  68. try {
  69. var getObjectRequest = new GetObjectRequest(bucketName, key);
  70. getObjectRequest.StreamTransferProgress += StreamProgressCallback;
  71. var ossObject = client.GetObject(getObjectRequest);
  72. using (var stream = ossObject.Content) {
  73. var buffer = new byte[1024 * 1024];
  74. var bytesTotal = 0;
  75. var bytesRead = 0;
  76. using (var fs = File.Open(filePath, FileMode.OpenOrCreate)) {
  77. while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
  78. bytesTotal += bytesRead;
  79. // Process read data
  80. // TODO
  81. fs.Write(buffer, 0, bytesRead);
  82. }
  83. }
  84. }
  85. UnpackFiles();
  86. Debug.WriteLine("Get object:{0} succeeded", key);
  87. }
  88. catch (OssException ex) {
  89. Debug.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  90. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  91. }
  92. catch (Exception ex) {
  93. Debug.WriteLine("Failed with error info: {0}", ex.Message);
  94. }
  95. }
  96. public void DownloadAndUppack() {
  97. string key = $"test/files/revit/{m_fileName}";
  98. try {
  99. var getObjectRequest = new GetObjectRequest(bucketName, key);
  100. getObjectRequest.StreamTransferProgress += StreamProgressCallback;
  101. var ossObject = client.GetObject(getObjectRequest);
  102. using (var stream = ossObject.Content) {
  103. var buffer = new byte[1024 * 1024];
  104. var bytesTotal = 0;
  105. var bytesRead = 0;
  106. //默认为512M,可以使用MemoryStream(1000*1024*1024)扩大
  107. using (var fs = new System.IO.MemoryStream()) {
  108. while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
  109. bytesTotal += bytesRead;
  110. // Process read data
  111. // TODO
  112. fs.Write(buffer, 0, bytesRead);
  113. }
  114. SharpZip.UnpackStreamFiles(fs, DirectoryName);
  115. }
  116. }
  117. Debug.WriteLine("Get object:{0} succeeded", key);
  118. }
  119. catch (OssException ex) {
  120. Debug.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  121. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  122. }
  123. catch (Exception ex) {
  124. Debug.WriteLine("Failed with error info: {0}", ex.Message);
  125. }
  126. }
  127. private void StreamProgressCallback(object sender, StreamTransferProgressArgs args) {
  128. var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
  129. ShowProgress(currentData);
  130. Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
  131. args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData);
  132. //下载完成,启动revit
  133. if (currentData == 100d) {
  134. Start();
  135. }
  136. }
  137. private void ShowProgress(double currentData) {
  138. lblpresent.Content = currentData + "%";
  139. Canvas.SetLeft(lblpresent, 81 + (progressBar.Width - lblpresent.Width) / 100 * currentData);
  140. Dispatcher.Invoke(new Action<DependencyProperty, object>((db, value) => this.progressBar.SetValue(db, value)),
  141. System.Windows.Threading.DispatcherPriority.Background,
  142. RangeBase.ValueProperty, Convert.ToDouble(currentData));
  143. }
  144. private void UnpackFiles() {
  145. //文件解压
  146. var filePath = Path.Combine(StaticData.ProjectsRoot, m_fileName);
  147. bool isSuccess = SharpZip.UnpackFiles(filePath, DirectoryName);
  148. if (isSuccess) {
  149. File.Delete(filePath);
  150. }
  151. }
  152. bool IsExist() {
  153. string key = $"test/files/revit/{m_fileName}";
  154. try {
  155. return client.DoesObjectExist(bucketName, key);
  156. }
  157. catch (OssException ex) {
  158. Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  159. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  160. }
  161. catch (Exception ex) {
  162. Console.WriteLine("Failed with error info: {0}", ex.Message);
  163. }
  164. return false;
  165. }
  166. private void btnCancel_Click(object sender, RoutedEventArgs e) {
  167. Start();
  168. }
  169. void Start() {
  170. SAGY.Revit.MainWindow window = new SAGY.Revit.MainWindow();
  171. window.Start();
  172. Application.Current.Shutdown();
  173. }
  174. void SetRoot() {
  175. FolderBrowserDialog fbd = new FolderBrowserDialog();
  176. if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
  177. StaticData.ProjectsRoot = fbd.SelectedPath.Trim();
  178. }
  179. }
  180. }