private bool DumpAppPackage(CR2WFile cr2w, Stream appStream, string outfile) { if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <appearanceAppearanceDefinition>().Any()) { return(false); } var blobs = cr2w.Chunks.Select(_ => _.Data).OfType <appearanceAppearanceDefinition>().ToList(); List <RedFileDto> datas = new List <RedFileDto>(); foreach (var blob in blobs) { if (blob.CompiledData.IsSerialized) { var bufferIdx = blob.CompiledData.Buffer.Value; var buffer = cr2w.Buffers[bufferIdx - 1]; appStream.Seek(buffer.Offset, SeekOrigin.Begin); var packageStream = new MemoryStream(); appStream.DecompressAndCopySegment(packageStream, buffer.DiskSize, buffer.MemSize); CompiledPackage package = new CompiledPackage(_hashService); packageStream.Seek(0, SeekOrigin.Begin); package.Read(new BinaryReader(packageStream)); datas.Add(new RedFileDto(package)); } } if (datas.Count > 1) { var data = JsonConvert.SerializeObject(datas, Formatting.Indented); File.WriteAllText(outfile, data); return(true); } return(false); }
private bool DumpAppPackage(CR2WFile cr2w, Stream appStream, string outfile) { var blob = cr2w.RootChunk as appearanceAppearanceResource; var datas = new List <RedFileDto>(); foreach (var appearance in blob.Appearances) { if (appearance.Chunk.CompiledData.Buffer.MemSize > 0) { var packageStream = new MemoryStream(); packageStream.Write(appearance.Chunk.CompiledData.Buffer.GetBytes()); var package = new CompiledPackage(_hashService); packageStream.Seek(0, SeekOrigin.Begin); package.Read(new BinaryReader(packageStream)); datas.Add(new RedFileDto(cr2w)); } } if (datas.Count > 1) { var data = RedJsonSerializer.Serialize(datas); File.WriteAllText(outfile, data); return(true); } return(false); }
private bool DumpEntPackage(CR2WFile cr2w, Stream entStream, string outfile) { if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <entEntityTemplate>().Any()) { return(false); } var blob = cr2w.Chunks.Select(_ => _.Data).OfType <entEntityTemplate>().First(); if (blob.CompiledData.IsSerialized) { var bufferIdx = blob.CompiledData.Buffer.Value; var buffer = cr2w.Buffers[bufferIdx - 1]; entStream.Seek(buffer.Offset, SeekOrigin.Begin); var packageStream = new MemoryStream(); entStream.DecompressAndCopySegment(packageStream, buffer.DiskSize, buffer.MemSize); CompiledPackage package = new CompiledPackage(_hashService); packageStream.Seek(0, SeekOrigin.Begin); package.Read(new BinaryReader(packageStream)); string data = JsonConvert.SerializeObject(new RedFileDto(package), Formatting.Indented); File.WriteAllText(outfile, data); return(true); } return(false); }
public InstallationSummary InstallCompiledPackageData(XDocument?packageXml, int userId = Constants.Security.SuperUserId) { CompiledPackage compiledPackage = GetCompiledPackageInfo(packageXml); if (compiledPackage == null) { throw new InvalidOperationException("Could not read the package file " + packageXml); } // Trigger the Importing Package Notification and stop execution if event/user is cancelling it var importingPackageNotification = new ImportingPackageNotification(compiledPackage.Name); if (_eventAggregator.PublishCancelable(importingPackageNotification)) { return(new InstallationSummary(compiledPackage.Name)); } var summary = _packageInstallation.InstallPackageData(compiledPackage, userId, out _); _auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'."); // trigger the ImportedPackage event _eventAggregator.Publish(new ImportedPackageNotification(summary).WithStateFrom(importingPackageNotification)); return(summary); }
public void Can_Read_Compiled_Package_Warnings() { // Copy a file to the same path that the package will install so we can detect file conflicts. string filePath = Path.Combine(HostingEnvironment.MapPathContentRoot("~/"), "bin", "Auros.DocumentTypePicker.dll"); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); File.WriteAllText(filePath, "test"); // this is where our test zip file is string packageFile = Path.Combine(HostingEnvironment.MapPathContentRoot("~/TestData/Packages"), DocumentTypePickerPackage); Console.WriteLine(packageFile); using var fileStream = File.OpenRead(packageFile); CompiledPackage package = PackageInstallation.ReadPackage(XDocument.Load(fileStream)); InstallWarnings preInstallWarnings = package.Warnings; var dataType = package.DataTypes.First(); Assert.Multiple(() => { Assert.AreEqual("Document Type Picker", package.Name); Assert.AreEqual("3593d8e7-8b35-47b9-beda-5e830ca8c93c", dataType.LastAttribute?.Value); Assert.AreEqual("Document Type Picker", dataType.FirstAttribute?.Value); Assert.IsNotNull(preInstallWarnings); Assert.AreEqual(0, preInstallWarnings.ConflictingMacros.Count()); Assert.AreEqual(0, preInstallWarnings.ConflictingStylesheets.Count()); Assert.AreEqual(0, preInstallWarnings.ConflictingTemplates.Count()); }); }
private void ValidatePackageFile(FileInfo packageFile, CompiledPackage package) { if (!(package.Files?.Count > 0)) { return; } var sourceDestination = _parser.ExtractSourceDestinationFileInformation(package.Files).ToArray(); var missingFiles = _packageExtraction.FindMissingFiles(packageFile, sourceDestination.Select(i => i.packageUniqueFile)).ToArray(); if (missingFiles.Any()) { throw new Exception("The following file(s) are missing in the package: " + string.Join(", ", missingFiles.Select( mf => { var(packageUniqueFile, appRelativePath) = sourceDestination.Single(fi => fi.packageUniqueFile == mf); return($"source: \"{packageUniqueFile}\" destination: \"{appRelativePath}\""); }))); } IEnumerable <string> duplicates = _packageExtraction.FindDuplicateFileNames(packageFile).ToArray(); if (duplicates.Any()) { throw new Exception("The following filename(s) are found more than one time in the package, since the filename is used ad primary key, this is not allowed: " + string.Join(", ", duplicates)); } }
/// <summary> /// Returns a list of all installed file paths /// </summary> /// <param name="compiledPackage"></param> /// <param name="packageFile"></param> /// <param name="targetRootFolder"> /// The absolute path of where to extract the package files (normally the application root) /// </param> /// <returns></returns> public IEnumerable <string> InstallFiles(CompiledPackage compiledPackage, FileInfo packageFile, string targetRootFolder) { using (_logger.DebugDuration <PackageFileInstallation>( "Installing package files for package " + compiledPackage.Name, "Package file installation complete for package " + compiledPackage.Name)) { var sourceAndRelativeDest = _parser.ExtractSourceDestinationFileInformation(compiledPackage.Files); var sourceAndAbsDest = AppendRootToDestination(targetRootFolder, sourceAndRelativeDest); _packageExtraction.CopyFilesFromArchive(packageFile, sourceAndAbsDest); return(sourceAndRelativeDest.Select(sd => sd.appRelativePath).ToArray()); } }
public void Install_Data() { var testPackageFile = new FileInfo(Path.Combine(HostingEnvironment.MapPathContentRoot("~/TestData/Packages"), DocumentTypePickerPackage)); using var fileStream = testPackageFile.OpenRead(); CompiledPackage package = PackageInstallation.ReadPackage(XDocument.Load(fileStream)); InstallationSummary summary = PackageInstallation.InstallPackageData(package, -1, out PackageDefinition def); Assert.AreEqual(1, summary.DataTypesInstalled.Count()); // make sure the def is updated too Assert.AreEqual(summary.DataTypesInstalled.Count(), def.DataTypes.Count); }
public void Can_Read_Compiled_Package_1() { var testPackageFile = new FileInfo(Path.Combine(HostingEnvironment.MapPathContentRoot("~/TestData/Packages"), DocumentTypePickerPackage)); using var fileStream = testPackageFile.OpenRead(); CompiledPackage package = PackageInstallation.ReadPackage(XDocument.Load(fileStream)); Assert.Multiple(() => { Assert.IsNotNull(package); Assert.AreEqual("Document Type Picker", package.Name); Assert.AreEqual(1, package.DataTypes.Count()); }); }
private PreInstallWarnings GetPreInstallWarnings(CompiledPackage package, string applicationRootFolder) { var sourceDestination = ExtractSourceDestinationFileInformation(package.Files); var installWarnings = new PreInstallWarnings { ConflictingMacros = _conflictingPackageData.FindConflictingMacros(package.Macros), ConflictingTemplates = _conflictingPackageData.FindConflictingTemplates(package.Templates), ConflictingStylesheets = _conflictingPackageData.FindConflictingStylesheets(package.Stylesheets), UnsecureFiles = FindUnsecureFiles(sourceDestination), FilesReplaced = FindFilesToBeReplaced(sourceDestination, applicationRootFolder) }; return(installWarnings); }
private bool DumpEntPackage(CR2WFile cr2w, Stream entStream, string outfile) { var blob = cr2w.RootChunk as entEntityTemplate; if (blob.CompiledData.Buffer.MemSize > 0) { var packageStream = new MemoryStream(); packageStream.Write(blob.CompiledData.Buffer.GetBytes()); var package = new CompiledPackage(_hashService); packageStream.Seek(0, SeekOrigin.Begin); package.Read(new BinaryReader(packageStream)); var data = RedJsonSerializer.Serialize(new RedFileDto(cr2w)); File.WriteAllText(outfile, data); return(true); } return(false); }
/// <summary> /// This will install the Articulate package based on the actual articulate package manifest file which is embedded /// into this assembly. /// </summary> /// <param name="userId"></param> /// <returns></returns> /// <remarks> /// This would be like installing the package in the back office to install all schema, etc... but we do this /// without the full package file, just to install the schema and content and to add the package to the package repo. /// </remarks> private bool InstallPackage(int userId) { //TODO: We need to reflect here because this isn't public and we're resolving the internal type from DI var parserType = _contentService.GetType().Assembly.GetType("Umbraco.Core.Packaging.CompiledPackageXmlParser"); if (parserType == null) { throw new InvalidOperationException("Could not get type Umbraco.Core.Packaging.CompiledPackageXmlParser"); } var parser = Current.Factory.GetInstance(parserType); //these are the parameters required, the fake FileInfo doesn't really do anything in this context var fakePackageFile = new FileInfo(Path.Combine(IOHelper.MapPath("~/App_Data/TEMP/Articulate"), Guid.NewGuid().ToString(), "fake-package.zip")); var xdoc = XDocument.Parse(ArticulateResources.packageManifest); //read in the xdocument package xml var appRoot = GetRootDirectorySafe(); //the root folder (based on what is passed in the Core) //reflect, call ToCompiledPackage to get the CompiledPackage reference CompiledPackage compiledPackage = (CompiledPackage)parser.CallMethod("ToCompiledPackage", xdoc, fakePackageFile, appRoot); //TODO: Need to reflect again to get the package definition PackageDefinition packageDefinition = (PackageDefinition)typeof(PackageDefinition).CallStaticMethod("FromCompiledPackage", compiledPackage); //if it's not installed or it's not the same version, then we need to run the installer if (!IsPackageVersionAlreadyInstalled(packageDefinition.Name, packageDefinition.Version, out var sameVersion, out var packageId) || !sameVersion) { //clear out the files, we don't want to save this package manifest with files since we don't want to delete them on package //uninstallation done in the back office since this will generally only be the case when we are installing via Nuget packageDefinition.Files = new List <string>(); var summary = _packageInstallation.InstallPackageData(packageDefinition, compiledPackage, userId); //persist this to the package repo, it will now show up as installed packages in the back office _packagingService.SaveInstalledPackage(packageDefinition); return(true); } return(false); }
public InstallationSummary InstallPackageData(PackageDefinition packageDefinition, CompiledPackage compiledPackage, int userId) { var installationSummary = _packageDataInstallation.InstallPackageData(compiledPackage, userId); installationSummary.Actions = CompiledPackageXmlParser.GetPackageActions(XElement.Parse(compiledPackage.Actions), compiledPackage.Name); installationSummary.MetaData = compiledPackage; installationSummary.FilesInstalled = packageDefinition.Files; //make sure the definition is up to date with everything foreach (var x in installationSummary.DataTypesInstalled) { packageDefinition.DataTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.LanguagesInstalled) { packageDefinition.Languages.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DictionaryItemsInstalled) { packageDefinition.DictionaryItems.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.MacrosInstalled) { packageDefinition.Macros.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.TemplatesInstalled) { packageDefinition.Templates.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DocumentTypesInstalled) { packageDefinition.DocumentTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.StylesheetsInstalled) { packageDefinition.Stylesheets.Add(x.Id.ToInvariantString()); } var contentInstalled = installationSummary.ContentInstalled.ToList(); packageDefinition.ContentNodeId = contentInstalled.Count > 0 ? contentInstalled[0].Id.ToInvariantString() : null; //run package actions installationSummary.ActionErrors = RunPackageActions(packageDefinition, installationSummary.Actions).ToList(); return(installationSummary); }
public InstallationSummary InstallPackageData(CompiledPackage compiledPackage, int userId, out PackageDefinition packageDefinition) { packageDefinition = new PackageDefinition { Name = compiledPackage.Name }; InstallationSummary installationSummary = _packageDataInstallation.InstallPackageData(compiledPackage, userId); // Make sure the definition is up to date with everything (note: macro partial views are embedded in macros) foreach (var x in installationSummary.DataTypesInstalled) { packageDefinition.DataTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.LanguagesInstalled) { packageDefinition.Languages.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DictionaryItemsInstalled) { packageDefinition.DictionaryItems.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.MacrosInstalled) { packageDefinition.Macros.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.TemplatesInstalled) { packageDefinition.Templates.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DocumentTypesInstalled) { packageDefinition.DocumentTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.MediaTypesInstalled) { packageDefinition.MediaTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.StylesheetsInstalled) { packageDefinition.Stylesheets.Add(x.Path); } foreach (var x in installationSummary.ScriptsInstalled) { packageDefinition.Scripts.Add(x.Path); } foreach (var x in installationSummary.PartialViewsInstalled) { packageDefinition.PartialViews.Add(x.Path); } packageDefinition.ContentNodeId = installationSummary.ContentInstalled.FirstOrDefault()?.Id.ToInvariantString(); foreach (var x in installationSummary.MediaInstalled) { packageDefinition.MediaUdis.Add(x.GetUdi()); } return(installationSummary); }
public InstallationSummary InstallPackageData(PackageDefinition packageDefinition, CompiledPackage compiledPackage, int userId) { var installationSummary = new InstallationSummary { DataTypesInstalled = _packageDataInstallation.ImportDataTypes(compiledPackage.DataTypes.ToList(), userId), LanguagesInstalled = _packageDataInstallation.ImportLanguages(compiledPackage.Languages, userId), DictionaryItemsInstalled = _packageDataInstallation.ImportDictionaryItems(compiledPackage.DictionaryItems, userId), MacrosInstalled = _packageDataInstallation.ImportMacros(compiledPackage.Macros, userId), TemplatesInstalled = _packageDataInstallation.ImportTemplates(compiledPackage.Templates.ToList(), userId), DocumentTypesInstalled = _packageDataInstallation.ImportDocumentTypes(compiledPackage.DocumentTypes, userId) }; //we need a reference to the imported doc types to continue var importedDocTypes = installationSummary.DocumentTypesInstalled.ToDictionary(x => x.Alias, x => x); installationSummary.StylesheetsInstalled = _packageDataInstallation.ImportStylesheets(compiledPackage.Stylesheets, userId); installationSummary.ContentInstalled = _packageDataInstallation.ImportContent(compiledPackage.Documents, importedDocTypes, userId); installationSummary.Actions = CompiledPackageXmlParser.GetPackageActions(XElement.Parse(compiledPackage.Actions), compiledPackage.Name); installationSummary.MetaData = compiledPackage; installationSummary.FilesInstalled = packageDefinition.Files; //make sure the definition is up to date with everything foreach (var x in installationSummary.DataTypesInstalled) { packageDefinition.DataTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.LanguagesInstalled) { packageDefinition.Languages.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DictionaryItemsInstalled) { packageDefinition.DictionaryItems.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.MacrosInstalled) { packageDefinition.Macros.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.TemplatesInstalled) { packageDefinition.Templates.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.DocumentTypesInstalled) { packageDefinition.DocumentTypes.Add(x.Id.ToInvariantString()); } foreach (var x in installationSummary.StylesheetsInstalled) { packageDefinition.Stylesheets.Add(x.Id.ToInvariantString()); } var contentInstalled = installationSummary.ContentInstalled.ToList(); packageDefinition.ContentNodeId = contentInstalled.Count > 0 ? contentInstalled[0].Id.ToInvariantString() : null; //run package actions installationSummary.ActionErrors = RunPackageActions(packageDefinition, installationSummary.Actions).ToList(); return(installationSummary); }
public IEnumerable <string> InstallPackageFiles(PackageDefinition packageDefinition, CompiledPackage compiledPackage, int userId) { if (packageDefinition == null) { throw new ArgumentNullException(nameof(packageDefinition)); } if (compiledPackage == null) { throw new ArgumentNullException(nameof(compiledPackage)); } //these should be the same, TODO: we should have a better validator for this if (packageDefinition.Name != compiledPackage.Name) { throw new InvalidOperationException("The package definition does not match the compiled package manifest"); } var packageZipFile = compiledPackage.PackageFile; var files = _packageFileInstallation.InstallFiles(compiledPackage, packageZipFile, _applicationRootFolder.FullName).ToList(); packageDefinition.Files = files; return(files); }
public CompiledPackage ToCompiledPackage(XDocument xml, FileInfo packageFile, string applicationRootFolder) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } if (xml.Root == null) { throw new ArgumentException(nameof(xml), "The xml document is invalid"); } if (xml.Root.Name != "umbPackage") { throw new FormatException("The xml document is invalid"); } var info = xml.Root.Element("info"); if (info == null) { throw new FormatException("The xml document is invalid"); } var package = info.Element("package"); if (package == null) { throw new FormatException("The xml document is invalid"); } var author = info.Element("author"); if (author == null) { throw new FormatException("The xml document is invalid"); } var requirements = package.Element("requirements"); if (requirements == null) { throw new FormatException("The xml document is invalid"); } var def = new CompiledPackage { PackageFile = packageFile, Name = package.Element("name")?.Value, Author = author.Element("name")?.Value, AuthorUrl = author.Element("website")?.Value, Contributors = info.Element("contributors")?.Elements("contributor").Select(x => x.Value).ToList() ?? new List <string>(), Version = package.Element("version")?.Value, Readme = info.Element("readme")?.Value, License = package.Element("license")?.Value, LicenseUrl = package.Element("license")?.AttributeValue <string>("url"), Url = package.Element("url")?.Value, IconUrl = package.Element("iconUrl")?.Value, UmbracoVersion = new Version((int)requirements.Element("major"), (int)requirements.Element("minor"), (int)requirements.Element("patch")), UmbracoVersionRequirementsType = requirements.AttributeValue <string>("type").IsNullOrWhiteSpace() ? RequirementsType.Legacy : Enum <RequirementsType> .Parse(requirements.AttributeValue <string>("type"), true), PackageView = xml.Root.Element("view")?.Value, Actions = xml.Root.Element("Actions")?.ToString(SaveOptions.None) ?? "<Actions></Actions>", //take the entire outer xml value Files = xml.Root.Element("files")?.Elements("file")?.Select(CompiledPackageFile.Create).ToList() ?? new List <CompiledPackageFile>(), Macros = xml.Root.Element("Macros")?.Elements("macro") ?? Enumerable.Empty <XElement>(), Templates = xml.Root.Element("Templates")?.Elements("Template") ?? Enumerable.Empty <XElement>(), Stylesheets = xml.Root.Element("Stylesheets")?.Elements("styleSheet") ?? Enumerable.Empty <XElement>(), DataTypes = xml.Root.Element("DataTypes")?.Elements("DataType") ?? Enumerable.Empty <XElement>(), Languages = xml.Root.Element("Languages")?.Elements("Language") ?? Enumerable.Empty <XElement>(), DictionaryItems = xml.Root.Element("DictionaryItems")?.Elements("DictionaryItem") ?? Enumerable.Empty <XElement>(), DocumentTypes = xml.Root.Element("DocumentTypes")?.Elements("DocumentType") ?? Enumerable.Empty <XElement>(), Documents = xml.Root.Element("Documents")?.Elements("DocumentSet")?.Select(CompiledPackageDocument.Create) ?? Enumerable.Empty <CompiledPackageDocument>(), }; def.Warnings = GetPreInstallWarnings(def, applicationRootFolder); return(def); }