Exemplo n.º 1
0
        public async Task <URL> GetByIdAsync(ShortURL id)
        {
            string key   = id.ToString();
            string value = await _redisDB.StringGetAsync(key);

            return(new URL(value, id));
        }
Exemplo n.º 2
0
        public IActionResult OnPost(string inputURL)
        {
            if (inputURL == null)
            {
                ViewData["NullError"] = "URL input cannot be blank.";
                return(Page());
            }
            else if (!inputURL.StartsWith("http"))
            {
                inputURL = "http://" + inputURL;
            }

            string token = GenerateToken.Generate();

            while (urlData.GetShortURLByToken(token) != null)
            {
                token = token = GenerateToken.Generate();
            }

            ShortURL = new ShortURL()
            {
                URL = inputURL, Token = token
            };
            urlData.AddShortURL(ShortURL);

            return(Page());
        }
Exemplo n.º 3
0
        public ActionResult DeleteConfirmed(int id)
        {
            ShortURL shorturl = db.ShortURLs.Find(id);

            db.ShortURLs.Remove(shorturl);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        public void CreateShortURL()
        {
            var su = new ShortURL(_shortURL);

            Assert.Equal(_shortURL, su.SUrl);
            su = ShortURL.ComputeShortURLFromExtendedURL(_url);
            Assert.Equal(_shortURL, su.SUrl);
        }
Exemplo n.º 5
0
        //
        // GET: /ShortURL/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ShortURL shorturl = db.ShortURLs.Find(id);

            if (shorturl == null)
            {
                return(HttpNotFound());
            }
            return(View(shorturl));
        }
Exemplo n.º 6
0
 public ActionResult Edit(ShortURL shorturl)
 {
     if (ModelState.IsValid)
     {
         db.Entry(shorturl).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(shorturl));
 }
Exemplo n.º 7
0
        public ActionResult Create(ShortURL shorturl)
        {
            if (ModelState.IsValid)
            {
                db.ShortURLs.Add(shorturl);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(shorturl));
        }
Exemplo n.º 8
0
 public bool RegisterUrl(ShortURL Url, Owner owner)
 {
     if (!IsIdAvail(Url.Key))
     {
         return(false);
     }
     else
     {
         _urls.Add(Url.Key, Url);
         return(true);
     }
 }
