Ejemplo n.º 1
0
        /// <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 RedirectItemDto
            {
                Id                 = 0,
                IsPermanent        = true,
                IsRegex            = false,
                ForwardQueryString = false,
                QueryString        = string.Empty,
                Created            = DateTime.Now,
                Updated            = DateTime.Now
            };

            var sourceUrl = row.Cells[0]?.Value.Replace("\"", string.Empty).Trim().ToUri();

            if (sourceUrl != null)
            {
                var lastSlash = sourceUrl.AbsolutePath.LastIndexOf('/');
                var sourceUrlNoTrailingSlash = (lastSlash > 0) ? sourceUrl.AbsolutePath.Substring(0, lastSlash) : sourceUrl.AbsolutePath;

                redirectItemRow.Url = sourceUrlNoTrailingSlash;

                if (!string.IsNullOrEmpty(sourceUrl.Query.TrimStart('?')))
                {
                    redirectItemRow.ForwardQueryString = true;
                    redirectItemRow.QueryString        = sourceUrl.Query.TrimStart('?');
                }
            }

            var destinationUrl = row.Cells[1]?.Value.Replace("\"", string.Empty).Trim().ToUri();

            if (destinationUrl != null)
            {
                var lastSlash = destinationUrl.AbsolutePath.LastIndexOf('/');
                var destUrlNoTrailingSlash = (lastSlash > 0) ? destinationUrl.AbsolutePath.Substring(0, lastSlash) : destinationUrl.AbsolutePath;

                var destinationUrlContent = contentFinder.Find(destUrlNoTrailingSlash);

                if (destinationUrlContent != null)
                {
                    redirectItemRow.DestinationType = RedirectDestinationType.Content.ToString().ToLower();
                    redirectItemRow.DestinationId   = destinationUrlContent.Id;
                    redirectItemRow.DestinationUrl  = destinationUrlContent.Url;
                }
                else
                {
                    var linkModeRaw = row.Cells[2].Value == null?RedirectDestinationType.Url.ToString() : row.Cells[2].Value.Replace("\"", string.Empty).Trim();

                    Enum.TryParse(linkModeRaw, out RedirectDestinationType linkMode);

                    redirectItemRow.DestinationType = linkMode.ToString().ToLower();
                    redirectItemRow.DestinationUrl  = destinationUrl.AbsolutePath;
                }
            }

            var redirectItem = new RedirectItem(redirectItemRow);

            return(redirectItem);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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 redirectOptions = new AddRedirectOptions();

            redirectOptions.RootNodeId         = 0;
            redirectOptions.IsPermanent        = true;
            redirectOptions.IsRegex            = false;
            redirectOptions.ForwardQueryString = true;

            ParseSourceUrl(row.GetCellByColumnName("A").Value.ToString(), redirectOptions);

            var destinationUrlRaw = row.GetCellByColumnName("B").Value == null ? null : row.GetCellByColumnName("B").Value.ToString().Trim();

            var destinationUrl = destinationUrlRaw.ToUri();

            RedirectDestinationType linkMode;
            var linkModeRaw = row.GetCellByColumnName("C").Value == null?RedirectDestinationType.Url.ToString() : row.GetCellByColumnName("C").Value.ToString().Trim();

            Enum.TryParse(linkModeRaw, out linkMode);

            redirectOptions.Destination.Type = linkMode;

            if (destinationUrl != null)
            {
                var urlContent = contentFinder.Find(destinationUrl.AbsolutePath);

                if (urlContent != null)
                {
                    redirectOptions.Destination = new RedirectDestination(urlContent.Id, Guid.Empty, urlContent.Url, linkMode);

                    //redirectOptions. = RedirectDestinationType.Content.ToString().ToLower();
                    //redirectOptions.LinkId = urlContent.Id;
                    //redirectOptions.LinkName = urlContent.Name;
                    //redirectOptions.LinkUrl = urlContent.Url;
                }
                else
                {
                    redirectOptions.Destination = new RedirectDestination(0, Guid.Empty, destinationUrl.AbsolutePath, linkMode);
                }
            }

            var redirectItem = new RedirectItem(redirectOptions);

            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);
        }