Exemple #1
0
        public ActionResult Create([Bind(Include = "TiendaVitualId,Nombre,Dominio,Descripcion,Logo,Fecha_creacion,Estado,Css,StringConection")] TiendaVirtualDTO tiendaVirtualDTO, HttpPostedFileBase logo)
        {
            tiendaVirtualDTO.UsuarioId = User.Identity.GetUserId();

            var num = _bl.ObtenerTiendaDelUsuario(tiendaVirtualDTO.UsuarioId).Count();

            if (num < 1)
            {
                if (ModelState.IsValid)
                {
                    var dominio_temp = tiendaVirtualDTO.Dominio.ToLower();
                    var dominio      = "";
                    try
                    {
                        dominio = Regex.Replace(dominio_temp, @"\s+", "");
                        tiendaVirtualDTO.Dominio = dominio;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    CloudBlobContainer blobContainer = _bss.GetContainerTienda(tiendaVirtualDTO.Dominio);
                    if (logo != null)
                    {
                        if (logo.ContentLength > 0)
                        {
                            var            nombreFoto = tiendaVirtualDTO.Dominio + Guid.NewGuid().ToString() + "_logo";
                            CloudBlockBlob blob       = blobContainer.GetBlockBlobReference(nombreFoto);
                            blob.UploadFromStream(logo.InputStream);
                            tiendaVirtualDTO.Logo = blob.Uri.ToString();
                        }
                    }
                    else
                    {
                        var path       = Server.MapPath(@"~/Imagenes/tiendadefault.png");
                        var nombreFoto = tiendaVirtualDTO.Dominio + Guid.NewGuid().ToString() + "_logo";
                        //byte[] imgDefault = System.IO.File.ReadAllBytes(path);
                        FileStream     fs   = new FileStream(path, FileMode.Create);
                        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(nombreFoto);
                        blob.UploadFromStream(fs);
                        tiendaVirtualDTO.Logo = blob.Uri.ToString();
                    }


                    ////string strMappath = "~/imagenes/" + tiendaVirtualDTO.Nombre;
                    //string strMappath = "~/imagenes/";
                    //try
                    //{

                    //    if (logo != null)
                    //    {
                    //        //var nombreFoto = juego.Nombre + "_" + Guid.NewGuid().ToString() + "_" + Path.GetFileName(foto.FileName);
                    //        var nombreFoto = tiendaVirtualDTO.Nombre + Guid.NewGuid().ToString() + Path.GetExtension(logo.FileName);
                    //        var rutaFoto = Path.Combine(Server.MapPath(strMappath), nombreFoto);
                    //        logo.SaveAs(rutaFoto);
                    //        tiendaVirtualDTO.Logo = strMappath + nombreFoto;
                    //    }
                    //    else
                    //    {
                    //        tiendaVirtualDTO.Logo = "~/Imagenes/tiendadefault.png";
                    //    }
                    //}
                    //catch (Exception e)
                    //{
                    //    Console.WriteLine("The process failed: {0}", e.ToString());
                    //}

                    var fileContents = System.IO.File.ReadAllText(Server.MapPath(@"~/Content/Site.css"));

                    //Controlo que el dominio sea en minuscula



                    // tiendaVirtualDTO.Dominio = dominio;
                    tiendaVirtualDTO.Css             = fileContents.ToString();
                    tiendaVirtualDTO.Fecha_creacion  = System.DateTime.Now;
                    tiendaVirtualDTO.Estado          = true;
                    tiendaVirtualDTO.StringConection = "StringConection";

                    _bl.AgregarTienda(tiendaVirtualDTO);
                    // IIS AGREGO SITIO
                    //127.0.0.1:80:sitio.chebay.com
                    //ServerManager iisManager = new ServerManager();
                    //iisManager.Sites.Add(dominio, "http", "127.0.0.1:80:"+dominio+".chebay.com", "C:\\inetpub\\wwwroot\\Site");
                    //iisManager.CommitChanges();


                    //ServerManager manager = new ServerManager();
                    try
                    {
                        IHosts _hosts = new Hosts();

                        _hosts.AgregarSitio(dominio);

                        // Add this site.

                        /*Site hrSite = manager.Sites.Add(name, "http", "*:80:", path);
                         * // The site will need to be started manually.
                         * hrSite.ServerAutoStart = false;
                         * manager.CommitChanges();
                         * Console.WriteLine("Site " + name + " added to ApplicationHost.config file.");*/


                        /*ServerManager serverMgr = new ServerManager();
                         * string strWebsitename = dominio; // abc
                         * string strApplicationPool = dominio.ToUpper();  // set your deafultpool :4.0 in IIS
                         * string strhostname = dominio+".chebay.com"; //abc.com
                         * string stripaddress = "127.0.0.1";// ip address
                         * string bindinginfo = stripaddress + ":80:" + strhostname;*/

                        //check if website name already exists in IIS
                        //ServerManager serverMgr = new ServerManager();
                        //Site mySite = serverMgr.Sites.Add(dominio.ToUpper(), "C:\\inetpub\\wwwroot\\Site", 80);
                        //serverMgr.ApplicationPools.Add("DefaultAppPool");
                        //mySite.ApplicationDefaults.ApplicationPoolName = "DefaultAppPool";
                        //mySite.TraceFailedRequestsLogging.Enabled = true;
                        //mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\wwwroot\\site";
                        //serverMgr.CommitChanges();


                        /* Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:\\inetpub\\wwwroot");
                         * mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
                         * mySite.TraceFailedRequestsLogging.Enabled = true;
                         * mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\wwwroot\\Site";
                         * serverMgr.CommitChanges();*/
                        //lblmsg.Text = "New website  " + strWebsitename + " added sucessfully";
                        //lblmsg.ForeColor = System.Drawing.Color.Green;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    // IIS AGREGO SITIO
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                return(RedirectToAction("Index"));
            }

            return(View(tiendaVirtualDTO));
        }