From 8be5368397453766c2c6980ada57989b5168d2e7 Mon Sep 17 00:00:00 2001 From: Andre Rodrigues Date: Wed, 1 Dec 2010 15:40:44 -0800 Subject: [PATCH] #16840: Implementing module installation from local uploaded file. Removing hard dependency between Orchard.Modules and Orchard.Packaging by delegating module installation to Orchard.Packaging. --HG-- branch : dev --- .../Controllers/AdminController.cs | 47 +----- .../Modules/Orchard.Modules/Module.txt | 2 +- .../Orchard.Modules/Orchard.Modules.csproj | 6 - .../ViewModels/ModuleAddViewModel.cs | 4 - .../ViewModels/ModulesIndexViewModel.cs | 1 + .../Views/Admin/Features.cshtml | 5 +- .../Orchard.Modules/Views/Admin/Index.cshtml | 6 +- .../Controllers/GalleryController.cs | 53 ++++++- .../Modules/Orchard.Packaging/Module.txt | 2 +- .../Orchard.Packaging.csproj | 7 +- .../Services/IPackageInstaller.cs | 3 + .../Services/IPackageManager.cs | 3 + .../Services/PackageInstaller.cs | 140 ++++++++---------- .../Services/PackageManager.cs | 6 + .../Views/Gallery/AddModule.cshtml} | 10 +- 15 files changed, 141 insertions(+), 154 deletions(-) delete mode 100644 src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModuleAddViewModel.cs rename src/Orchard.Web/Modules/{Orchard.Modules/Views/Admin/Add.cshtml => Orchard.Packaging/Views/Gallery/AddModule.cshtml} (57%) diff --git a/src/Orchard.Web/Modules/Orchard.Modules/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Modules/Controllers/AdminController.cs index dd212c539..fc094bcb6 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Modules/Controllers/AdminController.cs @@ -26,8 +26,8 @@ namespace Orchard.Modules.Controllers { IReportsCoordinator reportsCoordinator, IExtensionManager extensionManager, IFeatureManager featureManager, - ShellDescriptor shellDescriptor) { - + ShellDescriptor shellDescriptor) + { Services = services; _moduleService = moduleService; _dataMigrationManager = dataMigrationManager; @@ -47,47 +47,10 @@ namespace Orchard.Modules.Controllers { return new HttpUnauthorizedResult(); var modules = _extensionManager.AvailableExtensions().Where(x => x.ExtensionType == "Module"); - return View(new ModulesIndexViewModel { Modules = modules }); - } - public ActionResult Add() { - return View(new ModuleAddViewModel()); - } - - [HttpPost, ActionName("Add")] - public ActionResult AddPOST() { - // module not used for anything other than display (and that only to not have object in the view 'T') - var viewModel = new ModuleAddViewModel(); - try { - UpdateModel(viewModel); - if (!Services.Authorizer.Authorize(Permissions.ManageModules, T("Couldn't upload module package."))) - return new HttpUnauthorizedResult(); - - if (string.IsNullOrWhiteSpace(Request.Files[0].FileName)) { - ModelState.AddModelError("File", T("Select a file to upload.").ToString()); - } - - if (!ModelState.IsValid) - return View("add", viewModel); - - foreach (string fileName in Request.Files) { - var file = Request.Files[fileName]; -#if REFACTORING - var info = _packageManager.Install(file.InputStream); - - Services.Notifier.Information(T("Installed package \"{0}\", version {1} of type \"{2}\" at location \"{3}\"", - info.ExtensionName, info.ExtensionVersion, info.ExtensionType, info.ExtensionPath)); -#endif - } - - return RedirectToAction("index"); - } - catch (Exception exception) { - for (var scan = exception; scan != null; scan = scan.InnerException) { - Services.Notifier.Error(T("Uploading module package failed: {0}", exception.Message)); - } - return View("add", viewModel); - } + return View(new ModulesIndexViewModel { + Modules = modules, + InstallModules = _featureManager.GetEnabledFeatures().FirstOrDefault(f => f.Id == "Gallery") != null }); } public ActionResult Features() { diff --git a/src/Orchard.Web/Modules/Orchard.Modules/Module.txt b/src/Orchard.Web/Modules/Orchard.Modules/Module.txt index ce1313446..471dac200 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/Module.txt +++ b/src/Orchard.Web/Modules/Orchard.Modules/Module.txt @@ -8,4 +8,4 @@ Description: The Modules module enables the administrator of the site to manage Features: Orchard.Modules: Description: Standard module and feature management. - Category: Core + Category: Core \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Modules/Orchard.Modules.csproj b/src/Orchard.Web/Modules/Orchard.Modules/Orchard.Modules.csproj index 300a2c7c4..1ae541037 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/Orchard.Modules.csproj +++ b/src/Orchard.Web/Modules/Orchard.Modules/Orchard.Modules.csproj @@ -62,12 +62,10 @@ - - @@ -75,10 +73,6 @@ {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} Orchard.Framework - - {DFD137A2-DDB5-4D22-BE0D-FA9AD4C8B059} - Orchard.Packaging - diff --git a/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModuleAddViewModel.cs b/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModuleAddViewModel.cs deleted file mode 100644 index cf45dc975..000000000 --- a/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModuleAddViewModel.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Orchard.Modules.ViewModels { - public class ModuleAddViewModel { - } -} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModulesIndexViewModel.cs b/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModulesIndexViewModel.cs index 7256db0f5..6811dacbb 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModulesIndexViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.Modules/ViewModels/ModulesIndexViewModel.cs @@ -3,6 +3,7 @@ using Orchard.Environment.Extensions.Models; namespace Orchard.Modules.ViewModels { public class ModulesIndexViewModel { + public bool InstallModules { get; set; } public IEnumerable Modules { get; set; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Features.cshtml b/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Features.cshtml index 61f660f70..0b5ff95c8 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Features.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Features.cshtml @@ -23,7 +23,7 @@ if (featureGroup == featureGroups.Last()) { categoryClassName += " last"; } - + //temporarily "disable" actions on core features var showActions = categoryName.ToString() != "Core";
  • @@ -96,5 +96,4 @@
  • } } } -}} - +}} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Index.cshtml b/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Index.cshtml index 0456cc42c..0be57d5a4 100644 --- a/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Index.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Modules/Views/Admin/Index.cshtml @@ -8,7 +8,11 @@ }

    @Html.TitleForPage(T("Installed Modules").ToString())

    -
    @Html.ActionLink(T("Install a module").ToString(), "Add", null, new { @class = "button primaryAction" })
    + +@if (Model.InstallModules) { +
    @Html.ActionLink(T("Install a module").ToString(), "AddModule", "Gallery", new { area = "Orchard.Packaging", returnUrl = HttpContext.Current.Request.RawUrl }, new { @class = "button primaryAction" })
    +} + @if (Model.Modules.Count() > 0) {
      @foreach (var module in Model.Modules.OrderBy(m => m.Name)) { diff --git a/src/Orchard.Web/Modules/Orchard.Packaging/Controllers/GalleryController.cs b/src/Orchard.Web/Modules/Orchard.Packaging/Controllers/GalleryController.cs index 6400b0575..7ad804931 100644 --- a/src/Orchard.Web/Modules/Orchard.Packaging/Controllers/GalleryController.cs +++ b/src/Orchard.Web/Modules/Orchard.Packaging/Controllers/GalleryController.cs @@ -1,15 +1,20 @@ using System; +using System.IO; using System.Linq; +using System.Web; using System.Web.Hosting; using System.Web.Mvc; using System.Xml.Linq; +using NuGet; using Orchard.Environment.Extensions; +using Orchard.FileSystems.AppData; using Orchard.Localization; using Orchard.Packaging.Services; using Orchard.Packaging.ViewModels; using Orchard.Themes; using Orchard.UI.Admin; using Orchard.UI.Notify; +using IPackageManager = Orchard.Packaging.Services.IPackageManager; namespace Orchard.Packaging.Controllers { [OrchardFeature("Gallery")] @@ -18,18 +23,19 @@ namespace Orchard.Packaging.Controllers { private readonly IPackageManager _packageManager; private readonly IPackagingSourceManager _packagingSourceManager; - private readonly IExtensionManager _extensionManager; + private readonly IAppDataFolderRoot _appDataFolderRoot; private readonly INotifier _notifier; public GalleryController( IPackageManager packageManager, IPackagingSourceManager packagingSourceManager, - IExtensionManager extensionManager, - INotifier notifier) { + INotifier notifier, + IAppDataFolderRoot appDataFolderRoot) { _packageManager = packageManager; _packagingSourceManager = packagingSourceManager; - _extensionManager = extensionManager; _notifier = notifier; + _appDataFolderRoot = appDataFolderRoot; + T = NullLocalizer.Instance; } @@ -95,7 +101,6 @@ namespace Orchard.Packaging.Controllers { } } - public ActionResult Modules(int? sourceId) { var selectedSource = _packagingSourceManager.GetSources().Where(s => s.Id == sourceId).FirstOrDefault(); @@ -137,5 +142,43 @@ namespace Orchard.Packaging.Controllers { return RedirectToAction(redirectTo == "Themes" ? "Themes" : "Modules"); } + + public ActionResult AddModule(string returnUrl) { + return View(); + } + + [HttpPost, ActionName("AddModule")] + public ActionResult AddModulePOST(string returnUrl) { + // module not used for anything o2ther than display (and that only to not have object in the view 'T') + try { + if (string.IsNullOrWhiteSpace(Request.Files[0].FileName)) { + ModelState.AddModelError("File", T("Select a file to upload.").ToString()); + } + + foreach (string fileName in Request.Files) { + HttpPostedFileBase file = Request.Files[fileName]; + if (file != null) { + string fullFileName = Path.Combine(_appDataFolderRoot.RootFolder, fileName + ".nupkg").Replace(Path.DirectorySeparatorChar, '/'); + file.SaveAs(fullFileName); + PackageInfo info = _packageManager.Install(new ZipPackage(fullFileName), _appDataFolderRoot.RootFolder, HostingEnvironment.MapPath("~/")); + System.IO.File.Delete(fullFileName); + + _notifier.Information(T("Installed package \"{0}\", version {1} of type \"{2}\" at location \"{3}\"", + info.ExtensionName, info.ExtensionVersion, info.ExtensionType, info.ExtensionPath)); + } + } + + if (!string.IsNullOrEmpty(returnUrl)) + return Redirect(returnUrl); + + return RedirectToAction("Modules"); + } catch (Exception exception) { + for (var scan = exception; scan != null; scan = scan.InnerException) { + _notifier.Error(T("Uploading module package failed: {0}", exception.Message)); + } + + return View("AddModule"); + } + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Packaging/Module.txt b/src/Orchard.Web/Modules/Orchard.Packaging/Module.txt index 64b039f9f..bec584f51 100644 --- a/src/Orchard.Web/Modules/Orchard.Packaging/Module.txt +++ b/src/Orchard.Web/Modules/Orchard.Packaging/Module.txt @@ -16,4 +16,4 @@ Features: Gallery: Description: Module gallery management. Dependencies: PackagingServices, Orchard.Packaging - Category: Packaging + Category: Packaging \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Packaging/Orchard.Packaging.csproj b/src/Orchard.Web/Modules/Orchard.Packaging/Orchard.Packaging.csproj index 0a74fabfd..d0710b48b 100644 --- a/src/Orchard.Web/Modules/Orchard.Packaging/Orchard.Packaging.csproj +++ b/src/Orchard.Web/Modules/Orchard.Packaging/Orchard.Packaging.csproj @@ -35,7 +35,7 @@ - + False ..\..\..\..\lib\nuget\NuGet.Core.dll @@ -93,7 +93,9 @@ - + + + {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} @@ -118,6 +120,7 @@ +