private static void ParseSourceUrl(string url, RedirectItemRow redirectItemRow) { var sourceUrlRaw = url == null ? null : url.Trim(); var sourceUrl = sourceUrlRaw.ToUri(); if (sourceUrl != null) { redirectItemRow.Url = sourceUrl.AbsolutePath; } }
/// <summary> /// This is where an Excel Row gets parsed into a RedirectItem. The aim here is not to validate but /// to get everything into a nicely typed model. It's not pretty mainly because of old skool /// null checks. /// </summary> /// <param name="row"></param> /// <returns></returns> private RedirectItem Parse(Row row) { var redirectItemRow = new RedirectItemRow(); redirectItemRow.UniqueId = Guid.NewGuid().ToString(); redirectItemRow.RootNodeId = 0; redirectItemRow.IsPermanent = true; redirectItemRow.IsRegex = false; redirectItemRow.ForwardQueryString = true; ParseSourceUrl(row.GetCellByColumnName("A").Value.ToString(), redirectItemRow); var destinationUrlRaw = row.GetCellByColumnName("B").Value == null ? null : row.GetCellByColumnName("B").Value.ToString().Trim(); var destinationUrl = destinationUrlRaw.ToUri(); if (destinationUrl != null) { redirectItemRow.LinkUrl = destinationUrl.AbsolutePath; } RedirectLinkMode linkMode; var linkModeRaw = row.GetCellByColumnName("C").Value == null?RedirectLinkMode.Url.ToString() : row.GetCellByColumnName("C").Value.ToString().Trim(); Enum.TryParse(linkModeRaw, out linkMode); redirectItemRow.LinkMode = linkMode.ToString().ToLower(); if (destinationUrl != null) { var urlContent = contentFinder.Find(destinationUrl.AbsolutePath); if (urlContent != null) { redirectItemRow.LinkMode = RedirectLinkMode.Content.ToString().ToLower(); redirectItemRow.LinkId = urlContent.Id; redirectItemRow.LinkName = urlContent.Name; redirectItemRow.LinkUrl = urlContent.Url; } else { redirectItemRow.LinkUrl = destinationUrl.AbsolutePath; } } var redirectItem = new RedirectItem(redirectItemRow); return(redirectItem); }
/// <summary> /// This is where a CsvRow gets parsed into a RedirectItem. The aim here is not to validate but /// to get everything into a nicely typed model. It's not pretty mainly because of old skool /// null checks. /// </summary> /// <param name="row"></param> /// <returns></returns> private RedirectItem Parse(CsvRow row) { var redirectItemRow = new RedirectItemRow(); redirectItemRow.UniqueId = Guid.NewGuid().ToString(); redirectItemRow.RootNodeId = 0; redirectItemRow.IsPermanent = true; redirectItemRow.IsRegex = false; redirectItemRow.ForwardQueryString = false; redirectItemRow.Created = EssentialsDateTime.CurrentUnixTimestamp; redirectItemRow.Updated = EssentialsDateTime.CurrentUnixTimestamp; var sourceUrlRaw = row.Cells[0] == null ? null : row.Cells[0].Value.Replace("\"", string.Empty).Trim(); var sourceUrl = sourceUrlRaw.ToUri(); if (sourceUrl != null) { var lastSlash = sourceUrl.AbsolutePath.LastIndexOf('/'); var sourceUrlNoTrailingSlash = (lastSlash > 0) ? sourceUrl.AbsolutePath.Substring(0, lastSlash) : sourceUrl.AbsolutePath; redirectItemRow.Url = sourceUrlNoTrailingSlash; redirectItemRow.QueryString = sourceUrl.Query.TrimStart('?'); } var destinationUrlRaw = row.Cells[1] == null ? null : row.Cells[1].Value.Replace("\"", string.Empty).Trim(); var destinationUrl = destinationUrlRaw.ToUri(); if (destinationUrl != null) { redirectItemRow.LinkUrl = destinationUrl.AbsolutePath; } RedirectLinkMode linkMode; var linkModeRaw = row.Cells[2].Value == null?RedirectLinkMode.Url.ToString() : row.Cells[2].Value.Replace("\"", string.Empty).Trim(); Enum.TryParse(linkModeRaw, out linkMode); redirectItemRow.LinkMode = linkMode.ToString().ToLower(); if (destinationUrl != null) { var lastSlash = destinationUrl.AbsolutePath.LastIndexOf('/'); var destUrlNoTrailingSlash = (lastSlash > 0) ? destinationUrl.AbsolutePath.Substring(0, lastSlash) : destinationUrl.AbsolutePath; var urlContent = contentFinder.Find(destUrlNoTrailingSlash); if (urlContent != null) { redirectItemRow.LinkMode = RedirectLinkMode.Content.ToString().ToLower(); redirectItemRow.LinkId = urlContent.Id; redirectItemRow.LinkName = urlContent.Name; redirectItemRow.LinkUrl = urlContent.Url; } else { redirectItemRow.LinkUrl = destinationUrl.AbsolutePath; } } var redirectItem = new RedirectItem(redirectItemRow); return(redirectItem); }