From 8d82857348ec637ab5370513ddc1e4f1be72fa64 Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Thu, 5 Jan 2023 11:40:47 +0100 Subject: [PATCH] Add some null checks --- .../Notifications/DontShout.cs | 14 +++++--- .../CreateImageCropperValuesController.cs | 35 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Umbraco.Docs.Samples.Web/Notifications/DontShout.cs b/Umbraco.Docs.Samples.Web/Notifications/DontShout.cs index 67cd5f8..f296a55 100644 --- a/Umbraco.Docs.Samples.Web/Notifications/DontShout.cs +++ b/Umbraco.Docs.Samples.Web/Notifications/DontShout.cs @@ -13,12 +13,16 @@ public void Handle(ContentPublishingNotification notification) if (contentItem.ContentType.Alias.Equals("blogpost")) { var blogPostTitle = contentItem.GetValue("pageTitle"); - if (blogPostTitle.Equals(blogPostTitle.ToUpper())) + + if (!string.IsNullOrWhiteSpace(blogPostTitle)) { - notification.Cancel = true; - notification.CancelOperation(new EventMessage("Corporate style guideline infringement", - "Don't put the blog post title in upper case, no need to shout! Publishing was cancelled", - EventMessageType.Error)); + if (blogPostTitle.Equals(blogPostTitle.ToUpper())) + { + notification.Cancel = true; + notification.CancelOperation(new EventMessage("Corporate style guideline infringement", + "Don't put the blog post title in upper case, no need to shout! Publishing was cancelled", + EventMessageType.Error)); + } } } } diff --git a/Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs b/Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs index 7341196..a219cca 100644 --- a/Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs +++ b/Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs @@ -12,10 +12,10 @@ namespace Umbraco.Docs.Samples.Web.Property_Editors_Add_Values { public class CreateImageCropperValuesController : UmbracoApiController { - private IContentService _contentService; - private IMediaService _mediaService; - private MediaUrlGeneratorCollection _mediaUrlGeneratorCollection; - private IPublishedSnapshotAccessor _publishedSnapshotAccessor; + private readonly IContentService _contentService; + private readonly IMediaService _mediaService; + private readonly MediaUrlGeneratorCollection _mediaUrlGeneratorCollection; + private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor; public CreateImageCropperValuesController(IContentService contentService, IMediaService mediaService, MediaUrlGeneratorCollection mediaUrlGeneratorCollection, IPublishedSnapshotAccessor publishedSnapshotAccessor) @@ -48,10 +48,16 @@ public ActionResult CreateImageCropperValues() // Serialize the image cropper value var cropperValue = JsonConvert.SerializeObject(cropper); + var testCropperAlias = Product.GetModelPropertyType(_publishedSnapshotAccessor, x => x.TestCropper)?.Alias; + // Set the value of the property with alias 'cropper' - content.SetValue("testCropper", cropperValue, "en-US"); + if (content != null && !string.IsNullOrWhiteSpace(testCropperAlias)) + { + content.SetValue("testCropper", cropperValue, "en-US"); - content.SetValue(Product.GetModelPropertyType(_publishedSnapshotAccessor,x => x.TestCropper).Alias, cropperValue, "en-US"); + content.SetValue(testCropperAlias, cropperValue, "en-US"); + } + return _contentService.Save(content).Success.ToString(); } @@ -64,10 +70,21 @@ internal Dictionary GetCropUrls(IPublishedContent image) if (image.HasValue("umbracoFile")) { var imageCropper = image.Value("umbracoFile"); - foreach (var crop in imageCropper.Crops) + + if (imageCropper != null && imageCropper.Crops != null) { - //Get the cropped URL and add it to the dictionary that I will return - cropUrls.Add(crop.Alias, image.GetCropUrl(crop.Alias)); + foreach (var crop in imageCropper.Crops) + { + //Get the cropped URL and add it to the dictionary that I will return + if (!string.IsNullOrWhiteSpace(crop.Alias)) + { + var cropUrl = image.GetCropUrl(crop.Alias); + if (!string.IsNullOrEmpty(cropUrl)) + { + cropUrls.Add(crop.Alias, cropUrl); + } + } + } } }