2014-02-17 16:15:39 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2014-02-19 12:16:58 +01:00
|
|
|
|
|
|
|
|
|
|
using Umbraco.Core;
|
2014-02-17 16:15:39 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Web.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class ImageCropperBaseExtensions
|
|
|
|
|
|
{
|
2014-03-20 14:17:00 +11:00
|
|
|
|
|
2014-02-17 16:15:39 +01:00
|
|
|
|
internal static ImageCropData GetImageCrop(this string json, string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ic = new ImageCropData();
|
2014-02-19 12:16:58 +01:00
|
|
|
|
if (json.DetectIsJson())
|
2014-02-17 16:15:39 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var imageCropperSettings = JsonConvert.DeserializeObject<List<ImageCropData>>(json);
|
2014-03-18 19:47:18 +01:00
|
|
|
|
ic = imageCropperSettings.GetCrop(id);
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
2014-03-20 14:17:00 +11:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Error(typeof(ImageCropperBaseExtensions), "Could not parse the json string: " + json, ex);
|
|
|
|
|
|
}
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
return ic;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static ImageCropDataSet SerializeToCropDataSet(this string json)
|
|
|
|
|
|
{
|
|
|
|
|
|
var imageCrops = new ImageCropDataSet();
|
2014-02-19 12:16:58 +01:00
|
|
|
|
if (json.DetectIsJson())
|
2014-02-17 16:15:39 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(json);
|
|
|
|
|
|
}
|
2014-03-20 14:17:00 +11:00
|
|
|
|
catch (Exception ex)
|
2014-02-17 16:15:39 +01:00
|
|
|
|
{
|
2014-03-20 14:17:00 +11:00
|
|
|
|
LogHelper.Error(typeof(ImageCropperBaseExtensions), "Could not parse the json string: " + json, ex);
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageCrops;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-18 19:47:18 +01:00
|
|
|
|
internal static ImageCropData GetCrop(this ImageCropDataSet dataset, string cropAlias)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dataset == null || dataset.Crops == null || !dataset.Crops.Any())
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return dataset.Crops.GetCrop(cropAlias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-20 14:17:00 +11:00
|
|
|
|
internal static ImageCropData GetCrop(this IEnumerable<ImageCropData> dataset, string cropAlias)
|
|
|
|
|
|
{
|
2014-03-18 19:47:18 +01:00
|
|
|
|
if (dataset == null || !dataset.Any())
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(cropAlias))
|
|
|
|
|
|
return dataset.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
return dataset.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
|
|
|
|
|
|
}
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|