public async Task <PublishPageResponseModel> Ftp_IU(PublishExternalRequestModel data, string userId)
        {
            PublishPageResponseModel response = new PublishPageResponseModel();
            var itg = uow.Integration.Get(data.IntegrationId);

            if (itg == null)
            {
                throw new BusinessException("Không tìm thấy kết nối đến FTP");
            }
            var page = await uow.LandingPage.GetPageForSubcrible(data.PageId);

            if (page == null)
            {
                throw new BusinessException("Landing page không tồn tại");
            }

            FtpTask svc = new FtpTask(itg.Email, itg.AccId, itg.ApiKey);
            var     res = svc.UploadFile(data.PathUrl + ".html", data.Html);

            if (res.Code == System.Net.HttpStatusCode.OK)
            {
                //var domain = res.Data.ToString().Replace("https://", "").Replace("http://", "");
                //var basedomain = itg.AccId.Replace("https://", "").Replace("http://", "");
                var landingpage = new LandingPage()
                {
                    Id                   = data.PageId,
                    UserId               = userId,
                    Domain               = page.Domain,
                    Https                = true,
                    BaseDomain           = page.Domain,
                    UrlCode              = data.PathUrl,
                    PublishIntegrationId = itg.Id,
                    PublishType          = (int)PublishType.Ftp
                };

                await uow.LandingPage.IU(landingpage, "publish");

                response.FullUrl              = "https://" + page.Domain;
                response.PathUrl              = landingpage.UrlCode;
                response.BaseDomain           = landingpage.BaseDomain;
                response.Type                 = landingpage.Type;
                response.PublishIntegrationId = landingpage.PublishIntegrationId;
                return(response);
            }
            else
            {
                throw new BusinessException(res.Message);
            }
        }
        public async Task <PublishPageResponseModel> Sapo_IU(PublishExternalRequestModel data, string userId)
        {
            PublishPageResponseModel response = new PublishPageResponseModel();
            var itg = uow.Integration.Get(data.IntegrationId);

            if (itg == null)
            {
                throw new BusinessException("Không tìm thấy kết nối đến Sapo");
            }
            var page = await uow.LandingPage.GetPageForSubcrible(data.PageId);

            if (page == null)
            {
                throw new BusinessException("Landing page không tồn tại");
            }

            var res = await new SapoService.SapoService(itg.AccId, itg.ApiKey).CreateOrUpdatePage(data.Html, page.Name, data.PathUrl, page.Code, page.PublishReferId);

            if (res.Code == System.Net.HttpStatusCode.OK)
            {
                var link   = $"{itg.AccId}/{data.PathUrl}";
                var domain = link.Replace("https://", "").Replace("http://", "");

                var landingpage = new LandingPage()
                {
                    Id                   = data.PageId,
                    UserId               = userId,
                    Domain               = domain,
                    Https                = true,
                    BaseDomain           = itg.AccId.Replace("https://", "").Replace("http://", ""),
                    UrlCode              = data.PathUrl,
                    PublishReferId       = res.Data.ToString(),
                    PublishIntegrationId = itg.Id,
                    PublishType          = (int)PublishType.Sapo
                };
                await uow.LandingPage.IU(landingpage, "publish");

                response.FullUrl              = "https://" + link;
                response.PathUrl              = landingpage.UrlCode;
                response.BaseDomain           = landingpage.BaseDomain;
                response.Type                 = landingpage.Type;
                response.PublishIntegrationId = landingpage.PublishIntegrationId;
                return(response);
            }
            else
            {
                throw new BusinessException(res.Message);
            }
        }
