Пример #1
0
        public static UrlModel From(UrlMap urlMap)
        {
            if (urlMap == null) return new UrlModel();

            return new UrlModel
            {
                OriginalUrl = urlMap.OriginalUrl,
                ShortUrl = urlMap.ShortUrl,
                ShortUrlCode = urlMap.ShortUrlCode,
                IsActive = urlMap.IsActive,
                Clicks=urlMap.Clicks,
                DateCreated = urlMap.DateCreated
            };
        }
Пример #2
0
 public void AddUrlMap(UrlMap url, String userName = null)
 {
     url.UserName = userName;
     _UrlRepository.Add(url);
 }
Пример #3
0
 public void RemoveUrlMap(UrlMap url, String userName = null)
 {
     _UrlRepository.Delete(url);
 }
Пример #4
0
 public void UpdateMap(UrlMap url)
 {
     _UrlRepository.Update(url);
 }
Пример #5
0
        public ActionResult Generate(String longurl)
        {
            var jsonResp = new UrlShortenResponse{
            Success = true
            };

            /*
             *   +--------------------------------------------------------------------------------+
             *   | Step 1: Check if the URL is valid. We will issue a web request and see the     |
             *   |         status code.                                                           |
             *   | Step 2: Check if this URL is already in the system. If exists, return          |
             *   |         We will check for the current user, since we will generate a unique    |
             *   |         URL for each user for a given original URL                             |
             *   | Step 3: Generate short URL. (May be check again if we generated a duplicate?)  |
             *   | Step 4: Generate a QR code for the URL                                         |
             *   | Step 5: Save generated short url and also save the QR image.                   |
             *   | Step 6: Send Json response back to the user.                                   |
             *   +--------------------------------------------------------------------------------+
            */

            #region Step 1 - Check for a valid URL

            bool bValidUrl = true;
            try
            {
                HttpWebRequest request = WebRequest.Create(longurl) as HttpWebRequest;
                request.Method = "GET";
                request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                var response = request.GetResponse();
                bValidUrl = response.Headers.Count > 0;
            }
            catch
            {
                bValidUrl = false;
            }

            if (!bValidUrl)
            {
                return Json(new UrlShortenResponse
                {
                    Success = false,
                    Message = "Please check if the URL is correct and try again."
                }, "text/html");
            }

            String protoPrefix = "http://";
            String webUrl = longurl;
            if (longurl.Contains("://"))
            {
                int iIndex = longurl.IndexOf("://");
                protoPrefix = longurl.Substring(0, iIndex + 3);
                webUrl = longurl.Substring(iIndex + 3);
            }

            #endregion

            #region Step 2-5 - Generate URL and save

            UrlMap url = _UrlMapDataSource.FindSingleUrlByUserName(longurl, User.Identity.Name);
            if (url == null)
            {
                url = new UrlMap();
                url.OriginalUrl = longurl;
                url.DateCreated = DateTime.UtcNow;
                url.IsActive = true;

                //Step 3:
                //Now generate hash for the longUrl;
                uint hash = FNVHash.fnv_32a_str(webUrl + User.Identity.Name);

                //Convert hash to base36
                url.ShortUrlCode = Base36Converter.Encode(hash);

                url.ShortUrl = String.Format("{0}{1}", _URLShortenerHost, url.ShortUrlCode);

                //Step 4:
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                QrCode qrCode = new QrCode();
                qrEncoder.TryEncode(url.ShortUrl, out qrCode);

                Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);

                String fileName = String.Format("{0}{1}.qr", AppDomain.CurrentDomain.BaseDirectory, url.ShortUrlCode);
                renderer.CreateImageFile(qrCode.Matrix, fileName, ImageFormat.Png);
                jsonResp.QRCodeUrl = String.Format("{0}Home/QRImage/{1}.qr", _URLShortenerHost, url.ShortUrlCode);

                //Save Url Map
                _UrlMapDataSource.AddUrlMap(url, User.Identity.Name);

                //Save method appends the .qr extension
                _UrlMapDataSource.SaveQRCode(fileName, url.ShortUrlCode);
            }
            #endregion

            // Step 6
            jsonResp.Url = url.ShortUrl;
            jsonResp.QRCodeUrl = String.Format("{0}Home/QRImage/{1}.qr", _URLShortenerHost, url.ShortUrlCode);

            return Json(jsonResp, "text/html");
        }