public void Export_Zip() { var mt = MediaTypeBuilder.CreateImageMediaType("testImage"); MediaTypeService.Save(mt); var m1 = MediaBuilder.CreateMediaFile(mt, -1); MediaService.Save(m1); //Ensure a file exist var fullPath = HostingEnvironment.MapPathWebRoot(m1.Properties[Constants.Conventions.Media.File].GetValue().ToString()); using (StreamWriter file1 = File.CreateText(fullPath)) { file1.WriteLine("hello"); } var def = new PackageDefinition { Name = "test", MediaUdis = new List <GuidUdi>() { m1.GetUdi() } }; bool result = PackageBuilder.SavePackage(def); Assert.IsTrue(result); Assert.IsTrue(def.PackagePath.IsNullOrWhiteSpace()); string packageXmlPath = PackageBuilder.ExportPackage(def); def = PackageBuilder.GetById(def.Id); // re-get Assert.IsNotNull(def.PackagePath); using (FileStream packageZipStream = File.OpenRead(packageXmlPath)) using (ZipArchive zipArchive = PackageMigrationResource.GetPackageDataManifest(packageZipStream, out XDocument packageXml)) { string test = "test-file.txt"; Assert.Multiple(() => { var mediaEntry = zipArchive.GetEntry("media/media/test-file.txt"); Assert.AreEqual("umbPackage", packageXml.Root.Name.ToString()); Assert.IsNotNull(mediaEntry); Assert.AreEqual(test, mediaEntry.Name); Assert.IsNotNull(zipArchive.GetEntry("package.xml")); Assert.AreEqual( $"<MediaItems><MediaSet><testImage id=\"{m1.Id}\" key=\"{m1.Key}\" parentID=\"-1\" level=\"1\" creatorID=\"-1\" sortOrder=\"0\" createDate=\"{m1.CreateDate.ToString("s")}\" updateDate=\"{m1.UpdateDate.ToString("s")}\" nodeName=\"Test File\" urlName=\"test-file\" path=\"{m1.Path}\" isDoc=\"\" nodeType=\"{mt.Id}\" nodeTypeAlias=\"testImage\" writerName=\"\" writerID=\"0\" udi=\"{m1.GetUdi()}\" mediaFilePath=\"/media/test-file.txt\"><umbracoFile><![CDATA[/media/test-file.txt]]></umbracoFile><umbracoBytes><![CDATA[100]]></umbracoBytes><umbracoExtension><![CDATA[png]]></umbracoExtension></testImage></MediaSet></MediaItems>", packageXml.Element("umbPackage").Element("MediaItems").ToString(SaveOptions.DisableFormatting)); Assert.AreEqual(2, zipArchive.Entries.Count()); Assert.AreEqual(ZipArchiveMode.Read, zipArchive.Mode); Assert.IsNull(packageXml.DocumentType); Assert.IsNull(packageXml.NextNode); Assert.IsNull(packageXml.Parent); Assert.IsNull(packageXml.PreviousNode); }); } }
protected sealed override void DefinePlan() { // calculate the final state based on the hash value of the embedded resource Type planType = GetType(); var hash = PackageMigrationResource.GetEmbeddedPackageDataManifestHash(planType); var finalId = hash.ToGuid(); To <MigrateToPackageData>(finalId); }
public void Setup() { var xml = PackageMigrationResource.GetEmbeddedPackageDataManifest(GetType()); var packagingService = GetRequiredService <IPackagingService>(); InstallationSummary = packagingService.InstallCompiledPackageData(xml); Root = InstallationSummary.ContentInstalled.First(); ContentService.SaveAndPublish(Root); var cultures = new List <string> { GetRequiredService <ILocalizationService>().GetDefaultLanguageIsoCode() }; foreach (var language in InstallationSummary.LanguagesInstalled) { cultures.Add(language.IsoCode); } Cultures = cultures.ToArray(); var httpContextAccessor = GetRequiredService <IHttpContextAccessor>(); httpContextAccessor.HttpContext = new DefaultHttpContext { Request = { Scheme = "https", Host = new HostString("localhost"), Path = "/", QueryString = new QueryString(string.Empty) } }; //Like the request middleware we specify the VariationContext to the default language. _variationContextAccessor.VariationContext = new VariationContext(Cultures[0]); GetRequiredService <IUmbracoContextFactory>().EnsureUmbracoContext(); }
public override void Execute() { if (_executed) { throw new InvalidOperationException("This expression has already been executed."); } _executed = true; Context.BuildingExpression = false; if (EmbeddedResourceMigrationType == null && PackageDataManifest == null) { throw new InvalidOperationException( $"Nothing to execute, neither {nameof(EmbeddedResourceMigrationType)} or {nameof(PackageDataManifest)} has been set."); } if (!_packageMigrationSettings.RunSchemaAndContentMigrations) { Logger.LogInformation("Skipping import of embedded schema file, due to configuration"); return; } InstallationSummary installationSummary; if (EmbeddedResourceMigrationType != null) { if (PackageMigrationResource.TryGetEmbeddedPackageDataManifest( EmbeddedResourceMigrationType, out XDocument xml, out ZipArchive zipPackage)) { // first install the package installationSummary = _packagingService.InstallCompiledPackageData(xml); if (zipPackage is not null) { // get the embedded resource using (zipPackage) { // 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 (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); } } } } } else { installationSummary = _packagingService.InstallCompiledPackageData(PackageDataManifest); } Logger.LogInformation($"Package migration executed. Summary: {installationSummary}"); }