Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallSteps/StarterKitDownloadStep.cs

107 lines
4.3 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Umbraco.Core.Services;
2017-05-30 10:50:09 +02:00
using Umbraco.Core.Configuration;
using Umbraco.Core.Models.Packaging;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.Install.Models;
namespace Umbraco.Web.Install.InstallSteps
{
[InstallSetupStep(InstallationType.NewInstall,
2017-09-08 19:39:13 +02:00
"StarterKitDownload", "starterKit", 30, "Adding a simple website to Umbraco, will make it easier for you to get started",
PerformsAppRestart = true)]
internal class StarterKitDownloadStep : InstallSetupStep<Guid?>
{
private readonly InstallHelper _installHelper;
private readonly UmbracoContext _umbracoContext;
private readonly IContentService _contentService;
private readonly IPackagingService _packageService;
2014-02-27 10:16:30 +01:00
public StarterKitDownloadStep(IContentService contentService, IPackagingService packageService, InstallHelper installHelper, UmbracoContext umbracoContext)
2014-02-27 10:16:30 +01:00
{
_installHelper = installHelper;
_umbracoContext = umbracoContext;
_contentService = contentService;
_packageService = packageService;
2014-02-27 10:16:30 +01:00
}
//private const string RepoGuid = "65194810-1f85-11dd-bd0b-0800200c9a66";
public override async Task<InstallSetupResult> ExecuteAsync(Guid? starterKitId)
{
//if there is no value assigned then use the default starter kit
if (starterKitId.HasValue == false)
{
var starterKits = _installHelper.GetStarterKits().FirstOrDefault();
if (starterKits != null)
starterKitId = starterKits.Id;
else
return null;
}
else if (starterKitId.Value == Guid.Empty)
{
//if the startkit id is an empty GUID then it means the user has decided not to install one
// so we'll just exit
return null;
}
2014-02-27 10:16:30 +01:00
var result = await DownloadPackageFilesAsync(starterKitId.Value);
2017-09-14 11:41:46 +02:00
Current.RestartAppPool();
2017-09-08 19:39:13 +02:00
return new InstallSetupResult(new Dictionary<string, object>
{
{"packageId", result.Item2},
{"packageFile", result.Item1}
});
}
private async Task<Tuple<string, int>> DownloadPackageFilesAsync(Guid kitGuid)
2017-07-20 11:21:28 +02:00
{
2017-05-30 10:50:09 +02:00
//Go get the package file from the package repo
var packageFileName = await _packageService.FetchPackageFileAsync(kitGuid, UmbracoVersion.Current, _umbracoContext.Security.GetUserId().ResultOr(0));
if (packageFileName == null) throw new InvalidOperationException("Could not fetch package file " + kitGuid);
2017-05-30 10:50:09 +02:00
//add an entry to the installedPackages.config
var compiledPackage = _packageService.GetCompiledPackageInfo(packageFileName);
var packageDefinition = PackageDefinition.FromCompiledPackage(compiledPackage);
_packageService.SaveInstalledPackage(packageDefinition);
InstallPackageFiles(packageDefinition, compiledPackage.PackageFileName);
return new Tuple<string, int>(compiledPackage.PackageFileName, packageDefinition.Id);
}
private void InstallPackageFiles(PackageDefinition packageDefinition, string packageFileName)
{
if (packageDefinition == null) throw new ArgumentNullException(nameof(packageDefinition));
packageFileName = HttpUtility.UrlDecode(packageFileName);
_packageService.InstallCompiledPackageData(packageDefinition, packageFileName, _umbracoContext.Security.GetUserId().ResultOr(0));
}
public override string View => _packageService.GetAllInstalledPackages().Any() ? string.Empty : base.View;
public override bool RequiresExecution(Guid? model)
{
//Don't execute if it's an empty GUID - meaning the user has chosen not to install one
if (model.HasValue && model.Value == Guid.Empty)
{
return false;
}
if (_packageService.GetAllInstalledPackages().Any())
return false;
if (_contentService.GetRootContent().Any())
return false;
return true;
}
}
2017-07-20 11:21:28 +02:00
}