using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Web.Models; namespace Umbraco.Web { internal static class ImageCropperBaseExtensions { internal static ImageCropData GetImageCrop(this string json, string id) { var ic = new ImageCropData(); if (json.DetectIsJson()) { try { var imageCropperSettings = JsonConvert.DeserializeObject>(json); ic = imageCropperSettings.First(p => p.Alias == id); } catch { } } return ic; } internal static ImageCropDataSet SerializeToCropDataSet(this string json) { var imageCrops = new ImageCropDataSet(); if (json.DetectIsJson()) { try { imageCrops = JsonConvert.DeserializeObject(json); } catch(Exception ex) { var e = ex; } } return imageCrops; } internal static bool HasPropertyAndValue(this IPublishedContent publishedContent, string propertyAlias) { try { if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) && publishedContent.HasValue(propertyAlias)) { var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { return false; } return true; } } catch (Exception ex) { LogHelper.Warn("The cache unicorn is not happy with node id: " + publishedContent.Id + " - http://issues.umbraco.org/issue/U4-4146"); var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); if (cropsProperty != null && !string.IsNullOrEmpty(cropsProperty.Value.ToString())) { var propertyAliasValue = cropsProperty.Value.ToString(); if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { return false; } return true; } } return false; } internal static bool HasPropertyAndValueAndCrop(this IPublishedContent publishedContent, string propertyAlias, string cropAlias) { try { if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) && publishedContent.HasValue(propertyAlias)) { var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { return false; } var allTheCrops = propertyAliasValue.SerializeToCropDataSet(); if (allTheCrops != null && allTheCrops.Crops.Any()) { var crop = cropAlias != null ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) : allTheCrops.Crops.First(); if (crop != null) { return true; } } return false; } } catch (Exception ex) { LogHelper.Warn("The cache unicorn is not happy with node id: " + publishedContent.Id + " - http://issues.umbraco.org/issue/U4-4146"); var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); if (cropsProperty != null && !string.IsNullOrEmpty(cropsProperty.Value.ToString())) { var propertyAliasValue = cropsProperty.Value.ToString(); if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { return false; } var allTheCrops = propertyAliasValue.SerializeToCropDataSet(); if (allTheCrops != null && allTheCrops.Crops.Any()) { var crop = cropAlias != null ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) : allTheCrops.Crops.First(); if (crop != null) { return true; } } return false; } } return false; } internal static string GetPropertyValueHack(this IPublishedContent publishedContent, string propertyAlias) { string propertyValue = null; try { if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) && publishedContent.HasValue(propertyAlias)) { propertyValue = publishedContent.GetPropertyValue(propertyAlias); } } catch (Exception ex) { var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); if (cropsProperty != null) { propertyValue = cropsProperty.Value.ToString(); } } return propertyValue; } } }