public JsonResult UrlShortnerAction(string OriginalUrl)
        {
            var result = new JsonResult();

            if (string.IsNullOrEmpty(OriginalUrl))
            {
                result.Data = "Index";
                return(result);
            }

            try
            {
                List <UrlShortnerViewModel> urlList = new List <UrlShortnerViewModel>();
                UrlShortnerViewModel        Row     = new UrlShortnerViewModel();

                List <UrlShortnerListViewModel> objeUrlList = new List <UrlShortnerListViewModel>();
                UrlShortnerListViewModel        Row1        = new UrlShortnerListViewModel();

                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    result.Data = "Login";
                    return(result);
                }
                else
                {
                    if ((bool)Session["PaymentStatus"] == false)
                    {
                        result.Data = "Plans";
                        return(result);
                    }

                    if (OriginalUrl.StartsWith("http://") || OriginalUrl.StartsWith("https://"))
                    {
                    }
                    else
                    {
                        OriginalUrl = "http://" + OriginalUrl;
                    }

                    //Select domain for persian domain
                    //string currenturl = HttpContext.Request.Url.AbsoluteUri;

                    //string currenturl = HttpContext.Request.Url.AbsoluteUri;

                    //if (currenturl.Contains(persiandomain))
                    //{
                    //    hosturl = persiandomain;
                    //}

                    //Create Unique short key and check if exist then recreate
                    string  urlKey = "";
                    dynamic shorturlList;
                    do
                    {
                        urlKey       = GetURL();
                        shorturlList = urlShortnerRepository.GetAllShortUrl(hosturl);
                    }while (shorturlList.Contains(urlKey));

                    #region Inster Logic
                    Row.OriginalUrl = OriginalUrl;
                    Row.RegisId     = Convert.ToInt64(Session["UserId"]);
                    Row.AdId        = adManageRepository.MemberAdId(Convert.ToInt64(Session["UserId"]));
                    Row.AddedDate   = DateTime.Now.Date;
                    Row.UpdatedDate = DateTime.Now.Date;
                    Row.ShortUrlKey = hosturl + urlKey;
                    urlList.Add(Row);
                    urlShortnerRepository.AddUrl(Mapper.Map <UrlShortnerViewModel, UrlShortner.Database.Entities.UrlShortner>(Row));
                    urlShortnerRepository.SaveAll();
                    #endregion

                    #region Create HTML
                    var    strReturnHTMLGenerate = RenderViewToString("AdViewClone", "", ControllerContext);
                    string HTMLFileUrl           = hosturl + "/AdHTML/" + adManageRepository.GetFileName(Row.AdId);
                    string HTMLAdViewUrl         = hosturl + "/Ad/" + urlKey + ".html";
                    //script replace

                    //strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("http://43.250.164.92:1150/AdHTML/ad.html", HTMLFileUrl);
                    strReturnHTMLGenerate = strReturnHTMLGenerate.Replace(hosturl + "AdHTML/ad.html", HTMLFileUrl);
                    strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("http://www.example.com", OriginalUrl);
                    strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("https://www.google.co.in", OriginalUrl);

                    using (FileStream fs = new FileStream(Server.MapPath("~\\Ad\\" + urlKey + ".html"), FileMode.Create))
                    {
                        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                        {
                            w.Write(strReturnHTMLGenerate);
                        }
                    }
                    #endregion

                    //Row1.OriginalUrl = HTMLAdViewUrl;
                    Row1.OriginalUrl = urlKey;
                    //Row1.ShortUrlKey = ConfigurationManager.AppSettings["HostUrl"] + urlKey;
                    Row1.ShortUrlKey = shorturl + urlKey;

                    if (string.IsNullOrEmpty(Session["UrlList"].ToString()))
                    {
                        objeUrlList.Add(Row1);
                        Session["UrlList"] = objeUrlList;
                    }
                    else
                    {
                        objeUrlList = (List <UrlShortnerListViewModel>)Session["UrlList"];
                        objeUrlList.Add(Row1);
                    }
                    var strReturn = RenderViewToString("_ListofUrl", objeUrlList, ControllerContext);
                    result.Data = strReturn;
                    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    return(result);
                }
            }
            catch (Exception ex)
            {
                result.Data = ex.Message.ToString();
                result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return(result);
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(UrlShortnerViewModel model, FormCollection form)
        {
            string SelectedAdId = form["SelectedAd"];
            long   J;
            //string requesturl = HttpContext.Request.Url.AbsoluteUri.ToString();
            var requesturl = string.Format("{0}://{1}{2}", HttpContext.Request.Url.Scheme,
                                           HttpContext.Request.Url.Authority,
                                           Url.Content("~"));

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                //FTP Folder name. Leave blank if you want to upload to root folder.
                string ftpFolder = "Ad/";

                string  urlKey = "";
                dynamic shorturlList;

                if (model.OriginalUrl.StartsWith("http://") || model.OriginalUrl.StartsWith("https://"))
                {
                }
                else
                {
                    model.OriginalUrl = "http://" + model.OriginalUrl;
                }

                //create short url key
                do
                {
                    urlKey       = GetURL();
                    shorturlList = urlShortnerRepository.GetAllShortUrl(hosturl);
                }
                //check if already exist then create again short key
                while (shorturlList.Contains(urlKey));

                var strReturnHTMLGenerate = RenderViewToString("AdViewClone", "", ControllerContext);

                string HTMLFileUrl = "";

                if (!string.IsNullOrEmpty(SelectedAdId))
                {
                    HTMLFileUrl = hosturl + "/AdHTML/" + adManageRepository.GetFileName(Int64.Parse(SelectedAdId.ToString()));
                }
                else
                {
                    HTMLFileUrl = hosturl + "/AdHTML/" + adManageRepository.GetFileName(adManageRepository.MemberAdId(Int64.Parse(Session["UserId"].ToString())));
                }

                string HTMLAdViewUrl = hosturl + "/Ad/" + urlKey + ".html";

                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace(hosturl + "AdHTML/ad.html", HTMLFileUrl);
                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("http://www.example.com", model.OriginalUrl);
                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("https://www.google.co.in", model.OriginalUrl);

                //Read the FileName and convert it to Byte array.

                try
                {
                    string directorypath = "";
                    if (requesturl.Contains(domain_ijli))
                    {
                        directorypath = @"C:\Inetpub\vhosts\ijl.li\httpdocs\UrlShotner\" + ftpFolder + urlKey + ".html";
                    }
                    else
                    {
                        directorypath = @"C:\Inetpub\vhosts\aiim.li\httpdocs\UrlShotner\" + ftpFolder + urlKey + ".html";
                    }
                    //using (FileStream fs = new FileStream(Server.MapPath("~\\Ad\\" + urlKey + ".html"), FileMode.Create))
                    using (FileStream fs = new FileStream(directorypath, FileMode.Create))
                    {
                        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                        {
                            w.Write(strReturnHTMLGenerate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(View(model));
                    //throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
                }

                Database.Entities.UrlShortner objUrlShortner = new Database.Entities.UrlShortner();
                objUrlShortner           = Mapper.Map <UrlShortnerViewModel, Database.Entities.UrlShortner>(model);
                objUrlShortner.AddedDate = DateTime.Now.Date;
                objUrlShortner.RegisId   = long.TryParse(Session["UserId"].ToString(), out J) == true?Int64.Parse(Session["UserId"].ToString()) : 0;

                objUrlShortner.UpdatedDate = DateTime.Now.Date;
                //objUrlShortner.ShortUrlKey = ConfigurationManager.AppSettings["HostUrl"] + GetURL();
                objUrlShortner.ShortUrlKey = hosturl + urlKey;
                objUrlShortner.UpdatedBy   = Int64.Parse(Session["UserId"].ToString());

                //single user or admin
                if ((string)Session["MemberShipType"] == "Single" || IsAdmin(Int64.Parse(Session["UserId"].ToString())))
                {
                    objUrlShortner.AdId = adManageRepository.MemberAdId(Int64.Parse(Session["UserId"].ToString()));
                }

                urlShortnerRepository.AddUrl(objUrlShortner);
                bool result = urlShortnerRepository.SaveAll();

                if (result == true)
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    return(View(model));
                }
            }
        }