Merge pull request #11050 from umbraco/v9/bugfix/fix-imagecropper-trygetmediapath

Fix Image Cropper media path parsing
This commit is contained in:
Bjarke Berg
2021-09-08 21:03:46 +02:00
committed by GitHub
3 changed files with 46 additions and 37 deletions

View File

@@ -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<IMediaUrlGenerator>
{
public MediaUrlGeneratorCollection(System.Func<IEnumerable<IMediaUrlGenerator>> items) : base(items)
{
}
public MediaUrlGeneratorCollection(Func<IEnumerable<IMediaUrlGenerator>> 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;
}
}
}

View File

@@ -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.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>A value indicating whether a property is an upload field, and (optionally) has a non-empty value.</returns>
private static bool IsUploadField(IProperty property)
{
return property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField;
}
/// <returns>
/// <c>true</c> if the specified property is an upload field; otherwise, <c>false</c>.
/// </returns>
private static bool IsUploadField(IProperty property) => property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField;
/// <summary>
/// The paths to all file upload property files contained within a collection of content entities

View File

@@ -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.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>A value indicating whether a property is an image cropper field, and (optionally) has a non-empty value.</returns>
private static bool IsCropperField(IProperty property)
{
return property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.ImageCropper;
}
/// <returns>
/// <c>true</c> if the specified property is an image cropper field; otherwise, <c>false</c>.
/// </returns>
private static bool IsCropperField(IProperty property) => property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.ImageCropper;
/// <summary>
/// Parses the property value into a json object.
@@ -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<string>();
return relative ? _mediaFileManager.FileSystem.GetRelativePath(src) : src;
}