From bed5f8a03be9eae9f5c10077329d312de32ba5e6 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 27 Apr 2017 13:21:07 +1000 Subject: [PATCH] backports updates from 7.6 so that the packages.umbraco.org endpoint is not used for fetching package files any longer --- .../UmbracoSettings/IRepository.cs | 3 +- .../UmbracoSettings/RepositoryElement.cs | 10 +-- .../Exceptions/ConnectionException.cs | 12 ++++ .../Services/IPackagingService.cs | 12 +++- src/Umbraco.Core/Services/PackagingService.cs | 61 ++++++++++++++++++- src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../Editors/PackageInstallController.cs | 6 +- 7 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 src/Umbraco.Core/Exceptions/ConnectionException.cs diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/IRepository.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/IRepository.cs index 34730f3e45..7559f090c0 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/IRepository.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/IRepository.cs @@ -9,7 +9,6 @@ namespace Umbraco.Core.Configuration.UmbracoSettings string RepositoryUrl { get; } string WebServiceUrl { get; } bool HasCustomWebServiceUrl { get; } - string RestApiUrl { get; } - bool HasCustomRestApiUrl { get; } + string RestApiUrl { get; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/RepositoryElement.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/RepositoryElement.cs index 69465f62da..a249be2ee3 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/RepositoryElement.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/RepositoryElement.cs @@ -48,14 +48,6 @@ namespace Umbraco.Core.Configuration.UmbracoSettings get { return (string)base["restapiurl"]; } set { base["restapiurl"] = value; } } - - public bool HasCustomRestApiUrl - { - get - { - var prop = Properties["restapiurl"]; - return (string)prop.DefaultValue != (string)this[prop]; - } - } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Exceptions/ConnectionException.cs b/src/Umbraco.Core/Exceptions/ConnectionException.cs new file mode 100644 index 0000000000..b0e8778359 --- /dev/null +++ b/src/Umbraco.Core/Exceptions/ConnectionException.cs @@ -0,0 +1,12 @@ +using System; + +namespace Umbraco.Core.Exceptions +{ + internal class ConnectionException : Exception + { + public ConnectionException(string message, Exception innerException) : base(message, innerException) + { + + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IPackagingService.cs b/src/Umbraco.Core/Services/IPackagingService.cs index c7e68e6eca..047d87afcd 100644 --- a/src/Umbraco.Core/Services/IPackagingService.cs +++ b/src/Umbraco.Core/Services/IPackagingService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Xml.Linq; using Umbraco.Core.Models; @@ -6,6 +7,15 @@ namespace Umbraco.Core.Services { public interface IPackagingService : IService { + /// + /// This will fetch an Umbraco package file from the package repository and return the relative file path to the downloaded package file + /// + /// + /// + /// The current user id performing the operation + /// + string FetchPackageFile(Guid packageId, Version umbracoVersion, int userId); + /// /// Imports and saves package xml as /// diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index 4d8a2773ba..2d0ccc3521 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; +using System.Net.Http; using System.Text.RegularExpressions; using System.Web; using System.Web.UI.WebControls; @@ -9,7 +11,9 @@ using System.Xml.Linq; using System.Xml.XPath; using Newtonsoft.Json; using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Events; +using Umbraco.Core.Exceptions; using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -58,7 +62,7 @@ namespace Umbraco.Core.Services IEntityService entityService, IUserService userService, RepositoryFactory repositoryFactory, - IDatabaseUnitOfWorkProvider uowProvider) + IDatabaseUnitOfWorkProvider uowProvider) { _logger = logger; _contentService = contentService; @@ -75,6 +79,61 @@ namespace Umbraco.Core.Services _importedContentTypes = new Dictionary(); } + /// + /// This will fetch an Umbraco package file from the package repository and return the relative file path to the downloaded package file + /// + /// + /// + /// /// The current user id performing the operation + /// + public string FetchPackageFile(Guid packageId, Version umbracoVersion, int userId) + { + var packageRepo = UmbracoConfig.For.UmbracoSettings().PackageRepositories.GetDefault(); + + using (var httpClient = new HttpClient()) + using (var uow = _uowProvider.GetUnitOfWork()) + { + //includeHidden = true because we don't care if it's hidden we want to get the file regardless + var url = string.Format("{0}/{1}?version={2}&includeHidden=true&asFile=true", packageRepo.RestApiUrl, packageId, umbracoVersion.ToString(3)); + byte[] bytes; + try + { + bytes = httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult(); + } + catch (HttpRequestException ex) + { + throw new ConnectionException("An error occuring downloading the package from " + url, ex); + } + + //successfull + if (bytes.Length > 0) + { + var packagePath = IOHelper.MapPath(SystemDirectories.Packages); + + // Check for package directory + if (Directory.Exists(packagePath) == false) + Directory.CreateDirectory(packagePath); + + var packageFilePath = Path.Combine(packagePath, packageId + ".umb"); + + using (var fs1 = new FileStream(packageFilePath, FileMode.Create)) + { + fs1.Write(bytes, 0, bytes.Length); + return "packages\\" + packageId + ".umb"; + } + } + + Audit(uow, AuditType.PackagerInstall, string.Format("Package {0} fetched from {1}", packageId, packageRepo.Id), userId, -1); + return null; + } + } + + private void Audit(IDatabaseUnitOfWork uow, AuditType type, string message, int userId, int objectId) + { + var auditRepo = _repositoryFactory.CreateAuditRepository(uow); + auditRepo.AddOrUpdate(new AuditItem(objectId, message, type, userId)); + } + #region Content /// diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index de81b7e519..34a700de33 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -336,6 +336,7 @@ + diff --git a/src/Umbraco.Web/Editors/PackageInstallController.cs b/src/Umbraco.Web/Editors/PackageInstallController.cs index dce69c9d27..48a2808ef3 100644 --- a/src/Umbraco.Web/Editors/PackageInstallController.cs +++ b/src/Umbraco.Web/Editors/PackageInstallController.cs @@ -444,11 +444,7 @@ namespace Umbraco.Web.Editors string path = Path.Combine("packages", packageGuid + ".umb"); if (File.Exists(IOHelper.MapPath(Path.Combine(SystemDirectories.Data, path))) == false) { - //our repo guid - using (var our = Repository.getByGuid("65194810-1f85-11dd-bd0b-0800200c9a66")) - { - path = our.GetPackageFile(packageGuid, Security.CurrentUser.Id, UmbracoVersion.Current); - } + path = Services.PackagingService.FetchPackageFile(Guid.Parse(packageGuid), UmbracoVersion.Current, Security.GetUserId()); } var model = new LocalPackageInstallModel