Example #1
0
        private async void tvDir_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.tsbRefresh.Enabled = false;
            this.tsbShare.Enabled   = false;
            Control.SeafileDirTreeView.DataTreeViewNode node = tvDir.SelectedNode as Control.SeafileDirTreeView.DataTreeViewNode;
            if (node != null)
            {
                var nodetype = node.Type.ToString();
                //判断结点类型
                if (nodetype == "0" || nodetype == "1")
                {
                    if ((node.Nodes == null) || (node.Nodes.Count == 0))
                    {
                        this.UseWaitCursor = true;
                        try
                        {
                            var temp = await session.ListDirectory(node.LibraryID.ToString(), node.Path.ToString());

                            if (temp != null)
                            {
                                for (int i = 0; i < temp.Count; i++)
                                {
                                    DirType type;
                                    if (temp[i].Type == DirEntryType.Dir)
                                    {
                                        type = DirType.Dir;
                                    }
                                    else
                                    {
                                        type = DirType.File;
                                    }

                                    this.seafiledirData.SeafileDataTable.AddSeafileDataTableRow(string.Format("{0}-{1}", node.ID, (i + 1).ToString()), temp[i].Name, node.ID.ToString(), temp[i].LibraryId, ((int)(type)).ToString(), temp[i].Path);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex.Message);
                            MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            this.UseWaitCursor = false;
                        }
                        node.Expand();
                    }
                }
            }

            this.tsbRefresh.Enabled = true;
            this.tsbShare.Enabled   = true;
        }
Example #2
0
        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;
                }
            }
        }