Example #1
0
        public async Task <bool> InsertExternalContent(ExternalContent content)
        {
            try
            {
                await exContentData.Insert(content);

                return(true);
            }
            catch
            {
                Debug.WriteLine("InsertExternalContent failed.");

                return(false);
            }
        }
Example #2
0
        private ExternalContent GetContent(string url, out ShortUrlResult result)
        {
            result = new ShortUrlResult();
            ExternalContent externalContent = null;

            try
            {
                externalContent = externalContentService.Retrieve(url);
            }
            catch (WebException e)
            {
                result = new ShortUrlResult(new[] { new RuleViolation("url", e.Message) });
            }

            return(externalContent);
        }
Example #3
0
 public ExternalContentTests()
 {
     externalContent = new ExternalContent("This is a dummy title", "This is a dummy content");
 }
Example #4
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);
        }