Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v9/dev' into v9/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmania committed Aug 11, 2021
2 parents 5ae0b64 + 558352e commit 805a228
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 102 deletions.
80 changes: 72 additions & 8 deletions src/Umbraco.Core/Packaging/PackageMigrationResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,99 @@ namespace Umbraco.Cms.Core.Packaging
{
public static class PackageMigrationResource
{
private static Stream GetEmbeddedPackageStream(Type planType)
private static Stream GetEmbeddedPackageZipStream(Type planType)
{
// lookup the embedded resource by convention
Assembly currentAssembly = planType.Assembly;
var fileName = $"{planType.Namespace}.package.zip";
Stream stream = currentAssembly.GetManifestResourceStream(fileName);

return stream;
}

public static XDocument GetEmbeddedPackageDataManifest(Type planType, out ZipArchive zipArchive)
{
XDocument packageXml;
var zipStream = GetEmbeddedPackageZipStream(planType);
if (zipStream is not null)
{
zipArchive = GetPackageDataManifest(zipStream, out packageXml);
return packageXml;
}

zipArchive = null;
packageXml = GetEmbeddedPackageXmlDoc(planType);
return packageXml;
}

public static XDocument GetEmbeddedPackageDataManifest(Type planType)
{
return GetEmbeddedPackageDataManifest(planType, out _);
}

private static XDocument GetEmbeddedPackageXmlDoc(Type planType)
{
// lookup the embedded resource by convention
Assembly currentAssembly = planType.Assembly;
var fileName = $"{planType.Namespace}.package.xml";
Stream stream = currentAssembly.GetManifestResourceStream(fileName);
if (stream == null)
{
throw new FileNotFoundException("Cannot find the embedded file.", fileName);
return null;
}
return stream;
XDocument xml;
using (stream)
{
xml = XDocument.Load(stream);
}
return xml;
}

public static string GetEmbeddedPackageDataManifestHash(Type planType)
{
// SEE: HashFromStreams in the benchmarks project for how fast this is. It will run
// on every startup for every embedded package.zip. The bigger the zip, the more time it takes.
// But it is still very fast ~303ms for a 100MB file. This will only be an issue if there are
// several very large package.zips.
// several very large package.zips.

using Stream stream = GetEmbeddedPackageZipStream(planType);

if (stream is not null)
{
return stream.GetStreamHash();
}

var xml = GetEmbeddedPackageXmlDoc(planType);

if (xml is not null)
{
return xml.ToString();
}

using Stream stream = GetEmbeddedPackageStream(planType);
return stream.GetStreamHash();
throw new IOException("Missing embedded files for planType: " + planType);
}

public static ZipArchive GetEmbeddedPackageDataManifest(Type planType, out XDocument packageXml)
=> GetPackageDataManifest(GetEmbeddedPackageStream(planType), out packageXml);
public static bool TryGetEmbeddedPackageDataManifest(Type planType, out XDocument packageXml, out ZipArchive zipArchive)
{
var zipStream = GetEmbeddedPackageZipStream(planType);
if (zipStream is not null)
{
zipArchive = GetPackageDataManifest(zipStream, out packageXml);
return true;
}

zipArchive = null;
packageXml = GetEmbeddedPackageXmlDoc(planType);
return packageXml is not null;
}

public static ZipArchive GetPackageDataManifest(Stream packageZipStream, out XDocument packageXml)
{
if (packageZipStream == null)
{
throw new ArgumentNullException(nameof(packageZipStream));
}

var zip = new ZipArchive(packageZipStream, ZipArchiveMode.Read);
ZipArchiveEntry packageXmlEntry = zip.GetEntry("package.xml");
if (packageXmlEntry == null)
Expand Down
53 changes: 35 additions & 18 deletions src/Umbraco.Core/Packaging/PackagesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,46 @@ public string ExportPackage(PackageDefinition definition)
PackageDataTypes(definition, root);
Dictionary<string, Stream> mediaFiles = PackageMedia(definition, root);

var tempPackagePath = temporaryPath + "/package.zip";

using (FileStream fileStream = File.OpenWrite(tempPackagePath))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
string fileName;
string tempPackagePath;
if (mediaFiles.Count > 0)
{
ZipArchiveEntry packageXmlEntry = archive.CreateEntry("package.xml");
using (Stream entryStream = packageXmlEntry.Open())
fileName = "package.zip";
tempPackagePath = Path.Combine(temporaryPath, fileName);
using (FileStream fileStream = File.OpenWrite(tempPackagePath))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
{
compiledPackageXml.Save(entryStream);
}
ZipArchiveEntry packageXmlEntry = archive.CreateEntry("package.xml");
using (Stream entryStream = packageXmlEntry.Open())
{
compiledPackageXml.Save(entryStream);
}

foreach (KeyValuePair<string, Stream> mediaFile in mediaFiles)
{
var entryPath = $"media{mediaFile.Key.EnsureStartsWith('/')}";
ZipArchiveEntry mediaEntry = archive.CreateEntry(entryPath);
using (Stream entryStream = mediaEntry.Open())
using (mediaFile.Value)
foreach (KeyValuePair<string, Stream> mediaFile in mediaFiles)
{
mediaFile.Value.Seek(0, SeekOrigin.Begin);
mediaFile.Value.CopyTo(entryStream);
var entryPath = $"media{mediaFile.Key.EnsureStartsWith('/')}";
ZipArchiveEntry mediaEntry = archive.CreateEntry(entryPath);
using (Stream entryStream = mediaEntry.Open())
using (mediaFile.Value)
{
mediaFile.Value.Seek(0, SeekOrigin.Begin);
mediaFile.Value.CopyTo(entryStream);
}
}
}
}
else
{
fileName = "package.xml";
tempPackagePath = Path.Combine(temporaryPath, fileName);

using (FileStream fileStream = File.OpenWrite(tempPackagePath))
{
compiledPackageXml.Save(fileStream);
}
}



var directoryName =
_hostingEnvironment.MapPathWebRoot(Path.Combine(_mediaFolderPath, definition.Name.Replace(' ', '_')));
Expand All @@ -241,7 +258,7 @@ public string ExportPackage(PackageDefinition definition)
Directory.CreateDirectory(directoryName);
}

var finalPackagePath = Path.Combine(directoryName, "package.zip");
var finalPackagePath = Path.Combine(directoryName, fileName);

if (File.Exists(finalPackagePath))
{
Expand Down Expand Up @@ -347,7 +364,7 @@ private void PackageDictionaryItems(PackageDefinition definition, XContainer roo
}
else if (items.ContainsKey(dictionaryItem.ParentId.Value))
{
// we know the parent exists in the dictionary but
// we know the parent exists in the dictionary but
// we haven't processed it yet so we'll leave it for the next loop
continue;
}
Expand Down
103 changes: 56 additions & 47 deletions src/Umbraco.Infrastructure/Packaging/ImportPackageBuilderExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace Umbraco.Cms.Infrastructure.Packaging
{
internal class ImportPackageBuilderExpression : MigrationExpressionBase
{
private readonly IPackagingService _packagingService;
private readonly IMediaService _mediaService;
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
private readonly MediaFileManager _mediaFileManager;
private readonly IMediaService _mediaService;
private readonly MediaUrlGeneratorCollection _mediaUrlGenerators;
private readonly IPackagingService _packagingService;
private readonly IShortStringHelper _shortStringHelper;
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
private bool _executed;

public ImportPackageBuilderExpression(
Expand All @@ -45,7 +45,7 @@ public ImportPackageBuilderExpression(
}

/// <summary>
/// The type of the migration which dictates the namespace of the embedded resource
/// The type of the migration which dictates the namespace of the embedded resource
/// </summary>
public Type EmbeddedResourceMigrationType { get; set; }

Expand All @@ -63,68 +63,77 @@ public override void Execute()

if (EmbeddedResourceMigrationType == null && PackageDataManifest == null)
{
throw new InvalidOperationException($"Nothing to execute, neither {nameof(EmbeddedResourceMigrationType)} or {nameof(PackageDataManifest)} has been set.");
throw new InvalidOperationException(
$"Nothing to execute, neither {nameof(EmbeddedResourceMigrationType)} or {nameof(PackageDataManifest)} has been set.");
}

InstallationSummary installationSummary;
if (EmbeddedResourceMigrationType != null)
{
// get the embedded resource
using (ZipArchive zipPackage = PackageMigrationResource.GetEmbeddedPackageDataManifest(
if (PackageMigrationResource.TryGetEmbeddedPackageDataManifest(
EmbeddedResourceMigrationType,
out XDocument xml))
out XDocument xml, out ZipArchive zipPackage))
{
// first install the package
installationSummary = _packagingService.InstallCompiledPackageData(xml);

// then we need to save each file to the saved media items
var mediaWithFiles = xml.XPathSelectElements(
"./umbPackage/MediaItems/MediaSet//*[@id][@mediaFilePath]")
.ToDictionary(
x => x.AttributeValue<Guid>("key"),
x => x.AttributeValue<string>("mediaFilePath"));

// Any existing media by GUID will not be installed by the package service, it will just be skipped
// so you cannot 'update' media (or content) using a package since those are not schema type items.
// This means you cannot 'update' the media file either. The installationSummary.MediaInstalled
// will be empty for any existing media which means that the files will also not be updated.
foreach (IMedia media in installationSummary.MediaInstalled)
if (zipPackage is not null)
{
if (mediaWithFiles.TryGetValue(media.Key, out var mediaFilePath))
// get the embedded resource
using (zipPackage)
{
// this is a media item that has a file, so find that file in the zip
var entryPath = $"media{mediaFilePath.EnsureStartsWith('/')}";
ZipArchiveEntry mediaEntry = zipPackage.GetEntry(entryPath);
if (mediaEntry == null)
{
throw new InvalidOperationException("No media file found in package zip for path " + entryPath);
}
// then we need to save each file to the saved media items
var mediaWithFiles = xml.XPathSelectElements(
"./umbPackage/MediaItems/MediaSet//*[@id][@mediaFilePath]")
.ToDictionary(
x => x.AttributeValue<Guid>("key"),
x => x.AttributeValue<string>("mediaFilePath"));

// read the media file and save it to the media item
// using the current file system provider.
using (Stream mediaStream = mediaEntry.Open())
// Any existing media by GUID will not be installed by the package service, it will just be skipped
// so you cannot 'update' media (or content) using a package since those are not schema type items.
// This means you cannot 'update' the media file either. The installationSummary.MediaInstalled
// will be empty for any existing media which means that the files will also not be updated.
foreach (IMedia media in installationSummary.MediaInstalled)
{
media.SetValue(
_mediaFileManager,
_mediaUrlGenerators,
_shortStringHelper,
_contentTypeBaseServiceProvider,
Constants.Conventions.Media.File,
Path.GetFileName(mediaFilePath),
mediaStream);
}
if (mediaWithFiles.TryGetValue(media.Key, out var mediaFilePath))
{
// this is a media item that has a file, so find that file in the zip
var entryPath = $"media{mediaFilePath.EnsureStartsWith('/')}";
ZipArchiveEntry mediaEntry = zipPackage.GetEntry(entryPath);
if (mediaEntry == null)
{
throw new InvalidOperationException(
"No media file found in package zip for path " +
entryPath);
}

// read the media file and save it to the media item
// using the current file system provider.
using (Stream mediaStream = mediaEntry.Open())
{
media.SetValue(
_mediaFileManager,
_mediaUrlGenerators,
_shortStringHelper,
_contentTypeBaseServiceProvider,
Constants.Conventions.Media.File,
Path.GetFileName(mediaFilePath),
mediaStream);
}

_mediaService.Save(media);
_mediaService.Save(media);
}
}
}
}
}
}
else
{
installationSummary = _packagingService.InstallCompiledPackageData(PackageDataManifest);
}
else
{
installationSummary = _packagingService.InstallCompiledPackageData(PackageDataManifest);
}

Logger.LogInformation($"Package migration executed. Summary: {installationSummary}");
Logger.LogInformation($"Package migration executed. Summary: {installationSummary}");
}
}
}
}
Loading

0 comments on commit 805a228

Please sign in to comment.