private void InstallBusinessLogic(int packageId)
        {
            var definition = _packagingService.GetInstalledPackageById(packageId);

            if (definition == null)
            {
                throw new InvalidOperationException("Not package definition found with id " + packageId);
            }

            var packageFile = new FileInfo(definition.PackagePath);

            _packagingService.InstallCompiledPackageData(definition, packageFile, _umbracoContextAccessor.UmbracoContext.Security.GetUserId().ResultOr(-1));
        }
Exemplo n.º 2
0
        public void Setup()
        {
            XDocument         xml = PackageMigrationResource.GetEmbeddedPackageDataManifest(GetType());
            IPackagingService packagingService = GetRequiredService <IPackagingService>();

            InstallationSummary = packagingService.InstallCompiledPackageData(xml);

            Root = InstallationSummary.ContentInstalled.First();
            ContentService.SaveAndPublish(Root);

            var cultures = new List <string>();

            cultures.Add(GetRequiredService <ILocalizationService>().GetDefaultLanguageIsoCode());

            foreach (ILanguage language in InstallationSummary.LanguagesInstalled)
            {
                cultures.Add(language.IsoCode);
            }

            Cultures = cultures.ToArray();

            IHttpContextAccessor 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();
        }
Exemplo n.º 3
0
        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}");
            }