internal void Import(RedirectImportItem redirect, CsvImportOptions options) { // TODO: Use dependency injection var service = Current.Factory.GetInstance <IRedirectsService>(); // TODO: Check whether redirect exists try { service.AddRedirect(redirect.AddOptions); redirect.Status = RedirectImportStatus.Success; } catch (Exception ex) { redirect.Errors.Add(ex.Message); redirect.Status = RedirectImportStatus.Failed; } }
private List <RedirectImportItem> ParseCsvRows(CsvInternalImportOptions options) { List <RedirectImportItem> redirects = new List <RedirectImportItem>(); foreach (CsvRow row in options.File.Rows) { RedirectImportItem item = new RedirectImportItem { AddOptions = new AddRedirectOptions() }; string valueRootNodeId = row.GetCellValue(options.ColumnRootNodeId.Index); if (int.TryParse(valueRootNodeId, out int rootNodeId)) { item.AddOptions.RootNodeId = rootNodeId; IPublishedContent content = Current.UmbracoContext.Content.GetById(rootNodeId); if (content != null) { item.AddOptions.RootNodeKey = content.Key; } } else { // TODO: Should we validate the domain? Any security concerns about using the input value? IDomain domain = Current.Services.DomainService.GetByName(valueRootNodeId); if (domain == null) { item.Errors.Add("Unknown root node ID or domain: " + valueRootNodeId); } else if (domain.RootContentId == null) { item.Errors.Add("Domain doesn't have a root node ID: " + valueRootNodeId); } else { item.AddOptions.RootNodeId = domain.RootContentId.Value; IPublishedContent content = Current.UmbracoContext.Content.GetById(domain.RootContentId.Value); if (content != null) { item.AddOptions.RootNodeKey = content.Key; } } } string valueInboundUrl = row.GetCellValue(options.ColumnInboundUrl.Index); try { // TODO: Should we validate the domain? Any security concerns about using the input value? string testUrl = "http://hest.dk" + valueInboundUrl; Uri uri = new Uri(testUrl); item.AddOptions.OriginalUrl = valueInboundUrl; } catch (Exception) { item.Errors.Add("Invalid inbound URL specified: " + valueInboundUrl); } string valueDestinationId = row.GetCellValue(options.ColumnDestinationId.Index); if (!int.TryParse(valueDestinationId, out int destinationId)) { item.Errors.Add("Invalid destination ID: " + valueDestinationId); } string destinationUrl = row.GetCellValue(options.ColumnDestinationUrl.Index); if (string.IsNullOrWhiteSpace(destinationUrl)) { item.Errors.Add("Invalid destination URL: " + destinationUrl); } string valueLinkMode = row.GetCellValue(options.ColumnDestinationType.Index); if (!EnumUtils.TryParseEnum(valueLinkMode, out RedirectDestinationType destinationMode)) { item.Errors.Add("Invalid destination type: " + valueLinkMode); } string destinatioName = ""; Guid destinationKey = Guid.Empty; if (destinationMode == RedirectDestinationType.Content) { IPublishedContent content = Current.UmbracoContext.Content.GetById(destinationId); if (content != null) { destinatioName = content.Name; destinationKey = content.Key; destinationUrl = content.Url; } } else if (destinationMode == RedirectDestinationType.Media) { IPublishedContent media = Current.UmbracoContext.Media.GetById(destinationId); if (media != null) { destinatioName = media.Name; destinationKey = media.Key; destinationUrl = media.Url; } } item.AddOptions.Destination = new RedirectDestination(destinationId, destinationKey, destinationUrl, destinationMode) /*Name = destinatioName*/ } { ; //item.AddOptions.Overwrite = options.Options.OverwriteExisting; redirects.Add(item); } return(redirects); }