From 1d61d9193ff0ff1db512da75367ee3b8b0d4eb0e Mon Sep 17 00:00:00 2001 From: Luc Gosso Date: Wed, 4 Nov 2020 08:42:27 +0100 Subject: [PATCH] Unlink and removal of missing commerce media + logging + performance with skipvalidation --- src/EpiserverImporter/MediaImporter.cs | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/EpiserverImporter/MediaImporter.cs b/src/EpiserverImporter/MediaImporter.cs index 9491fc9..4db6281 100644 --- a/src/EpiserverImporter/MediaImporter.cs +++ b/src/EpiserverImporter/MediaImporter.cs @@ -134,7 +134,7 @@ public void ImportResources(ImportResourcesRequest request) catch (Exception ex) { errors++; - _logger.Error("Importing resource failed:", ex); + _logger.Error($"Importing resource failed: action: {resource.Action} - id: {resource.ResourceId} - path: {resource.Path} - ", ex); } }); @@ -175,13 +175,13 @@ protected ContentReference GetFolder(FileInfo fileInfo, ContentType contentType) }); } - private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable codes) + private void AddLinksFromMediaToCodes(MediaData contentMedia, IEnumerable codes) { var media = new CommerceMedia { AssetLink = contentMedia.ContentLink, GroupName = "default", AssetType = "episerver.core.icontentmedia" }; foreach (EntryCode entryCode in codes) { - ContentReference contentReference = _referenceConverter.GetContentLink(entryCode.Code); + var contentReference = _referenceConverter.GetContentLink(entryCode.Code); IAssetContainer writableContent = null; if (_contentRepository.TryGet(contentReference, out EntryContentBase entry)) @@ -196,10 +196,17 @@ private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable x.AssetLink.Equals(media.AssetLink)); + var existingMedia = writableContent.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink.Equals(media.AssetLink)); if (existingMedia != null) writableContent.CommerceMediaCollection.Remove(existingMedia); + //if image been delete the media is linked with status "Media not found" in UI - then need to be removed, else it will throw System.ComponentModel.DataAnnotations.ValidationException: Media is not found. Navigate to Assets tab and remove it in order to publish. at EPiServer.Core.ContentProvider.ThrowValidationException(ICollection`1 errors) at EPiServer.Core.Internal.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access) + var unlinkedAssets = writableContent.CommerceMediaCollection.Where(x => ContentReference.IsNullOrEmpty(x.AssetLink)).ToList(); + foreach (var asset in unlinkedAssets) + { + writableContent.CommerceMediaCollection.Remove(asset); + } + if (entryCode.IsMainPicture) { _logger.Debug($"Setting '{contentMedia.Name}' as main media on {entryCode.Code}"); @@ -213,7 +220,15 @@ private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable(contentReference); _logger.Debug($"Saved file {fileInfo.Name} with Content ID {contentReference?.ID}."); @@ -317,12 +332,12 @@ private void DeleteMediaLink(MediaData media, string code) return; writableContent.CommerceMediaCollection.Remove(mediaToRemove); - _contentRepository.Save((IContent)writableContent, SaveAction.Publish, AccessLevel.NoAccess); + _contentRepository.Save((IContent)writableContent, SaveAction.Publish | SaveAction.SkipValidation, AccessLevel.NoAccess); } private List DeserializeRequest(ImportResourcesRequest request) { - _logger.Debug($"Deserializing and preparing {request.ResourceXmlPath} for import."); + _logger.Debug($"Deserializing and preparing {request.ResourceXmlPath} for import. basepath is: {request.BasePath}"); var serializer = new XmlSerializer(typeof(Resources)); Resources resources; @@ -503,6 +518,7 @@ private void ImportResource(InRiverImportResource resource) else if (resource.Action == ImporterActions.Deleted) { _logger.Debug($"Got delete action for resource id: {resource.ResourceId}."); + HandleUnlink(resource);//always unlink before delete HandleDelete(resource); } else if (resource.Action == ImporterActions.Unlinked)