Ejemplo n.º 1
0
        protected void btnInstall_Click(object sender, EventArgs e)
        {
            IList <string> successList = new List <string>();
            IList <string> failedList  = new List <string>();

            // Loop through selected providers
            foreach (ListItem li in chkProviders.Items)
            {
                if (li.Selected)
                {
                    var url = li.Value;

                    try
                    {
                        var tmpFileName = Guid.NewGuid().ToString() + ".umb";
                        var tmpFilePath = IOHelper.MapPath(SystemDirectories.Data + Path.DirectorySeparatorChar + tmpFileName);

                        // Download file
                        new WebClient().DownloadFile(url, tmpFilePath);

                        // Extract package guid from zip
                        var packageGuid = GetPackageGuidFromZip(tmpFilePath);
                        if (packageGuid == default(Guid))
                        {
                            packageGuid = Guid.NewGuid();
                        }

                        var packageGuidString = packageGuid.ToString("D");

                        // Check package isn't already installed
                        if (!InstalledPackage.isPackageInstalled(packageGuidString))
                        {
                            // Rename file
                            var packageFileName = packageGuidString + ".umb";
                            var packageFilePath = IOHelper.MapPath(SystemDirectories.Data + Path.DirectorySeparatorChar + packageFileName);
                            File.Move(tmpFilePath, packageFilePath);

                            // Install package
                            var installer = new umbraco.cms.businesslogic.packager.Installer();
                            var tempDir   = installer.Import(packageFileName);
                            installer.LoadConfig(tempDir);
                            var packageId = installer.CreateManifest(tempDir, packageGuidString, REPO_GUID);
                            installer.InstallFiles(packageId, tempDir);
                            installer.InstallBusinessLogic(packageId, tempDir);
                            installer.InstallCleanUp(packageId, tempDir);

                            // Append package to success list
                            successList.Add(li.Text);

                            // Deselect and disable option
                            li.Enabled = li.Selected = false;
                        }
                        else
                        {
                            // Append package to failed list
                            failedList.Add(li.Text);
                        }
                    }
                    catch (Exception)
                    {
                        // Append package to failed list
                        failedList.Add(li.Text);
                    }
                }
            }

            library.RefreshContent();

            // Show message
            if (successList.Count > 0)
            {
                if (failedList.Count > 0)
                {
                    // Some providers installed, some failed
                    feedback.type = umbraco.uicontrols.Feedback.feedbacktype.notice;
                    feedback.Text = string.Format("{0} provider(s) installed successfully, however {1} provider(s) failed to install. Please see the <strong>Installed packages</strong> section to see which provider(s) were installed.", successList.Count, failedList.Count);
                }
                else
                {
                    // All successfull
                    feedback.type = umbraco.uicontrols.Feedback.feedbacktype.success;
                    feedback.Text = string.Format("{0} provider(s) installed successfully", successList.Count);
                }
            }
            else
            {
                if (failedList.Count > 0)
                {
                    // All failed
                    feedback.type = umbraco.uicontrols.Feedback.feedbacktype.error;
                    feedback.Text = string.Format("{0} provider(s) failed to install", failedList.Count);
                }
                else
                {
                    // None selected
                    feedback.type = umbraco.uicontrols.Feedback.feedbacktype.error;
                    feedback.Text = "Please select one or more providers to install";
                }
            }
        }