Skip to content

Commit

Permalink
Apply changes from #11805 and #11806 to v9
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldbarendse committed Jan 25, 2022
1 parent 76593aa commit eaf5cb5
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 87 deletions.
8 changes: 4 additions & 4 deletions src/Umbraco.Core/Models/PropertyTagsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -75,8 +75,7 @@ private static void AssignTags(this IProperty property, IEnumerable<string> tags
var updatedTags = currentTags.Union(trimmedTags).ToArray();
var updatedValue = updatedTags.Length == 0 ? null : serializer.Serialize(updatedTags);
property.SetValue(updatedValue, culture); // json array
break;
property.SetValue(serializer.Serialize(currentTags.Union(trimmedTags).ToArray()), culture); // json array
break;
}
}
else
Expand All @@ -88,7 +87,8 @@ private static void AssignTags(this IProperty property, IEnumerable<string> tags
break;

case TagsStorageType.Json:
property.SetValue(serializer.Serialize(trimmedTags), culture); // json array
var updatedValue = trimmedTags.Length == 0 ? null : serializer.Serialize(trimmedTags);
property.SetValue(updatedValue, culture); // json array
break;
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/Umbraco.Core/PropertyEditors/DataValueEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
Expand Down Expand Up @@ -155,15 +155,30 @@ public IEnumerable<ValidationResult> Validate(object value, bool required, strin
/// <returns></returns>
internal Attempt<object> TryConvertValueToCrlType(object value)
{
// if (value is JValue)
// value = value.ToString();

//this is a custom check to avoid any errors, if it's a string and it's empty just make it null
// this is a custom check to avoid any errors, if it's a string and it's empty just make it null
if (value is string s && string.IsNullOrWhiteSpace(s))
{
value = null;
}

if (value is not null && ValueType.InvariantEquals(ValueTypes.Json))
{
// Serialize value to string
var jsonValue = _jsonSerializer.Serialize(value);

// if it's json but it's empty json, then return null
if (jsonValue.DetectIsEmptyJson())
{
value = null;
}
else
{
value = jsonValue;
}
}

// convert the string to a known type
Type valueType;
//convert the string to a known type
switch (ValueTypes.ToStorageType(ValueType))
{
case ValueStorageType.Ntext:
Expand Down Expand Up @@ -221,12 +236,6 @@ internal Attempt<object> TryConvertValueToCrlType(object value)
/// </remarks>
public virtual object FromEditor(ContentPropertyData editorValue, object currentValue)
{
//if it's json but it's empty json, then return null
if (ValueType.InvariantEquals(ValueTypes.Json) && editorValue.Value != null && editorValue.Value.ToString().DetectIsEmptyJson())
{
return null;
}

var result = TryConvertValueToCrlType(editorValue.Value);
if (result.Success == false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public void Handle(ContentCopiedNotification notification)
{
continue;
}

var sourcePath = _mediaFileManager.FileSystem.GetRelativePath(src);
var copyPath = _mediaFileManager.CopyFile(notification.Copy, property.PropertyType, sourcePath);
jo["src"] = _mediaFileManager.FileSystem.GetUrl(copyPath);
Expand Down Expand Up @@ -273,10 +274,8 @@ private void AutoFillProperties(IContentBase model)
// the property value will be the file source eg '/media/23454/hello.jpg' and we
// are fixing that anomaly here - does not make any sense at all but... bah...
src = svalue;
property.SetValue(JsonConvert.SerializeObject(new
{
src = svalue
}, Formatting.None), pvalue.Culture, pvalue.Segment);

property.SetValue(JsonConvert.SerializeObject(new { src = svalue }, Formatting.None), pvalue.Culture, pvalue.Segment);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,42 @@ public override object ToEditor(IProperty property, string culture = null, strin
/// </remarks>
public override object FromEditor(ContentPropertyData editorValue, object currentValue)
{
// get the current path
// Get the current path
var currentPath = string.Empty;
try
{
var svalue = currentValue as string;
var currentJson = string.IsNullOrWhiteSpace(svalue) ? null : JObject.Parse(svalue);
if (currentJson != null && currentJson["src"] != null)
currentPath = currentJson["src"].Value<string>();
if (currentJson != null && currentJson.TryGetValue("src", out var src))
{
currentPath = src.Value<string>();
}
}
catch (Exception ex)
{
// for some reason the value is invalid so continue as if there was no value there
// For some reason the value is invalid so continue as if there was no value there
_logger.LogWarning(ex, "Could not parse current db value to a JObject.");
}

if (string.IsNullOrWhiteSpace(currentPath) == false)
currentPath = _mediaFileManager.FileSystem.GetRelativePath(currentPath);

// get the new json and path
JObject editorJson = null;
// Get the new JSON and file path
var editorFile = string.Empty;
if (editorValue.Value != null)
if (editorValue.Value is JObject editorJson)
{
editorJson = editorValue.Value as JObject;
if (editorJson != null && editorJson["src"] != null)
// Populate current file
if (editorJson["src"] != null)
{
editorFile = editorJson["src"].Value<string>();
}

// Clean up redundant/default data
ImageCropperValue.Prune(editorJson);
}
else
{
editorJson = null;
}

// ensure we have the required guids
Expand Down Expand Up @@ -138,7 +149,7 @@ public override object FromEditor(ContentPropertyData editorValue, object curren
return null; // clear
}

return editorJson?.ToString(); // unchanged
return editorJson?.ToString(Formatting.None); // unchanged
}

// process the file
Expand All @@ -159,7 +170,7 @@ public override object FromEditor(ContentPropertyData editorValue, object curren
// update json and return
if (editorJson == null) return null;
editorJson["src"] = filepath == null ? string.Empty : _mediaFileManager.FileSystem.GetUrl(filepath);
return editorJson.ToString();
return editorJson.ToString(Formatting.None);
}

private string ProcessFile(ContentPropertyFile file, Guid cuid, Guid puid)
Expand All @@ -186,7 +197,6 @@ private string ProcessFile(ContentPropertyFile file, Guid cuid, Guid puid)
return filepath;
}


public override string ConvertDbToString(IPropertyType propertyType, object value)
{
if (value == null || string.IsNullOrEmpty(value.ToString()))
Expand All @@ -205,7 +215,7 @@ public override string ConvertDbToString(IPropertyType propertyType, object val
{
src = val,
crops = crops
},new JsonSerializerSettings()
}, new JsonSerializerSettings()
{
Formatting = Formatting.None,
NullValueHandling = NullValueHandling.Ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.IO;
Expand Down Expand Up @@ -157,7 +157,6 @@ internal static IEnumerable<MediaWithCropsDto> Deserialize(IJsonSerializer jsonS
}
}


/// <summary>
/// Model/DTO that represents the JSON that the MediaPicker3 stores.
/// </summary>
Expand All @@ -176,7 +175,6 @@ internal class MediaWithCropsDto
[DataMember(Name = "focalPoint")]
public ImageCropperValue.ImageCropperFocalPoint FocalPoint { get; set; }


/// <summary>
/// Applies the configuration to ensure only valid crops are kept and have the correct width/height.
/// </summary>
Expand Down Expand Up @@ -214,9 +212,6 @@ public void ApplyConfiguration(MediaPicker3Configuration configuration)
/// Removes redundant crop data/default focal point.
/// </summary>
/// <param name="value">The media with crops DTO.</param>
/// <returns>
/// The cleaned up value.
/// </returns>
/// <remarks>
/// Because the DTO uses the same JSON keys as the image cropper value for crops and focal point, we can re-use the prune method.
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override object ToEditor(IProperty property, string culture = null, strin

try
{
var links = JsonConvert.DeserializeObject<List<MultiUrlPickerValueEditor.LinkDto>>(value);
var links = JsonConvert.DeserializeObject<List<LinkDto>>(value);

var documentLinks = links.FindAll(link => link.Udi != null && link.Udi.EntityType == Constants.UdiEntityType.Document);
var mediaLinks = links.FindAll(link => link.Udi != null && link.Udi.EntityType == Constants.UdiEntityType.Media);
Expand Down Expand Up @@ -158,11 +158,13 @@ public override object FromEditor(ContentPropertyData editorValue, object curren
{
var links = JsonConvert.DeserializeObject<List<LinkDisplay>>(value);
if (links.Count == 0)
{
return null;
}

return JsonConvert.SerializeObject(
from link in links
select new MultiUrlPickerValueEditor.LinkDto
select new LinkDto
{
Name = link.Name,
QueryString = link.QueryString,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Umbraco.Cms.Core.Exceptions;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
Expand Down Expand Up @@ -65,7 +65,6 @@ public override object FromEditor(ContentPropertyData editorValue, object curren
}

var values = json.Select(item => item.Value<string>()).ToArray();

if (values.Length == 0)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
Expand Down Expand Up @@ -105,7 +105,9 @@ public override string ConvertDbToString(IPropertyType propertyType, object prop
var rows = _nestedContentValues.GetPropertyValues(propertyValue);

if (rows.Count == 0)
{
return null;
}

foreach (var row in rows.ToList())
{
Expand Down Expand Up @@ -141,8 +143,6 @@ public override string ConvertDbToString(IPropertyType propertyType, object prop

#endregion



#region Convert database // editor

// note: there is NO variant support here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
Expand Down Expand Up @@ -145,7 +145,9 @@ public override object ToEditor(IProperty property, string culture = null, strin
public override object FromEditor(ContentPropertyData editorValue, object currentValue)
{
if (editorValue.Value == null)
{
return null;
}

var userId = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Id ?? Constants.Security.SuperUserId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.

using System;
Expand Down
Loading

0 comments on commit eaf5cb5

Please sign in to comment.