Esempio n. 1
0
        public async Task <ActionResult> Redirection(string shortUrl)
        {
            ShortLinkResponse slresponse = new ShortLinkResponse();

            slresponse.OperationSucceded = false;

            if (!string.IsNullOrEmpty(shortUrl))
            {
                var guestguid = CookieManager.TryAddCookie("VisitorNumber");
                shortUrl = Server.HtmlEncode(shortUrl);

                WebAPIManager wamgr         = new WebAPIManager();
                string        webapiaddress = ConfigurationManager.AppSettings["WebAPISite"];
                slresponse = await wamgr.GetAsync(String.Format("{0}{1}/{2}", webapiaddress, shortUrl, guestguid));
            }

            if (slresponse.OperationSucceded)
            {
                return(Redirect(slresponse.Link));
            }
            else
            {
                return(View("Index"));
            }
        }
        public ShortLinkResponse Post([FromBody] ShortLinkRequest slrequest)
        {
            LinkManager       urlManager = new LinkManager();
            ShortLinkResponse ro         = new ShortLinkResponse();

            ro = urlManager.AddShortLink(slrequest);

            return(ro);
        }
        public ShortLinkResponse Get(string key, string guestguid)
        {
            ShortLinkResponse ro = new ShortLinkResponse();

            LinkManager urlManager = new LinkManager();

            ro = urlManager.GetUrl(key, guestguid);

            return(ro);
        }
        public ShortLinkResponse AggiungiLink([FromBody] ShortLinkRequest slrequest)
        {
            //ShortLinkRequest slrequest = JsonConvert.DeserializeObject<ShortLinkRequest>(data);
            LinkManager       urlManager = new LinkManager();
            ShortLinkResponse ro         = new ShortLinkResponse();

            ro = urlManager.AddShortLink(slrequest);

            //return ro;
            var response = new HttpResponseMessage(HttpStatusCode.Created)
            {
                Content = new StringContent(JsonConvert.SerializeObject(ro))
            };

            return(ro);
        }
Esempio n. 5
0
        public async Task <ShortLinkResponse> GetAsync(string uri)
        {
            string         streamread = string.Empty;
            HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(uri);

            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        streamread = reader.ReadToEnd();
                    }

            ShortLinkResponse slr = JsonConvert.DeserializeObject <ShortLinkResponse>(streamread);

            return(slr);
        }
Esempio n. 6
0
        public ShortLinkResponse GetUrl(string ShortLinkKey, string guestGuid)
        {
            ShortLinkResponse ro = new ShortLinkResponse();

            ro.ErrorMessage = string.Empty;

            var url = unitOfWork.ShortLinkRepository.FindByProperty(l => l.Key.ToUpper() == ShortLinkKey.ToUpper());

            if (url != null)
            {
                AddGuest(guestGuid);
                AddVisit(url.Key, guestGuid);

                ro.Link = url.Url;
                ro.OperationSucceded = true;
            }
            else
            {
                ro.OperationSucceded = false;
                ro.ErrorMessage      = "ShortLink is Missing";
            }

            return(ro);
        }
Esempio n. 7
0
        public ShortLinkResponse AddShortLink(ShortLinkRequest linkRequest)
        {
            ShortLinkResponse ro = new ShortLinkResponse();

            ro.ErrorMessage = string.Empty;

            string Url = linkRequest.Link;

            if (!string.IsNullOrWhiteSpace(Url))
            {
                Url = Url.Trim().ToLower();
            }

            String cached = null;

            cached = CacheManager.GetCached <String>(Url);
            if (!String.IsNullOrWhiteSpace(cached))
            {
                ro.ShortLink         = cached;
                ro.OperationSucceded = true;

                return(ro);
            }

            try
            {
                //var url = new Uri(Url);
                UriBuilder uriBuilder = new UriBuilder(Url);
            }
            catch (Exception)
            {
                ro.OperationSucceded = false;
                ro.ErrorMessage      = "Invalid Link";
                return(ro);
            }

            String newKey = null;

            while (string.IsNullOrEmpty(newKey))
            {
                if (!unitOfWork.ShortLinkRepository.Exists(l => l.Url == Url))
                {
                    newKey = Guid.NewGuid().ToString("N").Substring(0, ConfigManager.KeyLength).ToLower();
                    Data.Models.ShortLink link = new Data.Models.ShortLink();
                    link.Key         = newKey;
                    link.Url         = Url;
                    link.DateCreated = DateTime.Now;
                    try
                    {
                        unitOfWork.ShortLinkRepository.Insert(link);
                        unitOfWork.Save();
                    }
                    catch (Exception)
                    {
                        ro.OperationSucceded = false;
                        ro.ErrorMessage      = "Error insert link in database";
                    }
                }
                else
                {
                    var lnks = unitOfWork.ShortLinkRepository.Get(l => l.Url == Url);
                    newKey = lnks.FirstOrDefault().Key;
                }
            }

            CacheManager.TryAddToCache <String>(Url, newKey);

            AddGuest(linkRequest.GuestGuid);
            AddVisit(newKey, linkRequest.GuestGuid);

            ro.ShortLink         = newKey;
            ro.OperationSucceded = true;

            return(ro);
        }