Beispiel #1
0
        private ShortUrlResult Create(string url, string aliasName, string ipAddress, string userName, string apiKey, bool useUserName)
        {
            if (!string.IsNullOrWhiteSpace(url))
            {
                // add the protocol if missing
                if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
                    !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                {
                    url = "http://" + url;
                }
            }

            ShortUrlResult result = Validation.Validate <ShortUrlResult>(() => string.IsNullOrWhiteSpace(url), "url", TextMessages.UrlCannotBeBlank)
                                    .Or(() => !url.IsWebUrl(), "url", TextMessages.UrlIsNotInValidFormat)
                                    .Or(() => url.StartsWith(urlResolver.ApplicationRoot, StringComparison.OrdinalIgnoreCase), "url", TextMessages.CannotShrinkUrlForItsOwnDomain)
                                    .Or(() => bannedDomainRepository.IsMatching(url), "url", TextMessages.CannotShrinkUrlWhichMatchesWithOurBannedDomains)
                                    .And(() => !string.IsNullOrWhiteSpace(aliasName) && !baseX.IsValid(aliasName), "alias", TextMessages.AliasIsNotValidAliasCanOnlyContainAlphanumericCharacters)
                                    .Or(() => !string.IsNullOrWhiteSpace(aliasName) && reservedAliasRepository.IsMatching(aliasName), "alias", TextMessages.SpecifiedAliasMatchesWithOurReservedAlias.FormatWith(aliasName))
                                    .And(() => string.IsNullOrWhiteSpace(ipAddress), "ipAddress", TextMessages.IpAddressCannotBeBlank)
                                    .Or(() => !ipAddress.IsIPAddress(), "ipAddress", TextMessages.IpAddressIsNotInValidFormat)
                                    .Result();

            if (result.RuleViolations.IsEmpty())
            {
                User user;

                result = useUserName ?
                         ValidateUserName <ShortUrlResult>(userName, out user) :
                         ValidateApiKey <ShortUrlResult>(apiKey, out user);

                if (result.RuleViolations.IsEmpty())
                {
                    string   hash     = url.ToLower(Culture.Invariant).Hash();
                    ShortUrl shortUrl = shortUrlRepository.GetByHash(hash);

                    if (shortUrl == null)
                    {
                        // Url does not exist, create it, if url is not valid then we
                        // will have the error in the rule violations.
                        ExternalContent externalContent = GetContent(url, out result);

                        if (result.RuleViolations.IsEmpty())
                        {
                            shortUrl = new ShortUrl {
                                Title = externalContent.Title, Url = url, Domain = new Uri(url, UriKind.Absolute).Host.ToLower(Culture.Current), Hash = hash
                            };

                            thumbnail.Capture(url);

                            shortUrlRepository.Add(shortUrl);
                        }
                    }

                    if (result.RuleViolations.IsEmpty() && (shortUrl != null))
                    {
                        if (string.IsNullOrWhiteSpace(aliasName))
                        {
                            if (shortUrl.Id == 0)
                            {
                                result = CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                            }
                            else
                            {
                                if (user == null)
                                {
                                    // Check for existing alias which does not have an associated user;
                                    Alias alias = shortUrl.Aliases.FirstOrDefault(a => a.User == null);

                                    result = alias != null ?
                                             new ShortUrlResult(CreateShortUrlDTO(alias)) :
                                             CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                                }
                                else
                                {
                                    Alias alias = shortUrl.Aliases.FirstOrDefault(a => (a.User != null) && (a.User.Id == user.Id));

                                    result = alias != null?
                                             CreateWithAlias(shortUrl, alias.Name, ipAddress, user, !useUserName) :
                                                 CreateWithoutAlias(shortUrl, ipAddress, user, !useUserName);
                                }
                            }
                        }
                        else
                        {
                            result = CreateWithAlias(shortUrl, aliasName, ipAddress, user, !useUserName);
                        }

                        if (result.RuleViolations.IsEmpty())
                        {
                            unitOfWork.Commit();
                        }
                    }
                }
            }

            return(result);
        }