Beispiel #1
0
        public ActionResult AddRedirect([FromBody] JObject m)
        {
            AddRedirectOptions model = m.ToObject <AddRedirectOptions>();

            try {
                // Some input validation
                if (model == null)
                {
                    throw new RedirectsException("Failed parsing request body.");
                }
                if (string.IsNullOrWhiteSpace(model.OriginalUrl))
                {
                    throw new RedirectsException(_backOffice.Localize("errorNoUrl"));
                }
                if (string.IsNullOrWhiteSpace(model.Destination?.Url))
                {
                    throw new RedirectsException(_backOffice.Localize("errorNoDestination"));
                }

                // Add the redirect
                IRedirect redirect = _redirects.AddRedirect(model);

                // Map the result for the API
                return(new JsonResult(_backOffice.Map(redirect)));
            } catch (RedirectsException ex) {
                if (!ex.Is404)
                {
                    _logger.LogError(ex, ex.Message);
                }

                // Generate the error response
                return(Error(ex));
            }
        }
Beispiel #2
0
        public RedirectItem AddRedirect(AddRedirectOptions model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            string url   = model.OriginalUrl;
            string query = string.Empty;

            if (model.IsRegex == false)
            {
                string[] urlParts = url.Split('?');
                url   = urlParts[0].TrimEnd('/');
                query = urlParts.Length == 2 ? urlParts[1] : string.Empty;
            }

            if (GetRedirectByUrl(model.RootNodeId, url, query) != null)
            {
                throw new RedirectsException("A redirect with the specified URL already exists.");
            }

            // Initialize the new redirect and populate the properties
            RedirectItem item = new RedirectItem {
                RootId             = model.RootNodeId,
                RootKey            = model.RootNodeKey,
                LinkId             = model.Destination.Id,
                LinkKey            = model.Destination.Key,
                LinkUrl            = model.Destination.Url,
                LinkMode           = model.Destination.Type,
                Url                = url,
                QueryString        = query,
                Created            = EssentialsTime.UtcNow,
                Updated            = EssentialsTime.UtcNow,
                IsPermanent        = model.IsPermanent,
                IsRegex            = model.IsRegex,
                ForwardQueryString = model.ForwardQueryString
            };

            // Attempt to add the redirect to the database
            using (IScope scope = _scopeProvider.CreateScope()) {
                try {
                    scope.Database.Insert(item.Dto);
                } catch (Exception ex) {
                    _logger.Error <RedirectsService>("Unable to insert redirect into the database", ex);
                    throw new Exception("Unable to insert redirect into the database", ex);
                }
                scope.Complete();
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
Beispiel #3
0
        private static void ParseSourceUrl(string url, AddRedirectOptions redirectOptions)
        {
            var sourceUrlRaw = url == null
                ? null
                : url.Trim();

            var sourceUrl = sourceUrlRaw.ToUri();

            if (sourceUrl != null)
            {
                redirectOptions.OriginalUrl = sourceUrl.AbsolutePath;
            }
        }
Beispiel #4
0
        /// <inheritdoc />
        public IRedirect AddRedirect(AddRedirectOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            string url   = options.OriginalUrl;
            string query = string.Empty;

            if (GetRedirectByPathAndQuery(options.RootNodeKey, url, query) != null)
            {
                throw new RedirectsException("A redirect with the specified URL already exists.");
            }

            // Initialize the destination
            RedirectDestination destination = new RedirectDestination {
                Id       = options.Destination.Id,
                Key      = options.Destination.Key,
                Name     = options.Destination.Name,
                Url      = options.Destination.Url,
                Query    = options.Destination.Query ?? string.Empty,
                Fragment = options.Destination.Fragment ?? string.Empty,
                Type     = options.Destination.Type
            };

            // Initialize the new redirect and populate the properties
            Redirect item = new Redirect {
                RootKey            = options.RootNodeKey,
                Url                = url,
                QueryString        = query,
                CreateDate         = EssentialsTime.UtcNow,
                UpdateDate         = EssentialsTime.UtcNow,
                IsPermanent        = options.IsPermanent,
                ForwardQueryString = options.ForwardQueryString
            }.SetDestination(destination);

            // Attempt to add the redirect to the database
            using (IScope scope = _scopeProvider.CreateScope()) {
                try {
                    scope.Database.Insert(item.Dto);
                } catch (Exception ex) {
                    //_logger.Error<RedirectsService>("Unable to insert redirect into the database", ex);
                    throw new RedirectsException("Unable to insert redirect into the database.", ex);
                }
                scope.Complete();
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
Beispiel #5
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);
        }