Has anyone attempted to convert a media file type programmatically in EPiServer?
We have a type we modeled for images, ImageFile, and a type that was added via a plugin. We don't use the type added by the plugin but found that images uploaded will, from time to time, be created as the wrong type. I am hoping to figure out how to make the conversion so I can run a scheduled job to batch convert them all then remove the plugin.
So far, I can programmatically make a copy of A as type B then delete A. What I would really like to do is convert A to B for a seamless transition.
First thing I tried is cloning the image as the proper type but that comes up null
var media = _contentRepository.Service.GetDescendents(SiteDefinition.Current.GlobalAssetsRoot)
.Where(i => _contentRepository.Service.Get<IContent>(i) is MediaData);
var imageFileTypeId = new ImageFile().ContentTypeID;
foreach (var img in media)
{
if (img.Get<IContent>() is GcEpiImageFile)
{
count++ // for summary
var item = _contentLoader.Service.Get<GcEpiImageFile>(img.Get<IContent>().ContentLink);
var cloneItem = item.CreateWritableClone() as ImageFile;
if (cloneItem == null)
{
var msg = $"{item.Name}: {item.ContentLink.ID} came up null";
OnStatusChanged(msg);
summaryMessage.AppendLine(msg);
continue;
}
cloneItem.ContentTypeID = imageFileTypeId;
_contentRepository.Service.Save(cloneItem, SaveAction.Publish, AccessLevel.NoAccess);
}
Next thing I tried is cloning it as the base class ImageData which seems to create the object but throws a Object reference not set to an instance of an object exception when saving.
var media = _contentRepository.Service.GetDescendents(SiteDefinition.Current.GlobalAssetsRoot)
.Where(i => _contentRepository.Service.Get<IContent>(i) is MediaData);
var imageFileTypeId = new ImageFile().ContentTypeID;
foreach (var img in media)
{
if (img.Get<IContent>() is GcEpiImageFile)
{
count++ // for summary
var item = _contentLoader.Service.Get<GcEpiImageFile>(img.Get<IContent>().ContentLink);
var cloneItem = item.CreateWritableClone() as ImageData;
if (cloneItem == null)
{
var msg = $"{item.Name}: {item.ContentLink.ID} came up null";
OnStatusChanged(msg);
summaryMessage.AppendLine(msg);
continue;
}
try
{
cloneItem.ContentTypeID = imageFileTypeId;
}
catch (Exception ex)
{
summaryMessage.AppendLine($"exception triggered by id assignment {ex.Message}<br />{ex.InnerException}");
}
try
{
_contentRepository.Service.Save(cloneItem, SaveAction.Publish, AccessLevel.NoAccess);
}
catch (Exception ex)
{
summaryMessage.AppendLine(
$"exception triggered by save {ex.Message}<br />{ex.InnerException}");
}
}
here is my stack trace:
Object reference not set to an instance of an object.
at EPiServer.Validation.Internal.RoutingSegmentValidator.Validate(IContent instance) at
EPiServer.Validation.Internal.ContextValidatorWrapper`2.Validate(Object instance, Object context)
at EPiServer.Validation.Internal.ValidationService.ValidateRecursively(Object instance, Object context, HashSet`1 visitedInstances)
at EPiServer.Validation.Internal.ValidationService.Validate[T](Object instance, T context)
at EPiServer.Core.ContentProvider.Validate(IContent content, ContentSaveValidationContext saveValidationContext)
at EPiServer.Core.Internal.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access) at Web.Business.ScheduledJobs.ImageConversion.Execute()
Any thoughts, help, guidance would be greatly appreciated