From b145f093689f57283fda2df6fd9fa87d2bef9a82 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Wed, 8 Sep 2021 12:03:56 +0200 Subject: [PATCH 1/2] Ensure TryGetMediaPath only returns true on non-empty values --- .../MediaUrlGeneratorCollection.cs | 29 +++++++++---------- .../FileUploadPropertyEditor.cs | 19 ++++++------ .../ImageCropperPropertyEditor.cs | 18 ++++++------ 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/Umbraco.Core/PropertyEditors/MediaUrlGeneratorCollection.cs b/src/Umbraco.Core/PropertyEditors/MediaUrlGeneratorCollection.cs index f3adb6a05f..380017c5a2 100644 --- a/src/Umbraco.Core/PropertyEditors/MediaUrlGeneratorCollection.cs +++ b/src/Umbraco.Core/PropertyEditors/MediaUrlGeneratorCollection.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Models; @@ -6,32 +7,28 @@ namespace Umbraco.Cms.Core.PropertyEditors { public class MediaUrlGeneratorCollection : BuilderCollectionBase { - public MediaUrlGeneratorCollection(System.Func> items) : base(items) - { - } + public MediaUrlGeneratorCollection(Func> items) + : base(items) + { } public bool TryGetMediaPath(string propertyEditorAlias, object value, out string mediaPath) { // We can't get a media path from a null value - // The value will be null when uploading a brand new image, since we try to get the "old path" which doesn't exist yet. - if (value is null) + // The value will be null when uploading a brand new image, since we try to get the "old path" which doesn't exist yet + if (value is not null) { - mediaPath = null; - return false; - } - - foreach(IMediaUrlGenerator generator in this) - { - if (generator.TryGetMediaPath(propertyEditorAlias, value, out var mp)) + foreach (IMediaUrlGenerator generator in this) { - mediaPath = mp; - return true; + if (generator.TryGetMediaPath(propertyEditorAlias, value, out var generatorMediaPath)) + { + mediaPath = generatorMediaPath; + return true; + } } } + mediaPath = null; return false; } - - } } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs index 6cfc248827..4b26f8eefa 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Events; @@ -12,9 +11,7 @@ using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Media; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Notifications; -using Umbraco.Cms.Core.Serialization; using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Core.Strings; using Umbraco.Extensions; namespace Umbraco.Cms.Core.PropertyEditors @@ -71,11 +68,14 @@ namespace Umbraco.Cms.Core.PropertyEditors public bool TryGetMediaPath(string propertyEditorAlias, object value, out string mediaPath) { - if (propertyEditorAlias == Alias) + if (propertyEditorAlias == Alias && + value?.ToString() is var mediaPathValue && + !string.IsNullOrWhiteSpace(mediaPathValue)) { - mediaPath = value?.ToString(); + mediaPath = mediaPathValue; return true; } + mediaPath = null; return false; } @@ -84,11 +84,10 @@ namespace Umbraco.Cms.Core.PropertyEditors /// Gets a value indicating whether a property is an upload field. /// /// The property. - /// A value indicating whether a property is an upload field, and (optionally) has a non-empty value. - private static bool IsUploadField(IProperty property) - { - return property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField; - } + /// + /// true if the specified property is an upload field; otherwise, false. + /// + private static bool IsUploadField(IProperty property) => property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField; /// /// The paths to all file upload property files contained within a collection of content entities diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs index f21a18401c..b33e4fd6d8 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs @@ -14,9 +14,7 @@ using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Media; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Notifications; -using Umbraco.Cms.Core.Serialization; using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Core.Strings; using Umbraco.Extensions; namespace Umbraco.Cms.Core.PropertyEditors @@ -70,11 +68,14 @@ namespace Umbraco.Cms.Core.PropertyEditors public bool TryGetMediaPath(string propertyEditorAlias, object value, out string mediaPath) { - if (propertyEditorAlias == Alias) + if (propertyEditorAlias == Alias && + GetFileSrcFromPropertyValue(value, out _, false) is var mediaPathValue && + !string.IsNullOrWhiteSpace(mediaPathValue)) { - mediaPath = GetFileSrcFromPropertyValue(value, out _, false); + mediaPath = mediaPathValue; return true; } + mediaPath = null; return false; } @@ -95,11 +96,10 @@ namespace Umbraco.Cms.Core.PropertyEditors /// Gets a value indicating whether a property is an image cropper field. /// /// The property. - /// A value indicating whether a property is an image cropper field, and (optionally) has a non-empty value. - private static bool IsCropperField(IProperty property) - { - return property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.ImageCropper; - } + /// + /// true if the specified property is an image cropper field; otherwise, false. + /// + private static bool IsCropperField(IProperty property) => property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.ImageCropper; /// /// Parses the property value into a json object. From bd8d7b91d62fc035dc6f01fedae166e6aa9e1d4c Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Wed, 8 Sep 2021 12:07:05 +0200 Subject: [PATCH 2/2] Support plain file path strings as Image Cropper values --- .../ImageCropperPropertyEditor.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs index b33e4fd6d8..95023cecf6 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs @@ -167,10 +167,23 @@ namespace Umbraco.Cms.Core.PropertyEditors { deserializedValue = null; if (propVal == null || !(propVal is string str)) return null; - if (!str.DetectIsJson()) return null; - deserializedValue = GetJObject(str, true); + + if (!str.DetectIsJson()) + { + // Assume the value is a plain string with the file path + deserializedValue = new JObject() + { + { "src", str } + }; + } + else + { + deserializedValue = GetJObject(str, true); + } + if (deserializedValue?["src"] == null) return null; var src = deserializedValue["src"].Value(); + return relative ? _mediaFileManager.FileSystem.GetRelativePath(src) : src; }