Created
December 30, 2021 10:36
-
-
Save ronaldbarendse/cda65cb041c149e943d4be67ed091da0 to your computer and use it in GitHub Desktop.
Umbraco 8 - Prune existing Image Cropper and Media Picker (v3) values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Core; | |
using Umbraco.Core.Composing; | |
using Umbraco.Core.PropertyEditors.ValueConverters; | |
using Umbraco.Core.Services; | |
public class PruneImageCropperPropertyDataComposer : ComponentComposer<PruneImageCropperPropertyDataComponent> | |
{ } | |
public class PruneImageCropperPropertyDataComponent : IComponent | |
{ | |
private readonly IMediaService mediaService; | |
public PruneImageCropperPropertyDataComponent(IMediaService mediaService) | |
{ | |
this.mediaService = mediaService; | |
} | |
public void Initialize() | |
{ | |
for (int pageIndex = 0; ; pageIndex++) | |
{ | |
var medias = mediaService.GetPagedDescendants(-1, pageIndex, 1000, out _).ToList(); | |
if (medias.Count == 0) | |
{ | |
break; | |
} | |
foreach (var media in medias) | |
{ | |
foreach (var property in media.GetPropertiesByEditor(Constants.PropertyEditors.Aliases.ImageCropper)) | |
{ | |
var value = property.GetValue() as string; | |
if (string.IsNullOrEmpty(value) || value.DetectIsJson() == false) | |
{ | |
continue; | |
} | |
var jsonValue = JObject.Parse(value); | |
// Clean up redundant/default values | |
ImageCropperValue.Prune(jsonValue); | |
property.SetValue(jsonValue.ToString(Formatting.None)); | |
} | |
} | |
// Media isn't versioned, so this replaces the existing value | |
mediaService.Save(medias); | |
} | |
} | |
public void Terminate() | |
{ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Core; | |
using Umbraco.Core.Composing; | |
using Umbraco.Core.PropertyEditors.ValueConverters; | |
using Umbraco.Core.Services; | |
public class PruneMediaPicker3PropertyDataComposer : ComponentComposer<PruneMediaPicker3PropertyDataComponent> | |
{ } | |
public class PruneMediaPicker3PropertyDataComponent : IComponent | |
{ | |
private readonly IContentService _contentService; | |
public PruneMediaPicker3PropertyDataComponent(IContentService contentService) | |
{ | |
_contentService = contentService; | |
} | |
public void Initialize() | |
{ | |
for (int pageIndex = 0; ; pageIndex++) | |
{ | |
var contents = _contentService.GetPagedDescendants(-1, pageIndex, 1000, out _).ToList(); | |
if (contents.Count == 0) | |
{ | |
break; | |
} | |
foreach (var content in contents) | |
{ | |
foreach (var property in content.GetPropertiesByEditor(Constants.PropertyEditors.Aliases.MediaPicker3)) | |
{ | |
var value = property.GetValue() as string; | |
if (string.IsNullOrEmpty(value) || value.DetectIsJson() == false) | |
{ | |
continue; | |
} | |
var dtos = JArray.Parse(value); | |
// Clean up redundant/default values | |
foreach (var dto in dtos.Values<JObject>()) | |
{ | |
ImageCropperValue.Prune(dto); | |
} | |
property.SetValue(dtos.ToString(Formatting.None)); | |
} | |
} | |
// This only saves a new version: make sure to publish and cleanup old versions yourself | |
_contentService.Save(contents); | |
} | |
} | |
public void Terminate() | |
{ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment