Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlink and removal of missing commerce media + logging + performance … #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/EpiserverImporter/MediaImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -175,13 +175,13 @@ protected ContentReference GetFolder(FileInfo fileInfo, ContentType contentType)
});
}

private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable<EntryCode> codes)
private void AddLinksFromMediaToCodes(MediaData contentMedia, IEnumerable<EntryCode> 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))
Expand All @@ -196,10 +196,17 @@ private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable<EntryCo
continue;
}

CommerceMedia existingMedia = writableContent.CommerceMediaCollection.FirstOrDefault(x => 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}");
Expand All @@ -213,7 +220,15 @@ private void AddLinksFromMediaToCodes(IContent contentMedia, IEnumerable<EntryCo
writableContent.CommerceMediaCollection.Add(media);
}

_contentRepository.Save((IContent)writableContent, SaveAction.Publish, AccessLevel.NoAccess);
try
{
_contentRepository.Save((IContent)writableContent, SaveAction.Publish | SaveAction.SkipValidation, AccessLevel.NoAccess);
}
catch (Exception e)
{
_logger.Error($"Something went wrong connecting assets code: {entryCode.Code} cr: {contentReference.ID}, media: {media.AssetLink.ID} {contentMedia.Name}. Continuing... ", e);
continue;
}
}
}

Expand Down Expand Up @@ -277,7 +292,7 @@ private MediaData CreateNewFile(InRiverImportResource inriverResource)

_logger.Debug($"New mediadata is ready to be saved: {newFile.Name}, from path {inriverResource.Path}");

ContentReference contentReference = _contentRepository.Save(newFile, SaveAction.Publish, AccessLevel.NoAccess);
ContentReference contentReference = _contentRepository.Save(newFile, SaveAction.Publish | SaveAction.SkipValidation, AccessLevel.NoAccess);
var mediaData = _contentRepository.Get<MediaData>(contentReference);

_logger.Debug($"Saved file {fileInfo.Name} with Content ID {contentReference?.ID}.");
Expand Down Expand Up @@ -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<InRiverImportResource> 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;
Expand Down Expand Up @@ -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)
Expand Down