Exemplo n.º 1
0
        static SolrHttpOperation()
        {
            Action <object, SolrHttpClientEventArgs> beforeRequest = (s, e) =>
            {
                //add a parameter to avoid cache
                e.RequestMessage.RequestUri = new Uri(e.RequestMessage.RequestUri.ToString() + $"&_={DateTime.Now.ToString("mmssffffff")}");
                var url = e.RequestMessage.RequestUri.ToString();
                logger.Log($"request url:[{url}]");
            };

            Action <object, SolrHttpClientEventArgs> requestComplete = (s, e) =>
            {
                var url = e.RequestMessage.RequestUri.ToString();
                logger.Log($"finished request url:[{url}], response content: [{e.ResponseMsg.Content.ReadAsStringAsync().Result}]");
            };

            _client = new Lazy <HttpClient>(() => {
                return(SolrHttpClient.Create(
                           AppGlobalConfigs.AppConfig.Solr.BaseUrl,
                           AppGlobalConfigs.AppConfig.Solr.Auth.Username,
                           AppGlobalConfigs.AppConfig.Solr.Auth.Password,
                           beforeRequest,
                           requestComplete));
            });
        }
Exemplo n.º 2
0
        private void BindEvents()
        {
            Load += async(s, e) =>
            {
                StatusStripLabel.Text = "checking configs...";
                logger.Log("starting to CheckConfigs");
                await CheckConfigs();

                logger.Log("CheckConfigs finished");

                StatusStripLabel.Text = "load solr infomation...";
                await LoadSolrInfo();

                StatusStripLabel.Text = "finished";
            };

            FormClosed += (s, e) =>
            {
                logger.Log("application exit.");
                logger.Dispose();
            };

            timer.Tick += (s, e) =>
            {
                if (_timerExec)
                {
                    return;
                }

                _timerExec = true;
                var logs = logger.GetBufferLog();

                Action act = () =>
                {
                    AppendLog(logs);
                    TextBoxLog.Select(TextBoxLog.TextLength, 0);
                    TextBoxLog.ScrollToCaret();
                };

                if (logs.Length > 0)
                {
                    this.Invoke(act);
                }

                _timerExec = false;
            };

            ComboSolrCollection.SelectedIndexChanged += async(s, e) =>
            {
                var col   = ComboSolrCollection.Items[ComboSolrCollection.SelectedIndex].ToString();
                var files = await SolrHttpOperation.GetFiles(col);

                var dirs = files.Lst.FirstOrDefault(x => x.Name == "files").Lsts
                           .Where(x => x.Bool != null && x.Bool.Count > 0 && x.Bool.FirstOrDefault().Name == "directory")
                           .Select(x => x.Name);

                if (files.Lst.FirstOrDefault(x => x.Name == "files") != null)
                {
                    await GenTree(tree.Nodes, col);
                }
            };

            tree.AfterSelect += async(sender, treeArgs) =>
            {
                var selectedNode     = treeArgs.Node;
                var selectedNodeText = selectedNode.FullPath;

                if (selectedNode.Tag.ToString() == "d")
                {
                    await GenTree(selectedNode.Nodes, ComboSolrCollection.Items[ComboSolrCollection.SelectedIndex].ToString(), selectedNodeText);
                }
                if (selectedNode.Tag.ToString() == "f")
                {
                    var content = await SolrHttpOperation.GetFileContent(ComboSolrCollection.Items[ComboSolrCollection.SelectedIndex].ToString(), selectedNodeText);

                    TextBoxFileContent.Text = content;
                    TextBoxFileContent.Tag  = $"{ComboSolrCollection.Items[ComboSolrCollection.SelectedIndex].ToString()}/{ selectedNode.FullPath.Replace("\\", "/")}";
                }
            };

            tree.AfterExpand += async(s, e) =>
            {
                if (tree.SelectedNode != e.Node)
                {
                    tree.SelectedNode = e.Node;
                }
            };

            LinkLblCopy.Click += async(s, e) =>
            {
                Clipboard.SetText(TextBoxFileContent.Text);
                LinkLblCopy.Text = "复制成功";
                await Task.Delay(2000);

                LinkLblCopy.Text = "COPY";
            };

            //导出
            BtnExportAllFiles.Click += async(s, e) =>
            {
                BtnExportAllFiles.Enabled = false;
                var path = await ExportAllFiles(ComboSolrCollection.Items[ComboSolrCollection.SelectedIndex].ToString());

                BtnExportAllFiles.Enabled = true;

                var psi = new ProcessStartInfo("cmd.exe", $"/c \"explorer.exe {path}\"");
                psi.CreateNoWindow = true;
                Process.Start(psi);
            };

            BtnExportAllCollections.Click += async(s, e) =>
            {
                BtnExportAllCollections.Enabled = false;

                var path = "";
                ComboSolrCollection.SelectedIndex = 0;
                foreach (var item in ComboSolrCollection.Items)
                {
                    path = await ExportAllFiles(item.ToString());
                }
                var configPath = new DirectoryInfo(path).Parent.FullName;

                BtnExportAllCollections.Enabled = true;

                var psi = new ProcessStartInfo("cmd.exe", $"/c \"explorer.exe {configPath}\"");
                psi.CreateNoWindow = true;
                Process.Start(psi);
            };

            //文件更新
            BtnUpdateCurrentFile.Click += async(s, e) =>
            {
                var path = TextBoxFileContent.Tag?.ToString();
                if (string.IsNullOrWhiteSpace(path))
                {
                    MessageBox.Show("please select a node for update.");
                    return;
                }

                var dr = MessageBox.Show($"将更新文件:[{path}], 是否继续?", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    BtnUpdateCurrentFile.Enabled = false;
                    StatusStripLabel.Text        = "updateing config...";

                    await UpdateCurrentFile(path);

                    StatusStripLabel.Text        = "finished";
                    BtnUpdateCurrentFile.Enabled = true;
                    MessageBox.Show("更新成功");
                }
            };

            //重载
            BtnReoload.Click += async(s, e) =>
            {
                BtnReoload.Enabled = false;
                await LoadSolrInfo();

                BtnReoload.Enabled = true;
            };

            //创建配置
            BtnUploadConfig.Click += async(s, e) =>
            {
                BtnUploadConfig.Enabled = false;
                string confName = "", confFolder = "";
                var    frm = new FrmConfigUploadBox();
                frm.StartPosition = FormStartPosition.CenterParent;
                frm.FormClosing  += (sender, args) =>
                {
                    confFolder = frm.ConfigFolder;
                    confName   = frm.ConfigName;
                };
                frm.ShowDialog(this);
                if (confName != "" && confFolder != "")
                {
                    var client = SolrHttpClient.Create(
                        AppGlobalConfigs.AppConfig.Solr.BaseUrl,
                        AppGlobalConfigs.AppConfig.Solr.Auth.Username,
                        AppGlobalConfigs.AppConfig.Solr.Auth.Password
                        );

                    if (Directory.Exists(confFolder))
                    {
                        var tmpDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp");
                        if (!Directory.Exists(tmpDir))
                        {
                            Directory.CreateDirectory(tmpDir);
                        }

                        var file = Path.Combine(tmpDir, Guid.NewGuid().ToString("N") + ".zip");
                        using (var zip = new ZipFile())
                        {
                            zip.AddDirectory(confFolder);
                            zip.Save(file);
                        }
                        await SolrHttpOperation.CreateConfig(confName, file);
                    }
                }

                BtnUploadConfig.Enabled = true;
            };
        }