private void ImportImageAndAttachToEntry(InRiverImportResource inriverResource) { Guid mediaGuid = EpiserverEntryIdentifier.EntityIdToGuid(inriverResource.ResourceId); if (_contentRepository.TryGet(mediaGuid, out MediaData existingMediaData)) { _logger.Debug($"Found existing resource with Resource ID: {inriverResource.ResourceId}"); // ReSharper disable once SuspiciousTypeConversion.Global UpdateMetaData((IInRiverResource)existingMediaData, inriverResource); if (inriverResource.Action == ImporterActions.Added) { AddLinksFromMediaToCodes(existingMediaData, inriverResource.EntryCodes); } } else { existingMediaData = CreateNewFile(inriverResource); if (existingMediaData == null) { return; } AddLinksFromMediaToCodes(existingMediaData, inriverResource.EntryCodes); } }
private void DeleteResourceLink(Entity removedResource, Entity removalTarget) { Guid resourceGuid = EpiserverEntryIdentifier.EntityIdToGuid(removedResource.Id); string targetCode = _catalogCodeGenerator.GetEpiserverCode(removalTarget); _epiApi.DeleteLink(resourceGuid, targetCode); }
private async Task DeleteAsync(Entity channelEntity, Entity deletedEntity) { switch (deletedEntity.EntityType.Id) { case "Resource": Guid resourceGuid = EpiserverEntryIdentifier.EntityIdToGuid(deletedEntity.Id); await _epiApi.DeleteResourceAsync(resourceGuid); break; case "Channel": await _epiApi.DeleteCatalogAsync(deletedEntity.Id); break; case "ChannelNode": await _epiApi.DeleteCatalogNodeAsync(deletedEntity, channelEntity.Id); break; case "Item": if ((_config.ItemsToSkus && _config.UseThreeLevelsInCommerce) || !_config.ItemsToSkus) { await _epiApi.DeleteCatalogEntryAsync(deletedEntity); } if (_config.ItemsToSkus) { var entitiesToDelete = new List <string>(); List <XElement> skuElements = _catalogElementFactory.GenerateSkuItemElemetsFromItem(deletedEntity); foreach (XElement sku in skuElements) { XElement skuCodElement = sku.Element("Code"); if (skuCodElement != null) { entitiesToDelete.Add(skuCodElement.Value); } } await _epiApi.DeleteSkusAsync(entitiesToDelete); } break; default: await _epiApi.DeleteCatalogEntryAsync(deletedEntity); break; } }
private void HandleDelete(InRiverImportResource inriverResource) { var existingMediaData = _contentRepository.Get <MediaData>(EpiserverEntryIdentifier.EntityIdToGuid(inriverResource.ResourceId)); List <IDeleteActionsHandler> importerHandlers = ServiceLocator.Current.GetAllInstances <IDeleteActionsHandler>().ToList(); if (_config.RunDeleteActionsHandlers) { foreach (IDeleteActionsHandler handler in importerHandlers) { handler.PreDeleteResource(inriverResource); } } _contentRepository.Delete(existingMediaData.ContentLink, true, AccessLevel.NoAccess); if (_config.RunDeleteActionsHandlers) { foreach (IDeleteActionsHandler handler in importerHandlers) { handler.PostDeleteResource(inriverResource); } } }
private void HandleUnlink(InRiverImportResource inriverResource) { var existingMediaData = _contentRepository.Get <MediaData>(EpiserverEntryIdentifier.EntityIdToGuid(inriverResource.ResourceId)); DeleteLinksBetweenMediaAndCodes(existingMediaData, inriverResource.Codes); }
private MediaData CreateNewFile(InRiverImportResource inriverResource) { ResourceMetaField resourceFileId = inriverResource.MetaFields.FirstOrDefault(m => m.Id == "ResourceFileId"); if (String.IsNullOrEmpty(resourceFileId?.Values.FirstOrDefault()?.Data)) { _logger.Debug("ResourceFileId is null, won't do stuff."); return(null); } _logger.Debug($"Attempting to create and import file from path: {inriverResource.Path}"); var fileInfo = new FileInfo(inriverResource.Path); IEnumerable <Type> mediaTypes = _contentMediaResolver.ListAllMatching(fileInfo.Extension).ToList(); _logger.Debug($"Found {mediaTypes.Count()} matching media types for extension {fileInfo.Extension}."); Type contentTypeType = mediaTypes.FirstOrDefault(x => x.GetInterfaces().Contains(typeof(IInRiverResource))) ?? _contentMediaResolver.GetFirstMatching(fileInfo.Extension); if (contentTypeType == null) { _logger.Warning($"Can't find suitable content type when trying to import {inriverResource.Path}"); } else { _logger.Debug($"Chosen content type-type is {contentTypeType.Name}."); } ContentType contentType = _contentTypeRepository.Load(contentTypeType); var newFile = _contentRepository.GetDefault <MediaData>(GetFolder(fileInfo, contentType), contentType.ID); newFile.Name = fileInfo.Name; newFile.ContentGuid = EpiserverEntryIdentifier.EntityIdToGuid(inriverResource.ResourceId); // ReSharper disable once SuspiciousTypeConversion.Global if (newFile is IInRiverResource resource) { resource.ResourceFileId = Int32.Parse(resourceFileId.Values.First().Data); resource.EntityId = inriverResource.ResourceId; try { resource.HandleMetaData(inriverResource.MetaFields); } catch (Exception exception) { _logger.Error($"Error when running HandleMetaData for resource {inriverResource.ResourceId} with contentType {contentType.Name}: {exception.Message}"); } } Blob blob = _blobFactory.CreateBlob(newFile.BinaryDataContainer, fileInfo.Extension); using (Stream stream = blob.OpenWrite()) { FileStream fileStream = File.OpenRead(fileInfo.FullName); fileStream.CopyTo(stream); fileStream.Dispose(); } newFile.BinaryData = blob; _logger.Debug($"New mediadata is ready to be saved: {newFile.Name}, from path {inriverResource.Path}"); ContentReference contentReference = _contentRepository.Save(newFile, SaveAction.Publish, AccessLevel.NoAccess); var mediaData = _contentRepository.Get <MediaData>(contentReference); _logger.Debug($"Saved file {fileInfo.Name} with Content ID {contentReference?.ID}."); return(mediaData); }