Example #3
0
        public async Task <IHttpActionResult> PublishToVendor([FromBody] PublishExternalRequestModel value)
        {
            try
            {
                PublishPageResponseModel res = new PublishPageResponseModel();
                switch (value.Vendor)
                {
                case (int)IntegrationType.Wordpress:
                    res = await _uow.Integration.WP_IU(value, this.CurrentUserId);

                    break;

                case (int)IntegrationType.Shopify:
                    res = await _uow.Integration.Shopify_IU(value, this.CurrentUserId);

                    break;

                case (int)IntegrationType.Haravan:
                    res = await _uow.Integration.Haravan_IU(value, this.CurrentUserId);

                    break;

                case (int)IntegrationType.Sapo:
                    res = await _uow.Integration.Sapo_IU(value, this.CurrentUserId);

                    break;

                case (int)IntegrationType.Ftp:
                    res = await _uow.Integration.Ftp_IU(value, this.CurrentUserId);

                    break;
                }
                return(Ok(res));
            }
            catch (BusinessException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                return(BadRequest(ex.Message));
            }
        }
        public async Task <PublishPageResponseModel> PublishDns(PublishRequestModel model, string userId)
        {
            PublishPageResponseModel response = new PublishPageResponseModel();
            var m = _dbSet.FirstOrDefault(x => x.Id == model.id && x.UserId == userId);

            if (m == null)
            {
                throw new BusinessException(Messages.LandingPage_NotFound);
            }
            if (model.type > 0)
            {
                m.PublishType = model.type;
            }

            if (m.PublishIntegrationId.HasValue && m.Publish == true)
            {
                var itg = uow.Integration.Get(m.PublishIntegrationId.Value);
                if (itg != null)
                {
                    //ftp
                    if (m.Publish && m.PublishType == (int)PublishType.Ftp)
                    {
                        if (m.PublishIntegrationId == null)
                        {
                            throw new BusinessException("Vui lòng xem lại cấu hình kết nối Ftp");
                        }
                        PublishExternalRequestModel data = new PublishExternalRequestModel()
                        {
                            Html          = model.html,
                            PageId        = model.id,
                            PathUrl       = m.UrlCode,
                            IntegrationId = m.PublishIntegrationId.Value
                        };
                        await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Xuất bản Ftp");

                        return(await uow.Integration.Ftp_IU(data, userId));
                    }

                    //wordpress
                    if (m.Publish && m.PublishType == (int)PublishType.WP)
                    {
                        if (m.PublishIntegrationId == null)
                        {
                            throw new BusinessException("Vui lòng xem lại cấu hình kết nối Wordpress");
                        }
                        PublishExternalRequestModel data = new PublishExternalRequestModel()
                        {
                            Html          = model.html,
                            PageId        = model.id,
                            PathUrl       = m.UrlCode,
                            IntegrationId = m.PublishIntegrationId.Value
                        };
                        await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Xuất bản wordpress");

                        return(await uow.Integration.WP_IU(data, userId));

                        //return await uow.Integration.WP_IU(model.id, m.Name, model.html);
                    }

                    //shopify
                    if (m.Publish && m.PublishType == (int)PublishType.Shopify)
                    {
                        if (m.PublishIntegrationId == null)
                        {
                            throw new BusinessException("Vui lòng xem lại cấu hình kết nối Shopify");
                        }
                        PublishExternalRequestModel data = new PublishExternalRequestModel()
                        {
                            Html          = model.html,
                            PageId        = model.id,
                            PathUrl       = m.UrlCode,
                            IntegrationId = m.PublishIntegrationId.Value
                        };
                        await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Xuất bản shopify");

                        return(await uow.Integration.Shopify_IU(data, userId));
                    }

                    //haravan
                    if (m.Publish && m.PublishType == (int)PublishType.Haravan)
                    {
                        if (m.PublishIntegrationId == null)
                        {
                            throw new BusinessException("Vui lòng xem lại cấu hình kết nối Haravan");
                        }
                        PublishExternalRequestModel data = new PublishExternalRequestModel()
                        {
                            Html          = model.html,
                            PageId        = model.id,
                            PathUrl       = m.UrlCode,
                            IntegrationId = m.PublishIntegrationId.Value
                        };
                        await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Xuất bản haravan");

                        return(await uow.Integration.Haravan_IU(data, userId));
                    }

                    //sapo
                    if (m.Publish && m.PublishType == (int)PublishType.Sapo)
                    {
                        if (m.PublishIntegrationId == null)
                        {
                            throw new BusinessException("Vui lòng xem lại cấu hình kết nối Sapo");
                        }
                        PublishExternalRequestModel data = new PublishExternalRequestModel()
                        {
                            Html          = model.html,
                            PageId        = model.id,
                            PathUrl       = m.UrlCode,
                            IntegrationId = m.PublishIntegrationId.Value
                        };
                        await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Xuất bản sapo");

                        return(await uow.Integration.Sapo_IU(data, userId));
                    }
                }
                else
                {
                    m.Publish    = false;
                    m.Domain     = null;
                    m.BaseDomain = PUBLISH_DOMAIN;
                    model.domain = null;
                }
            }

            bool isFirstPublish = (m.Publish == false || string.IsNullOrEmpty(model.domain));

            var hasBaseDomain = (string.IsNullOrEmpty(m.BaseDomain) == false);

            model.domain = string.IsNullOrEmpty(model.domain) ? PUBLISH_DOMAIN : model.domain;
            model.domain = model.domain.Replace("www.", "");
            if (IsValidUri("http://" + model.domain) == false)
            {
                throw new BusinessException("Tên miền không hợp lệ!");
            }

            Uri u = new Uri("http://" + model.domain);


            bool   isHostChanged = (u.Host != m.BaseDomain);
            string old_domain    = m.BaseDomain;
            string old_UrlCode   = m.UrlCode;

            m.BaseDomain = u.Host;
            string path_name = u.LocalPath.Replace("/", "");

            if (string.IsNullOrEmpty(path_name) && IsSystemPublishDomain(m.BaseDomain))
            {
                if (string.IsNullOrEmpty(m.Name) == true)
                {
                    m.Name = "Tiêu đề trang";
                }
                m.UrlCode = MakeUrlCode(m.BaseDomain, m.Name);
            }
            else
            {
                if (uow.Domain.IsNotOwner(m.BaseDomain, userId) == true && IsSystemPublishDomain(m.BaseDomain) == false)
                {
                    throw new BusinessException(string.Format("{0} đã được sử dụng!", m.BaseDomain));
                }

                var exists = _dbSet.AsNoTracking().Any(x => x.Deleted == false && x.BaseDomain == m.BaseDomain && x.UrlCode == path_name && x.UserId == userId && x.Id != model.id);
                if (exists)
                {
                    throw new BusinessException(string.Format("{0} đã được sử dụng cho landing page khác", model.domain));
                }
                m.UrlCode = path_name;
            }

            var res = new Core.BLL.FileServices.FileBuilder(m.BaseDomain, m.UrlCode, model.html).Create(m.Id.ToString());

            if (model.domain != m.Domain && !string.IsNullOrEmpty(old_domain))
            {
                new Core.BLL.FileServices.FileBuilder(old_domain, old_UrlCode, "").Remove();
            }

            string urlFolderPath = u.Host;

            //Nếu user cấu hình domain mới => kiểm tra IIS
            bool hasTracking = true;

            if (!string.IsNullOrEmpty(m.BaseDomain) && isHostChanged && IsSystemPublishDomain(m.BaseDomain) == false)
            {
                if (isHostChanged)
                {
                    //Kiem tra domain da tro ve DNS server?
                    uow.Domain.Verify(m.BaseDomain, userId);
                    //Tracking với domain ngoài
                    hasTracking = false;
                }
                var pagesInDomain = await GetByBaseDomain(old_domain);

                var iisService = new Services.IISService();
                if (pagesInDomain.Count <= 1 && IsSystemPublishDomain(old_domain) == false)
                {
                    var path = await iisService.ChangeWebsite(old_domain, m.BaseDomain);
                }
                else
                {
                    var path = await iisService.AddWebsite(m.BaseDomain);
                }
                // create Web on IIS
                //if (path != "") res = path;
            }

            //var iisService1 = new Services.IISService();
            //iisService1.ChangeWebsite(m.BaseDomain);

            m.PublishType = (int)PublishType.Dns;//IsSystemPublishDomain(m.BaseDomain) ? (int)PublishType.Demo : (int)PublishType.Domain;
            m.Domain      = res;
            if (!string.IsNullOrEmpty(model.source))
            {
                m.Source = model.source;
            }
            m.Publish     = true;
            m.PublishDate = DateTime.Now;
            m.UpdatedDate = DateTime.Now;
            await this.CommitAsync();

            this.RemoveCache(m.Id, "p");
            if (isFirstPublish || hasTracking == false)
            {
                try
                {
                    await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Hot, "Đã xuất bản domain");

                    //uow.TaskQueue.AddTask(new TaskQueue()
                    //{
                    //    Data = m.Id.ToString(),
                    //    Type = (int)Entities.TaskQueueType.UpdatePageContent,
                    //    CreatedDate = DateTime.Now,
                    //    UpdatedDate=DateTime.Now
                    //});
                }catch (Exception ex)
                {
                }
            }
            else if (IsSystemPublishDomain(m.BaseDomain) && isHostChanged)
            {
                try
                {
                    await uow.UserProfile.SetUserStatusNote(m.UserId, LeadStatus.Warm, "Đã xuất bản trang");
                }
                catch (Exception ex)
                {
                }
            }



            if (hasTracking == false)
            {
                uow.PublishPage.IU(new PublishPage()
                {
                    Id          = m.Id,
                    UrlPath     = urlFolderPath,
                    HasTracking = hasTracking,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });
            }

            response.FullUrl              = res;
            response.PathUrl              = m.UrlCode;
            response.BaseDomain           = m.BaseDomain;
            response.Type                 = m.PublishType;
            response.PublishIntegrationId = m.PublishIntegrationId;
            return(response);
        }