diff --git a/src/Umbraco.Web/Models/PublishedContentBase.cs b/src/Umbraco.Web/Models/PublishedContentBase.cs index 8a7565265f..39933b49be 100644 --- a/src/Umbraco.Web/Models/PublishedContentBase.cs +++ b/src/Umbraco.Web/Models/PublishedContentBase.cs @@ -15,13 +15,13 @@ namespace Umbraco.Web.Models [DebuggerDisplay("Content Id: {Id}, Name: {Name}")] public abstract class PublishedContentBase : IPublishedContent { - private readonly IUmbracoContextAccessor _umbracoContextAccessor; - protected PublishedContentBase(IUmbracoContextAccessor umbracoContextAccessor) { - _umbracoContextAccessor = umbracoContextAccessor; + UmbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); } + protected IUmbracoContextAccessor UmbracoContextAccessor { get; } + #region ContentType public abstract PublishedContentType ContentType { get; } @@ -86,50 +86,52 @@ namespace Umbraco.Web.Models /// public virtual string GetUrl(string culture = null) // TODO: consider .GetCulture("fr-FR").Url { - var umbracoContext = _umbracoContextAccessor.UmbracoContext; - switch (ItemType) - { - case PublishedItemType.Content: - if (umbracoContext == null) - throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext is null."); - if (umbracoContext.UrlProvider == null) - throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.UrlProvider is null."); - return umbracoContext.UrlProvider.GetUrl(this, culture); + switch (ItemType) + { + case PublishedItemType.Content: + var umbracoContext = UmbracoContextAccessor.UmbracoContext; - case PublishedItemType.Media: - var prop = GetProperty(Constants.Conventions.Media.File); - if (prop?.GetValue() == null) - { - return string.Empty; - } + if (umbracoContext == null) + throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext is null."); + if (umbracoContext.UrlProvider == null) + throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.UrlProvider is null."); - var propType = ContentType.GetPropertyType(Constants.Conventions.Media.File); - - // TODO: consider implementing media url providers - // note: that one does not support variations - //This is a hack - since we now have 2 properties that support a URL: upload and cropper, we need to detect this since we always - // want to return the normal URL and the cropper stores data as json - switch (propType.EditorAlias) - { - case Constants.PropertyEditors.Aliases.UploadField: - return prop.GetValue().ToString(); - break; - case Constants.PropertyEditors.Aliases.ImageCropper: - //get the url from the json format - - var stronglyTyped = prop.GetValue() as ImageCropperValue; - if (stronglyTyped != null) - { - return stronglyTyped.Src; - } - return prop.GetValue()?.ToString(); - } + return umbracoContext.UrlProvider.GetUrl(this, culture); + case PublishedItemType.Media: + var prop = GetProperty(Constants.Conventions.Media.File); + if (prop?.GetValue() == null) + { return string.Empty; + } - default: - throw new NotSupportedException(); - } + var propType = ContentType.GetPropertyType(Constants.Conventions.Media.File); + + // TODO: consider implementing media url providers + // note: that one does not support variations + //This is a hack - since we now have 2 properties that support a URL: upload and cropper, we need to detect this since we always + // want to return the normal URL and the cropper stores data as json + switch (propType.EditorAlias) + { + case Constants.PropertyEditors.Aliases.UploadField: + + return prop.GetValue().ToString(); + case Constants.PropertyEditors.Aliases.ImageCropper: + //get the url from the json format + + var stronglyTyped = prop.GetValue() as ImageCropperValue; + if (stronglyTyped != null) + { + return stronglyTyped.Src; + } + return prop.GetValue()?.ToString(); + } + + return string.Empty; + + default: + throw new NotSupportedException(); + } } /// diff --git a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs index aa19918ca9..3526a6daeb 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs @@ -13,7 +13,6 @@ namespace Umbraco.Web.PublishedCache.NuCache internal class PublishedContent : PublishedContentBase { private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor; - private readonly IUmbracoContextAccessor _umbracoContextAccessor; private readonly ContentNode _contentNode; private readonly string _urlSegment; @@ -24,13 +23,13 @@ namespace Umbraco.Web.PublishedCache.NuCache ContentData contentData, IPublishedSnapshotAccessor publishedSnapshotAccessor, IVariationContextAccessor variationContextAccessor, - IUmbracoContextAccessor umbracoContextAccessor) :base(umbracoContextAccessor) + IUmbracoContextAccessor umbracoContextAccessor) + : base(umbracoContextAccessor) { - _contentNode = contentNode; - ContentData = contentData; - _publishedSnapshotAccessor = publishedSnapshotAccessor; - _umbracoContextAccessor = umbracoContextAccessor; - VariationContextAccessor = variationContextAccessor; + _contentNode = contentNode ?? throw new ArgumentNullException(nameof(contentNode)); + ContentData = contentData ?? throw new ArgumentNullException(nameof(contentData)); + _publishedSnapshotAccessor = publishedSnapshotAccessor ?? throw new ArgumentNullException(nameof(publishedSnapshotAccessor)); + VariationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor)); _urlSegment = ContentData.Name.ToUrlSegment(); IsPreviewing = ContentData.Published == false; @@ -72,7 +71,8 @@ namespace Umbraco.Web.PublishedCache.NuCache public PublishedContent( ContentNode contentNode, PublishedContent origin, - IUmbracoContextAccessor umbracoContextAccessor) :base(umbracoContextAccessor) + IUmbracoContextAccessor umbracoContextAccessor) + : base(umbracoContextAccessor) { _contentNode = contentNode; _publishedSnapshotAccessor = origin._publishedSnapshotAccessor; @@ -91,7 +91,8 @@ namespace Umbraco.Web.PublishedCache.NuCache // clone for previewing as draft a published content that is published and has no draft private PublishedContent( PublishedContent origin, - IUmbracoContextAccessor umbracoContextAccessor) :base(umbracoContextAccessor) + IUmbracoContextAccessor umbracoContextAccessor) + : base(umbracoContextAccessor) { _publishedSnapshotAccessor = origin._publishedSnapshotAccessor; VariationContextAccessor = origin.VariationContextAccessor; @@ -468,8 +469,8 @@ namespace Umbraco.Web.PublishedCache.NuCache return this; var cache = GetAppropriateCache(); - if (cache == null) return new PublishedContent(this, _umbracoContextAccessor).CreateModel(); - return (IPublishedContent)cache.Get(AsPreviewingCacheKey, () => new PublishedContent(this, _umbracoContextAccessor).CreateModel()); + if (cache == null) return new PublishedContent(this, UmbracoContextAccessor).CreateModel(); + return (IPublishedContent)cache.Get(AsPreviewingCacheKey, () => new PublishedContent(this, UmbracoContextAccessor).CreateModel()); } // used by Navigable.Source,...