From c3bac00f6e6eb6511be36f9ac6e9713f18f6aef4 Mon Sep 17 00:00:00 2001 From: Zeegaan <70372949+Zeegaan@users.noreply.github.com> Date: Wed, 11 Aug 2021 12:17:35 +0200 Subject: [PATCH] Fixed 11 more classes with new TryGetUmbracoContext --- .../UmbracoBuilder.CoreServices.cs | 7 ++++++- .../RichTextEditorPastedImages.cs | 3 ++- .../RteMacroRenderingValueConverter.cs | 6 +++++- .../Routing/ContentFinderByConfigured404.cs | 12 ++++++------ .../Controllers/PluginController.cs | 14 ++++++++++++-- .../Controllers/RenderController.cs | 13 ++++++++++++- .../FriendlyPublishedContentExtensions.cs | 14 ++++++++++++-- .../Extensions/PublishedContentExtensions.cs | 8 +++++++- .../UmbracoVirtualPageFilterAttribute.cs | 8 ++++++-- src/Umbraco.Web.Common/Macros/MacroRenderer.cs | 12 ++++++++++-- .../Templates/TemplateRenderer.cs | 18 +++++++++++++++--- 11 files changed, 93 insertions(+), 22 deletions(-) diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs index 6ec5128e4a..98ec92ec70 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs @@ -52,6 +52,7 @@ using Umbraco.Cms.Infrastructure.Runtime; using Umbraco.Cms.Infrastructure.Search; using Umbraco.Cms.Infrastructure.Serialization; using Umbraco.Extensions; +using System; namespace Umbraco.Cms.Infrastructure.DependencyInjection { @@ -165,7 +166,11 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection builder.Services.AddScoped(factory => { var umbCtx = factory.GetRequiredService(); - return new PublishedContentQuery(umbCtx.UmbracoContext.PublishedSnapshot, factory.GetRequiredService(), factory.GetRequiredService()); + if (!umbCtx.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + return new PublishedContentQuery(umbracoContext.PublishedSnapshot, factory.GetRequiredService(), factory.GetRequiredService()); }); // register accessors for cultures diff --git a/src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs b/src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs index 80769663c6..b8dd935b89 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs @@ -123,7 +123,8 @@ namespace Umbraco.Cms.Core.PropertyEditors img.SetAttributeValue("data-udi", udi.ToString()); // Get the new persisted image URL - var mediaTyped = _umbracoContextAccessor?.UmbracoContext?.Media.GetById(udi.Guid); + _umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext); + var mediaTyped = umbracoContext?.Media.GetById(udi.Guid); if (mediaTyped == null) throw new PanicException($"Could not find media by id {udi.Guid} or there was no UmbracoContext available."); diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs index d3ed87be24..2f01388edb 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs @@ -1,6 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. +using System; using System.Linq; using System.Text; using HtmlAgilityPack; @@ -50,7 +51,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters // different preview modes. private string RenderRteMacros(string source, bool preview) { - var umbracoContext = _umbracoContextAccessor.UmbracoContext; + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } using (umbracoContext.ForcedPreview(preview)) // force for macro rendering { var sb = new StringBuilder(); diff --git a/src/Umbraco.Infrastructure/Routing/ContentFinderByConfigured404.cs b/src/Umbraco.Infrastructure/Routing/ContentFinderByConfigured404.cs index ef392f1644..f423347b0a 100644 --- a/src/Umbraco.Infrastructure/Routing/ContentFinderByConfigured404.cs +++ b/src/Umbraco.Infrastructure/Routing/ContentFinderByConfigured404.cs @@ -1,3 +1,4 @@ +using System; using System.Globalization; using System.Linq; using Examine; @@ -46,8 +47,7 @@ namespace Umbraco.Cms.Core.Routing /// A value indicating whether an Umbraco document was found and assigned. public bool TryFindContent(IPublishedRequestBuilder frequest) { - IUmbracoContext umbCtx = _umbracoContextAccessor.UmbracoContext; - if (umbCtx == null) + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) { return false; } @@ -68,7 +68,7 @@ namespace Umbraco.Cms.Core.Routing while (pos > 1) { route = route.Substring(0, pos); - node = umbCtx.Content.GetByRoute(route, culture: frequest?.Culture); + node = umbracoContext.Content.GetByRoute(route, culture: frequest?.Culture); if (node != null) { break; @@ -79,7 +79,7 @@ namespace Umbraco.Cms.Core.Routing if (node != null) { - Domain d = DomainUtilities.FindWildcardDomainInPath(umbCtx.PublishedSnapshot.Domains.GetAll(true), node.Path, null); + Domain d = DomainUtilities.FindWildcardDomainInPath(umbracoContext.PublishedSnapshot.Domains.GetAll(true), node.Path, null); if (d != null) { errorCulture = d.Culture; @@ -90,7 +90,7 @@ namespace Umbraco.Cms.Core.Routing var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId( _contentSettings.Error404Collection.ToArray(), _entityService, - new PublishedContentQuery(umbCtx.PublishedSnapshot, umbCtx.VariationContextAccessor, _examineManager), + new PublishedContentQuery(umbracoContext.PublishedSnapshot, umbracoContext.VariationContextAccessor, _examineManager), errorCulture); IPublishedContent content = null; @@ -99,7 +99,7 @@ namespace Umbraco.Cms.Core.Routing { _logger.LogDebug("Got id={ErrorNodeId}.", error404.Value); - content = umbCtx.Content.GetById(error404.Value); + content = umbracoContext.Content.GetById(error404.Value); _logger.LogDebug(content == null ? "Could not find content with that id." diff --git a/src/Umbraco.Web.Common/Controllers/PluginController.cs b/src/Umbraco.Web.Common/Controllers/PluginController.cs index 8120ddc94d..93e9534a95 100644 --- a/src/Umbraco.Web.Common/Controllers/PluginController.cs +++ b/src/Umbraco.Web.Common/Controllers/PluginController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Core.Cache; @@ -27,7 +27,17 @@ namespace Umbraco.Cms.Web.Common.Controllers /// /// Gets the Umbraco context. /// - public virtual IUmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext; + public virtual IUmbracoContext UmbracoContext + { + get + { + if (!UmbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + return umbracoContext; + } + } /// /// Gets the database context accessor. diff --git a/src/Umbraco.Web.Common/Controllers/RenderController.cs b/src/Umbraco.Web.Common/Controllers/RenderController.cs index c6bedf7e6e..d46a6c1a87 100644 --- a/src/Umbraco.Web.Common/Controllers/RenderController.cs +++ b/src/Umbraco.Web.Common/Controllers/RenderController.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -37,7 +38,17 @@ namespace Umbraco.Cms.Web.Common.Controllers /// /// Gets the umbraco context /// - protected IUmbracoContext UmbracoContext => _umbracoContextAccessor.UmbracoContext; + protected IUmbracoContext UmbracoContext + { + get + { + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + return umbracoContext; + } + } /// /// The default action to render the front-end view. diff --git a/src/Umbraco.Web.Common/Extensions/FriendlyPublishedContentExtensions.cs b/src/Umbraco.Web.Common/Extensions/FriendlyPublishedContentExtensions.cs index e991a6a6ed..9a6615653e 100644 --- a/src/Umbraco.Web.Common/Extensions/FriendlyPublishedContentExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/FriendlyPublishedContentExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Data; using Examine; @@ -50,7 +50,17 @@ namespace Umbraco.Extensions private static IPublishedValueFallback PublishedValueFallback { get; } = StaticServiceProvider.Instance.GetRequiredService(); - private static IPublishedSnapshot PublishedSnapshot => UmbracoContextAccessor.UmbracoContext?.PublishedSnapshot; + private static IPublishedSnapshot PublishedSnapshot + { + get + { + if (!UmbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + return null; + } + return umbracoContext.PublishedSnapshot; + } + } private static IMediaTypeService MediaTypeService { get; } = StaticServiceProvider.Instance.GetRequiredService(); diff --git a/src/Umbraco.Web.Common/Extensions/PublishedContentExtensions.cs b/src/Umbraco.Web.Common/Extensions/PublishedContentExtensions.cs index 1a6823bde8..9e04ba8889 100644 --- a/src/Umbraco.Web.Common/Extensions/PublishedContentExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/PublishedContentExtensions.cs @@ -48,7 +48,13 @@ namespace Umbraco.Extensions /// a culture to that document. /// public static string GetCultureFromDomains(this IPublishedContent content, IUmbracoContextAccessor umbracoContextAccessor, ISiteDomainMapper siteDomainHelper, Uri current = null) - => DomainUtilities.GetCultureFromDomains(content.Id, content.Path, current, umbracoContextAccessor.UmbracoContext, siteDomainHelper); + { + if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + return DomainUtilities.GetCultureFromDomains(content.Id, content.Path, current, umbracoContext, siteDomainHelper); + } #endregion diff --git a/src/Umbraco.Web.Common/Filters/UmbracoVirtualPageFilterAttribute.cs b/src/Umbraco.Web.Common/Filters/UmbracoVirtualPageFilterAttribute.cs index ac656004c3..f83243b427 100644 --- a/src/Umbraco.Web.Common/Filters/UmbracoVirtualPageFilterAttribute.cs +++ b/src/Umbraco.Web.Common/Filters/UmbracoVirtualPageFilterAttribute.cs @@ -53,9 +53,13 @@ namespace Umbraco.Cms.Web.Common.Filters { if (content != null) { - IUmbracoContextAccessor umbracoContext = context.HttpContext.RequestServices.GetRequiredService(); + IUmbracoContextAccessor umbracoContextAccessor = context.HttpContext.RequestServices.GetRequiredService(); IPublishedRouter router = context.HttpContext.RequestServices.GetRequiredService(); - IPublishedRequestBuilder requestBuilder = await router.CreateRequestAsync(umbracoContext.UmbracoContext.CleanedUmbracoUrl); + if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + IPublishedRequestBuilder requestBuilder = await router.CreateRequestAsync(umbracoContext.CleanedUmbracoUrl); requestBuilder.SetPublishedContent(content); IPublishedRequest publishedRequest = requestBuilder.Build(); diff --git a/src/Umbraco.Web.Common/Macros/MacroRenderer.cs b/src/Umbraco.Web.Common/Macros/MacroRenderer.cs index b05900ca75..b4539b5896 100644 --- a/src/Umbraco.Web.Common/Macros/MacroRenderer.cs +++ b/src/Umbraco.Web.Common/Macros/MacroRenderer.cs @@ -115,8 +115,12 @@ namespace Umbraco.Cms.Web.Common.Macros // ensuring that it is appropriate to use the cache private MacroContent GetMacroContentFromCache(MacroModel model) { + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + return null; + } // only if cache is enabled - if (_umbracoContextAccessor.UmbracoContext.InPreviewMode || model.CacheDuration <= 0) + if (umbracoContext.InPreviewMode || model.CacheDuration <= 0) return null; var cache = _appCaches.RuntimeCache; @@ -151,8 +155,12 @@ namespace Umbraco.Cms.Web.Common.Macros // stores macro content into the cache private async Task AddMacroContentToCacheAsync(MacroModel model, MacroContent macroContent) { + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } // only if cache is enabled - if (_umbracoContextAccessor.UmbracoContext.InPreviewMode || model.CacheDuration <= 0) + if (umbracoContext.InPreviewMode || model.CacheDuration <= 0) return; // just make sure... diff --git a/src/Umbraco.Web.Common/Templates/TemplateRenderer.cs b/src/Umbraco.Web.Common/Templates/TemplateRenderer.cs index df72f44f10..20b37fe802 100644 --- a/src/Umbraco.Web.Common/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web.Common/Templates/TemplateRenderer.cs @@ -187,8 +187,12 @@ namespace Umbraco.Cms.Web.Common.Templates private void SetNewItemsOnContextObjects(IPublishedRequest request) { + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } // now, set the new ones for this page execution - _umbracoContextAccessor.UmbracoContext.PublishedRequest = request; + umbracoContext.PublishedRequest = request; } /// @@ -196,9 +200,13 @@ namespace Umbraco.Cms.Web.Common.Templates /// private void SaveExistingItems(out IPublishedRequest oldPublishedRequest) { + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } // Many objects require that these legacy items are in the http context items... before we render this template we need to first // save the values in them so that we can re-set them after we render so the rest of the execution works as per normal - oldPublishedRequest = _umbracoContextAccessor.UmbracoContext.PublishedRequest; + oldPublishedRequest = umbracoContext.PublishedRequest; } /// @@ -206,7 +214,11 @@ namespace Umbraco.Cms.Web.Common.Templates /// private void RestoreItems(IPublishedRequest oldPublishedRequest) { - _umbracoContextAccessor.UmbracoContext.PublishedRequest = oldPublishedRequest; + if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) + { + throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); + } + umbracoContext.PublishedRequest = oldPublishedRequest; } } }