private async void btnGenerateLink_Click(object sender, EventArgs e) { tokenSource = new CancellationTokenSource(); this.btnGenerateLink.Enabled = false; string strPassword = string.Empty; string strExpire = string.Empty; if (cbPassword.Checked) { if (string.IsNullOrEmpty(this.tbPassword.Text)) { MessageBox.Show( Properties.Resources.EmptyPassword, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } if (this.tbPassword.Text.Length < 8) { MessageBox.Show( Properties.Resources.PasswordTooShort, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } if (this.tbPassword.Text != this.tbPasswordR.Text) { MessageBox.Show( Properties.Resources.PasswordNotMatch, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } strPassword = this.tbPassword.Text; } if (cbExpire.Checked) { if (string.IsNullOrEmpty(this.tbExpireDay.Text)) { return; } strExpire = this.tbExpireDay.Text; } this.UseWaitCursor = true; try { //var resultGetFileDetail = await _session.GetFileDetail(_libraryID, _path, tokenSource.Token); //if (resultGetFileDetail != null) //{ //var resultCreatShareLink = await _session.CreatShareLink(resultGetFileDetail, tokenSource.Token, strPassword, strExpire); SeafDirEntry resultGetFileDetail = new SeafDirEntry(); resultGetFileDetail.LibraryId = _libraryID; resultGetFileDetail.Path = _path; var resultCreatShareLink = await _session.CreatShareLink(resultGetFileDetail, tokenSource.Token, strPassword, strExpire); if (!string.IsNullOrEmpty(resultCreatShareLink)) { this.DialogResult = DialogResult.OK; JObject o2 = (JObject)JToken.Parse(resultCreatShareLink); this.ShareLink = (string)o2["link"]; if (cbPassword.Checked) { this.ShareLink += string.Format("\r\n {0}:{1}", Properties.Resources.SharePassword, this.tbPassword.Text); } } //} } catch (System.OperationCanceledException ex) { log.Info(ex.Message); } catch (SeafException se) { log.Error(se.SeafError.GetErrorMessage()); MessageBox.Show( se.SeafError.GetErrorMessage(), Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.btnGenerateLink.Enabled = true; this.UseWaitCursor = false; tokenSource.Dispose(); tokenSource = null; } }
private async void btnUpload_Click(object sender, EventArgs e) { log.Info("btnUpload_Click"); if (!bWork) { this.pbUpload.Value = 0; tokenSource = new CancellationTokenSource(); this.btnCancelUplaod.Enabled = false; bWork = true; string host = string.Empty; string user = string.Empty; string strPassWord = string.Empty; try { if (!File.Exists(Application.UserAppDataPath + "\\seafileaddin.cfg")) { log.Error("seafileaddin.cfg not exits"); MessageBox.Show( Properties.Resources.SetAccount, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); //MessageBox.Show("An error occurred while attempting to show the application." + // "The error is:" + Properties.Resources.SetAccount); this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); return; } using (var file = File.OpenText(Application.UserAppDataPath + "\\seafileaddin.cfg")) { using (JsonTextReader reader = new JsonTextReader(file)) { JObject o2 = (JObject)JToken.ReadFrom(reader); host = (string)o2["accessurl"]; user = (string)o2["account"]; strPassWord = DataProtectionExtensions.Unprotect((string)o2["poassword"]); } } } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show( Properties.Resources.ConfigParseError, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); // return; } if (string.IsNullOrEmpty(tbFile.Text)) { MessageBox.Show( Properties.Resources.EmptyUploadFile, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } string strPassword = string.Empty; string strExpire = string.Empty; if (cbPassword.Checked) { if (string.IsNullOrEmpty(this.tbPassword.Text)) { MessageBox.Show( Properties.Resources.EmptyPassword, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } if (this.tbPassword.Text.Length < 8) { MessageBox.Show( Properties.Resources.PasswordTooShort, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } if (this.tbPassword.Text != this.tbPasswordR.Text) { MessageBox.Show( Properties.Resources.PasswordNotMatch, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } strPassword = this.tbPassword.Text; } if (cbExpire.Checked) { if (string.IsNullOrEmpty(this.tbExpireDay.Text)) { this.btnCancelUplaod.Enabled = true; bWork = false; return; } strExpire = this.tbExpireDay.Text; } System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, errors) => { return(true); }; char[] pChar = strPassWord.ToCharArray(); SecureString pw = new SecureString(); foreach (char c in pChar) { pw.AppendChar(c); } char[] pwBuf = SecureStringUtils.SecureStringToCharArray(pw); pw.Dispose(); try { session = await SeafileSession.Establish(new Uri(host, UriKind.Absolute), user, pwBuf); // try to connect to the seafile server using the given credentials log.Info("connection success."); this.btnCancelUplaod.Enabled = true; this.btnCancelUplaod.Text = Properties.Resources.Cancel; //TODO loading process... var seafDefaultLibrary = await session.GetDefaultLibrary(); try { var exit = await session.ListDirectory(seafDefaultLibrary, "/outlook"); } catch (SeafException exListDirectory) { log.Info("outlook folder doesn't exit."); if (exListDirectory.SeafError.HttpStatusCode == System.Net.HttpStatusCode.Forbidden) { bool successCreate = await session.CreateDirectory(seafDefaultLibrary, "/outlook"); } } //catch (Exception ex) //{ // log.Error(ex.Message); // MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} //check if file exist string fileName = System.IO.Path.GetFileName(tbFile.Text); System.IO.FileStream fs = new System.IO.FileStream(tbFile.Text, System.IO.FileMode.Open); pbUpload.Value = 0; Action <float> ProcessAction = new Action <float>(ProcessCallBack); var resultUploadSingle = await session.UploadSingle(seafDefaultLibrary, "/outlook", fileName, fs, tokenSource.Token, ProcessAction); if (resultUploadSingle != null) { JObject o2 = (JObject)JToken.Parse(resultUploadSingle.Substring(1, resultUploadSingle.Length - 2)); string tmpName = (string)o2["name"]; SeafDirEntry tmp = new SeafDirEntry(); tmp.LibraryId = seafDefaultLibrary.Id; tmp.Path = string.Format("/outlook/{0}", tmpName); var resultCreatShareLink = await session.CreatShareLink(tmp, tokenSource.Token, strPassword, strExpire); if (!string.IsNullOrEmpty(resultCreatShareLink)) { this.DialogResult = DialogResult.OK; //this.ShareLink = resultCreatShareLink; JObject oShare = (JObject)JToken.Parse(resultCreatShareLink); this.ShareLink = (string)oShare["link"]; if (cbPassword.Checked) { this.ShareLink += string.Format("\r\n {0}:{1}", Properties.Resources.SharePassword, this.tbPassword.Text); } } } } catch (System.OperationCanceledException ex) { log.Info(ex.Message); } catch (System.IO.IOException ioEx) { log.Error(ioEx.Message); MessageBox.Show( Properties.Resources.ReadFileError, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.btnCancelUplaod.Enabled = true; this.btnCancelUplaod.Text = Properties.Resources.UploadBtn; tokenSource.Dispose(); bWork = false; } } else { if (tokenSource != null) { tokenSource.Cancel(); log.Info("cancel upload."); bWork = false; this.pbUpload.Value = 0; } } }