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

109 lines
4.3 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
2019-01-15 22:08:08 +11:00
using System.IO;
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
2019-01-15 22:08:08 +11:00
var (packageFile, packageId) = await DownloadPackageFilesAsync(starterKitId.Value);
2019-01-16 11:37:20 +01:00
UmbracoApplication.Restart();
2017-09-08 19:39:13 +02:00
return new InstallSetupResult(new Dictionary<string, object>
{
2019-01-15 22:08:08 +11:00
{"packageId", packageId},
{"packageFile", packageFile}
});
}
2019-01-15 22:08:08 +11:00
private async Task<(string packageFile, int packageId)> 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
2019-01-15 22:08:08 +11:00
var packageFile = await _packageService.FetchPackageFileAsync(kitGuid, UmbracoVersion.Current, _umbracoContext.Security.GetUserId().ResultOr(0));
if (packageFile == 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
2019-01-15 22:08:08 +11:00
var compiledPackage = _packageService.GetCompiledPackageInfo(packageFile);
var packageDefinition = PackageDefinition.FromCompiledPackage(compiledPackage);
packageDefinition.PackagePath = packageFile.FullName;
_packageService.SaveInstalledPackage(packageDefinition);
2019-01-15 22:08:08 +11:00
InstallPackageFiles(packageDefinition, compiledPackage.PackageFile);
2019-01-15 22:08:08 +11:00
return (compiledPackage.PackageFile.Name, packageDefinition.Id);
}
2019-01-15 22:08:08 +11:00
private void InstallPackageFiles(PackageDefinition packageDefinition, FileInfo packageFile)
{
if (packageDefinition == null) throw new ArgumentNullException(nameof(packageDefinition));
2019-01-15 22:08:08 +11:00
_packageService.InstallCompiledPackageData(packageDefinition, packageFile, _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
}