/// <summary> /// load account settings from local file if exists /// /// return empty object if none /// </summary> public static Account TryLoadAccount() { Account acct = new Account(); string myDocPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string accPath = System.IO.Path.Combine(myDocPath, "qsunsync", "account.json"); if (File.Exists(accPath)) { string accData = ""; using (StreamReader sr = new StreamReader(accPath, Encoding.UTF8)) { accData = sr.ReadToEnd(); } try { acct = JsonConvert.DeserializeObject<Account>(accData); } catch (Exception ex) { Log.Error("parse account info failed, " + ex.Message); } } return acct; }
/// <summary> /// sync setting page loaded event handler /// 更新 2016-10-20 15:51 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SyncSettingPageLoaded_EventHandler(object sender, RoutedEventArgs e) { // 尝试载入account this.account = Account.TryLoadAccount(); // 检查AK&SK if (string.IsNullOrEmpty(this.account.AccessKey) || string.IsNullOrEmpty(this.account.SecretKey)) { this.SettingsErrorTextBlock.Text = "请设置AK&SK"; return; } // 设置AK&SK SystemConfig.ACCESS_KEY = this.account.AccessKey; SystemConfig.SECRET_KEY = this.account.SecretKey; // 初始化bucketManager Mac mac = new Mac(this.account.AccessKey, this.account.SecretKey); this.bucketManager = new BucketManager(mac); if (!ValidateAccount()) { // Account设置不正确-->无法开始任务 ButtonStartSync.IsEnabled = false; return; } ButtonStartSync.IsEnabled = true; // 重建bucket列表 this.SyncTargetBucketsComboBox.ItemsSource = null; new Thread(new ThreadStart(this.reloadBuckets)).Start(); // 其他界面元素初始化 this.initUIDefaults(); }
/// <summary> /// init the bucket manager /// </summary> private void initBucketManager() { this.account = Account.TryLoadAccount(); if (string.IsNullOrEmpty(account.AccessKey) || string.IsNullOrEmpty(account.SecretKey)) { Log.Info("account info not set"); this.SettingsErrorTextBlock.Text = "请返回设置 AK 和 SK"; return; } Mac mac = new Mac(this.account.AccessKey, this.account.SecretKey); this.bucketManager = new BucketManager(mac); }
/// <summary> /// save account settings button click handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveAccountSettings_EventHandler(object sender, System.Windows.RoutedEventArgs e) { string accessKey = this.AccessKeyTextBox.Text.Trim(); string secretKey = this.SecretKeyTextBox.Text.Trim(); bool isAbroad = this.IsAbroadAccountCheckBox.IsChecked.Value; Account account = new Account(); account.AccessKey = accessKey; account.SecretKey = secretKey; account.IsAbroad = isAbroad; new Thread(new ParameterizedThreadStart(this.SaveAccountSetting)).Start(account); }
/// <summary> /// 使用stat模拟操作来检查Account是否正确 /// </summary> /// <returns></returns> private bool ValidateAccount(Account account) { Dispatcher.Invoke(new Action(delegate { this.SettingsErrorTextBlock.Text = ""; })); //check ak & sk validity Mac mac = new Mac(account.AccessKey, account.SecretKey); BucketManager bucketManager = new BucketManager(mac); StatResult statResult = bucketManager.stat("NONE_EXIST_BUCKET", "NONE_EXIST_KEY"); if (statResult.ResponseInfo.isNetworkBroken()) { Log.Error("stat file network error, " + statResult.ResponseInfo.ToString()); Dispatcher.Invoke(new Action(delegate { this.SettingsErrorTextBlock.Text = "网络故障"; })); } else { if (statResult.ResponseInfo.StatusCode == 401) { Log.Error("ak & sk wrong"); Dispatcher.Invoke(new Action(delegate { this.SettingsErrorTextBlock.Text = "AK 或 SK 设置不正确"; })); } else if (statResult.ResponseInfo.StatusCode == 612 || statResult.ResponseInfo.StatusCode == 631) { Log.Info("ak & sk is valid"); Dispatcher.Invoke(new Action(delegate { this.SettingsErrorTextBlock.Text = ""; this.mainWindow.GotoHomePage(); })); } else { Log.Error(string.Format("valid ak&sk unknown error, {0}:{1}:{2}:{3}", statResult.ResponseInfo.StatusCode, statResult.ResponseInfo.Error, statResult.ResponseInfo.ReqId, statResult.Response)); Dispatcher.Invoke(new Action(delegate { this.SettingsErrorTextBlock.Text = "未知错误,请联系七牛"; })); } } return true; }