2021-06-07 17:49:20 -06:00
|
|
|
using System;
|
2017-12-28 09:18:09 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-06-09 16:56:42 -06:00
|
|
|
using System.Xml.Linq;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Packaging;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Packaging;
|
2017-12-28 09:18:09 +01:00
|
|
|
|
2021-02-23 12:24:51 +01:00
|
|
|
namespace Umbraco.Cms.Core.Services.Implement
|
2017-12-28 09:18:09 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the Packaging Service, which provides import/export functionality for the Core models of the API
|
|
|
|
|
/// using xml representation. This is primarily used by the Package functionality.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PackagingService : IPackagingService
|
|
|
|
|
{
|
2019-01-14 17:46:12 +11:00
|
|
|
private readonly IPackageInstallation _packageInstallation;
|
2021-03-16 16:13:15 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2019-01-11 14:30:04 +11:00
|
|
|
private readonly IAuditService _auditService;
|
2019-01-11 10:35:37 +11:00
|
|
|
private readonly ICreatedPackagesRepository _createdPackages;
|
2017-12-28 09:18:09 +01:00
|
|
|
|
|
|
|
|
public PackagingService(
|
2019-01-11 14:30:04 +11:00
|
|
|
IAuditService auditService,
|
|
|
|
|
ICreatedPackagesRepository createdPackages,
|
2019-11-13 21:00:54 +01:00
|
|
|
IPackageInstallation packageInstallation,
|
2021-03-16 16:13:15 +00:00
|
|
|
IEventAggregator eventAggregator)
|
2019-01-16 17:25:46 +11:00
|
|
|
{
|
2019-01-11 14:30:04 +11:00
|
|
|
_auditService = auditService;
|
2019-01-11 10:35:37 +11:00
|
|
|
_createdPackages = createdPackages;
|
2019-01-14 17:46:12 +11:00
|
|
|
_packageInstallation = packageInstallation;
|
2021-03-16 16:13:15 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2017-12-28 09:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Installation
|
|
|
|
|
|
2021-06-09 16:56:42 -06:00
|
|
|
public CompiledPackage GetCompiledPackageInfo(XDocument xml) => _packageInstallation.ReadPackage(xml);
|
2019-01-10 12:44:57 +11:00
|
|
|
|
2021-06-09 16:56:42 -06:00
|
|
|
public InstallationSummary InstallCompiledPackageData(XDocument packageXml, int userId = Constants.Security.SuperUserId)
|
2017-12-28 09:18:09 +01:00
|
|
|
{
|
2021-06-09 16:56:42 -06:00
|
|
|
CompiledPackage compiledPackage = GetCompiledPackageInfo(packageXml);
|
|
|
|
|
|
|
|
|
|
if (compiledPackage == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Could not read the package file " + packageXml);
|
|
|
|
|
}
|
2019-01-14 14:28:00 +11:00
|
|
|
|
2021-03-18 09:43:00 +01:00
|
|
|
// Trigger the Importing Package Notification and stop execution if event/user is cancelling it
|
2021-06-09 16:56:42 -06:00
|
|
|
var importingPackageNotification = new ImportingPackageNotification(compiledPackage.Name);
|
2021-03-18 09:43:00 +01:00
|
|
|
if (_eventAggregator.PublishCancelable(importingPackageNotification))
|
|
|
|
|
{
|
2021-06-11 12:47:35 -06:00
|
|
|
return new InstallationSummary(compiledPackage.Name);
|
2021-03-18 09:43:00 +01:00
|
|
|
}
|
2019-01-14 14:28:00 +11:00
|
|
|
|
2021-06-09 16:56:42 -06:00
|
|
|
var summary = _packageInstallation.InstallPackageData(compiledPackage, userId, out _);
|
2019-01-14 17:46:12 +11:00
|
|
|
|
2019-01-14 14:28:00 +11:00
|
|
|
_auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'.");
|
|
|
|
|
|
2021-03-17 12:57:24 +00:00
|
|
|
// trigger the ImportedPackage event
|
2021-06-09 15:18:47 -06:00
|
|
|
_eventAggregator.Publish(new ImportedPackageNotification(summary).WithStateFrom(importingPackageNotification));
|
2019-01-15 22:08:08 +11:00
|
|
|
|
|
|
|
|
return summary;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 16:56:42 -06:00
|
|
|
public InstallationSummary InstallCompiledPackageData(FileInfo packageXmlFile, int userId = Constants.Security.SuperUserId)
|
|
|
|
|
{
|
|
|
|
|
XDocument xml;
|
|
|
|
|
using (StreamReader streamReader = System.IO.File.OpenText(packageXmlFile.FullName))
|
|
|
|
|
{
|
|
|
|
|
xml = XDocument.Load(streamReader);
|
|
|
|
|
}
|
|
|
|
|
return InstallCompiledPackageData(xml, userId);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 09:18:09 +01:00
|
|
|
#endregion
|
|
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
#region Created/Installed Package Repositories
|
2019-01-10 12:44:57 +11:00
|
|
|
|
2021-02-09 10:22:42 +01:00
|
|
|
public void DeleteCreatedPackage(int id, int userId = Cms.Core.Constants.Security.SuperUserId)
|
2019-01-11 14:30:04 +11:00
|
|
|
{
|
|
|
|
|
var package = GetCreatedPackageById(id);
|
|
|
|
|
if (package == null) return;
|
|
|
|
|
|
|
|
|
|
_auditService.Add(AuditType.PackagerUninstall, userId, -1, "Package", $"Created package '{package.Name}' deleted. Package id: {package.Id}");
|
|
|
|
|
_createdPackages.Delete(id);
|
|
|
|
|
}
|
2019-01-10 17:18:47 +11:00
|
|
|
|
2019-01-11 10:35:37 +11:00
|
|
|
public IEnumerable<PackageDefinition> GetAllCreatedPackages() => _createdPackages.GetAll();
|
2019-01-10 17:18:47 +11:00
|
|
|
|
2019-01-11 10:35:37 +11:00
|
|
|
public PackageDefinition GetCreatedPackageById(int id) => _createdPackages.GetById(id);
|
2019-01-10 17:18:47 +11:00
|
|
|
|
2019-01-11 10:35:37 +11:00
|
|
|
public bool SaveCreatedPackage(PackageDefinition definition) => _createdPackages.SavePackage(definition);
|
2019-01-10 17:18:47 +11:00
|
|
|
|
2019-01-11 10:35:37 +11:00
|
|
|
public string ExportCreatedPackage(PackageDefinition definition) => _createdPackages.ExportPackage(definition);
|
2019-01-10 12:44:57 +11:00
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
|
2021-06-09 15:18:47 -06:00
|
|
|
// TODO: Implement
|
|
|
|
|
public IEnumerable<PackageDefinition> GetAllInstalledPackages() => Enumerable.Empty<PackageDefinition>();
|
|
|
|
|
|
2017-12-28 09:18:09 +01:00
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|