Exemplo n.º 9
0
 public int saveURLDetail(string OriginalURL, string Directory)
 {
     try
     {
         ShortURL shortURL = new ShortURL();
         return(shortURL.SaveURLDetail(OriginalURL, Directory));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 10
0
        public void CreateURL()
        {
            URL u = new URL(_urlvalue);

            Assert.Equal(_urlvalue, u.Value);

            ShortURL surl = new ShortURL(_encodedURL);

            u = new URL(_urlvalue, surl);
            Assert.Equal(surl, u.Id);
            Assert.Equal(_urlvalue, u.Value);
        }
Exemplo n.º 11
0
        public IActionResult GetOriginalURL([FromBody] string ShortCode)
        {
            ShortURL shortURL = DAL.URLShortner.GetByCode(ShortCode, _configuration);

            if (shortURL != null && !string.IsNullOrEmpty(shortURL.ShortCode))
            {
                return(Ok(shortURL));
            }
            else
            {
                return(NotFound(shortURL));
            }
        }
Exemplo n.º 12
0
        public DataTable getURL(string OriginalURL)
        {
            DataTable dt = new DataTable();

            try
            {
                ShortURL shortURL = new ShortURL();
                dt = shortURL.getURL(OriginalURL);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(dt);
        }
Exemplo n.º 13
0
        public DataTable getURLByID(string pk_URLID)
        {
            DataTable dt = new DataTable();

            try
            {
                ShortURL shortURL = new ShortURL();
                dt = shortURL.getURLByID(pk_URLID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(dt);
        }
Exemplo n.º 14
0
        public async void CreateNotExists_Test()
        {
            _database.Execute("FLUSHDB"); //cleans db
            ShortURL surl = new ShortURL(ShortURLTest._shortURL);
            URL      url  = new URL(ShortURLTest._url, surl);
            bool     res  = await _urlRepository.ExistsAsync(surl);

            Assert.False(res);

            await _urlRepository.AddAsync(url);

            res = await _urlRepository.ExistsAsync(surl);

            Assert.True(res);
        }
Exemplo n.º 15
0
        static ShortURLBack GetShortUrl(string url)
        {
            var apps      = File.ReadAllLines(@"D:\cert.txt");
            var payHandle = new PayHandle();
            var shortUrl  = new ShortURL()
            {
                AppID   = apps[0],
                MchID   = apps[1],
                Key     = apps[3],
                LongURL = url
            };
            var shortUrlBack = payHandle.Send(shortUrl) as ShortURLBack;

            SavaQR(shortUrlBack.ShortURL);
            return(shortUrlBack);
        }
Exemplo n.º 16
0
        public URLServiceTest()
        {
            _mockUrlRepo = new Mock <IURLRepository>();

            _surl = ShortURL.ComputeShortURLFromExtendedURL(ShortURLTest._shortURL);
            _url  = new URL(ShortURLTest._url, _surl);
            _mockUrlRepo.Setup(x => x.GetByIdAsync(It.IsAny <ShortURL>())).ReturnsAsync(_url);

            _mockConfig = new Mock <IConfigApplicationLimits>();
            _mockConfig.Setup(x => x.MaxURLChars).Returns(100);
            _mockConfig.Setup(x => x.ShortUrlBase64Padding).Returns("=");
            _mockConfig.Setup(x => x.ShortUrlBase64PaddingLength).Returns(1);
            _mockConfig.Setup(x => x.ShortUrlLength).Returns(11);
            _goodSurl = StringGenerator.RandomString(11, false);
            _badSurl  = "asda";
        }
Exemplo n.º 17
0
        public ActionResult URL(string id)
        {
            ShortURL shortenURL = this.GetDataByURL(id);

            if (shortenURL != null)
            {
                shortenURL.NumOfVisit = shortenURL.NumOfVisit + 1;

                db.Entry(shortenURL).State = EntityState.Modified;
                db.SaveChanges();

                return(Redirect(shortenURL.Long_URL));
            }
            else
            {
                return(Content("URL is not found."));
            }
        }
Exemplo n.º 18
0
        public async Task <string> CreateURLAsync(string extendedURL)
        {
            Guard.Against.NullOrEmpty(extendedURL, "URL value");
            Guard.Against.OutOfRange(extendedURL.Length, "URL value", 1, _config.MaxURLChars);
            string res = null;

            try {
                ShortURL surl = ShortURL.ComputeShortURLFromExtendedURL(extendedURL);
                if (!await _urlRepository.ExistsAsync(surl))
                {
                    URL newURL = new URL(extendedURL, surl);
                    await _urlRepository.AddAsync(newURL);
                }

                res = RemoveBase64Strings(surl.ToString());
            } catch (Exception excp) {
                _logger.LogError(excp, $"Error creating url with value: {extendedURL}.");
            }
            return(res);
        }
Exemplo n.º 19
0
        public async Task <string> GetURLAsync(string shortUrl)
        {
            Guard.Against.NullOrEmpty(shortUrl, "Short URL");
            Guard.Against.OutOfRange(shortUrl.Length, "Short URL", _config.ShortUrlLength, _config.ShortUrlLength);
            URL res = null;

            try {
                ShortURL id = new ShortURL(AddBase64Strings(shortUrl));
                res = await _urlRepository.GetByIdAsync(id);

                if (res == null)
                {
                    throw new URLNotFoundException(shortUrl);
                }
                return(res.Value);
            }catch (Exception excp)
            {
                _logger.LogError(excp, $"Error fetching url with shortURL: {shortUrl}.");
                res = null;
            }
            return(res?.Value);
        }
Exemplo n.º 20
0
        public IActionResult Generate([FromBody] string OriginalURL)
        {
            ShortURL url = DAL.URLShortner.GetByOriginal(OriginalURL, _configuration);

            if (url != null && !string.IsNullOrEmpty(url.ShortCode))
            {
                return(Ok(url));
            }
            else
            {
                ShortURL shorten = new ShortURL
                {
                    Original_Url = OriginalURL,
                    ShortCode    = ShortGenerator.RandomString(7),
                    DateAdded    = DateTime.Now,
                    DateUpdated  = DateTime.Now
                };
                _coreContext.ShortenedURLs.Add(shorten);
                _coreContext.SaveChanges();
                return(Ok(shorten));
            }
        }
Exemplo n.º 21
0
 public URL(string extended)
 {
     Value = extended;
     Id    = ShortURL.ComputeShortURLFromExtendedURL(extended);
 }
Exemplo n.º 22
0
 public void DeleteFireAndForget(ShortURL entity)
 {
     _redisDB.KeyDelete(entity.ToString(), flags: CommandFlags.FireAndForget);
 }
Exemplo n.º 23
0
 public async Task <bool> ExistsAsync(ShortURL id)
 {
     return(await _redisDB.KeyExistsAsync(id.ToString()));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Adds a short URL to the data context.
 /// </summary>
 /// <param name="shortUrl">The short URL to be added.</param>
 public void AddShortUrl(ShortURL shortUrl)
 {
     _entityContainer.ShortURLs.Add(shortUrl);
     _entityContainer.SaveChanges();
 }
Exemplo n.º 25
0
 /// <summary>
 /// Deletes a short URL from the data context.
 /// </summary>
 /// <param name="shortUrl">The short URL to be deleted.</param>
 public void DeleteShortUrl(ShortURL shortUrl)
 {
     _entityContainer.ShortURLs.Remove(shortUrl);
     _entityContainer.SaveChanges();
 }
Exemplo n.º 26
0
        public JsonResult CreateShortURL(string url)
        {
            JsonResult result        = new JsonResult();
            String     resultCode    = "SUCCESS";
            Boolean    resultSuccess = true;

            String returnMessage = "";

            String strShortenURL  = "";
            String strNumOfClicks = "0";

            if (this.IsURLValid(url) == true)
            {
                ShortURL shortenURL = this.GetDataByURL(url);
                if (shortenURL != null)
                {
                    // Return View and Return shortURL and shortURL Stats
                    returnMessage = "Return View and Return shortURL and shortURL Stats";

                    resultCode    = "SUCCESS_BUT_EXISTS";
                    resultSuccess = true;
                    returnMessage = "The URL is already shorten.";
                }
                else
                {
                    // GenerateShortURLID And Add URL to Database
                    returnMessage = "GenerateShortURLID And Add URL to Database";
                    String domainName = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port);
                    String sUID       = GenerateShortURLID();
                    shortenURL             = new ShortURL();
                    shortenURL.DateAdded   = DateTime.Now;
                    shortenURL.Long_URL    = url;
                    shortenURL.Short_URL   = domainName + "/" + sUID;
                    shortenURL.Short_URLID = sUID;
                    shortenURL.NumOfVisit  = 0;

                    // Save to Database
                    db.ShortURLs.Add(shortenURL);
                    db.SaveChanges();


                    resultCode    = "SUCCESS";
                    resultSuccess = true;
                    returnMessage = "URL successfully shorten.";
                }


                strShortenURL  = shortenURL.Short_URL;
                strNumOfClicks = shortenURL.NumOfVisit.ToString();
            }
            else
            {
                returnMessage = "Invalid URL.";
                resultSuccess = false;
                resultCode    = "INVALID_URL";
            }

            result = new JsonResult
            {
                Data = new
                {
                    success      = resultSuccess,
                    message      = returnMessage,
                    success_code = resultCode,
                    shorten_url  = strShortenURL,
                    num_clicks   = strNumOfClicks,
                }
            };


            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return(result);
        }
Exemplo n.º 27
0
 public URL(string value, ShortURL shortURL)
 {
     Value = value;
     Id    = shortURL;
 }