| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using Aliyun.OSS;
- using Aliyun.OSS.Common;
- using SAGY.Models;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Forms;
- using SAGA.Models;
- using Utilities;
- using Application = System.Windows.Application;
- using Path = System.IO.Path;
- using StaticData = SAGA.MBI.StaticData;
- namespace UploadToCloud
- {
- /// <summary>
- /// WinLogin.xaml 的交互逻辑
- /// </summary>
- public partial class WinDownload : Window {
- private readonly string m_fileName;
- string DirectoryName => Path.Combine(StaticData.ProjectsRoot, StaticData.ProjId);
- static string accessKeyId = Config.AccessKeyId;
- static string accessKeySecret = Config.AccessKeySecret;
- static string endpoint = Config.Endpoint;
- const string bucketName = "prod-data";
- static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- public WinDownload() {
- InitializeComponent();
- m_fileName = StaticData.ProjId + ".rar";
- }
- private void btnLogin_Click(object sender, RoutedEventArgs e) {
- this.btnDownload.IsEnabled = false;
- if (string.IsNullOrEmpty(StaticData.ProjectsRoot))
- SetRoot();
- var filePath = Path.Combine(StaticData.ProjectsRoot, m_fileName);
- lblpresent.Visibility = Visibility.Visible;
- if (IsExist()) {
- // GetObjectProgress(filePath);
- DownloadAndUppack();
- }
- else {
- //提示是否重新选择目录
- //创建本地目录
- CreateLocalDirectory();
- //修改显示信息
- lblTitle.Content = "未找到本地文件,正在下载云平台数据";
- lblTips.Content = "用于创建本地账号,并储存修改的数据";
- for (int i = 0; i < 100; i++) {
- ShowProgress(i);
- Thread.Sleep(100);
- }
- Start();
- }
- }
- private void CreateLocalDirectory() {
- //创建建筑体目录
- foreach (var buildingName in StaticData.ProjectInfo.BuildingNames) {
- Directory.CreateDirectory(Path.Combine(StaticData.RevitDirectory, buildingName));
- }
- //创建数据库目录
- Directory.CreateDirectory(StaticData.DbDirectory);
- }
- public void GetObjectProgress(string filePath) {
- string key = $"test/files/revit/{m_fileName}";
- try {
- var getObjectRequest = new GetObjectRequest(bucketName, key);
- getObjectRequest.StreamTransferProgress += StreamProgressCallback;
- var ossObject = client.GetObject(getObjectRequest);
- using (var stream = ossObject.Content) {
- var buffer = new byte[1024 * 1024];
- var bytesTotal = 0;
- var bytesRead = 0;
- using (var fs = File.Open(filePath, FileMode.OpenOrCreate)) {
- while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
- bytesTotal += bytesRead;
- // Process read data
- // TODO
- fs.Write(buffer, 0, bytesRead);
- }
- }
- }
- UnpackFiles();
- Debug.WriteLine("Get object:{0} succeeded", key);
- }
- catch (OssException ex) {
- Debug.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
- ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
- }
- catch (Exception ex) {
- Debug.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
- public void DownloadAndUppack() {
- string key = $"test/files/revit/{m_fileName}";
- try {
- var getObjectRequest = new GetObjectRequest(bucketName, key);
- getObjectRequest.StreamTransferProgress += StreamProgressCallback;
- var ossObject = client.GetObject(getObjectRequest);
- using (var stream = ossObject.Content) {
- var buffer = new byte[1024 * 1024];
- var bytesTotal = 0;
- var bytesRead = 0;
- //默认为512M,可以使用MemoryStream(1000*1024*1024)扩大
- using (var fs = new System.IO.MemoryStream()) {
- while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
- bytesTotal += bytesRead;
- // Process read data
- // TODO
- fs.Write(buffer, 0, bytesRead);
- }
- SharpZip.UnpackStreamFiles(fs, DirectoryName);
- }
- }
- Debug.WriteLine("Get object:{0} succeeded", key);
- }
- catch (OssException ex) {
- Debug.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
- ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
- }
- catch (Exception ex) {
- Debug.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
- private void StreamProgressCallback(object sender, StreamTransferProgressArgs args) {
- var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
- ShowProgress(currentData);
- Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
- args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData);
- //下载完成,启动revit
- if (currentData == 100d) {
- Start();
- }
- }
- private void ShowProgress(double currentData) {
- lblpresent.Content = currentData + "%";
- Canvas.SetLeft(lblpresent, 81 + (progressBar.Width - lblpresent.Width) / 100 * currentData);
- Dispatcher.Invoke(new Action<DependencyProperty, object>((db, value) => this.progressBar.SetValue(db, value)),
- System.Windows.Threading.DispatcherPriority.Background,
- RangeBase.ValueProperty, Convert.ToDouble(currentData));
- }
- private void UnpackFiles() {
- //文件解压
- var filePath = Path.Combine(StaticData.ProjectsRoot, m_fileName);
- bool isSuccess = SharpZip.UnpackFiles(filePath, DirectoryName);
- if (isSuccess) {
- File.Delete(filePath);
- }
- }
- bool IsExist() {
- string key = $"test/files/revit/{m_fileName}";
- try {
- return client.DoesObjectExist(bucketName, key);
- }
- catch (OssException ex) {
- Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
- ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
- }
- catch (Exception ex) {
- Console.WriteLine("Failed with error info: {0}", ex.Message);
- }
- return false;
- }
- private void btnCancel_Click(object sender, RoutedEventArgs e) {
- Start();
- }
- void Start() {
- SAGY.Revit.MainWindow window = new SAGY.Revit.MainWindow();
- window.Start();
- Application.Current.Shutdown();
- }
- void SetRoot() {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
- StaticData.ProjectsRoot = fbd.SelectedPath.Trim();
- }
- }
- }
|