Exemplo n.º 1
0
        public IActionResult KPreview([FromBody] KitsunePreviewModel previewModel)
        {
            if (previewModel != null)
            {
                try
                {
                    var byteStream = Convert.FromBase64String(previewModel.FileContent);
                    previewModel.FileContent = System.Text.Encoding.UTF8.GetString(byteStream);

                    return(Ok(RequestHandler.GetKLMPreviewResponse(previewModel.ProjectId, previewModel.FileContent, previewModel.View, previewModel.ViewType, previewModel.WebsiteTag, previewModel.DeveloperId, previewModel.UrlParams, previewModel.NoCacheQueryParam, false)));
                }
                catch (Exception ex) { throw; }
            }
            return(null);
        }
        public async Task <ProjectPreview> GetPreviewAsync(string userEmail, string projectId, GetResourceDetailsResponseModel resourceDetails,
                                                           GetProjectDetailsResponseModel projectDetails,
                                                           KEntity languageEntity,
                                                           GetPartialPagesDetailsResponseModel partialPages,
                                                           string fpTag, string userId)
        {
            var    watch        = new Stopwatch();
            string compilerTime = "";

            watch.Start();

            ProjectPreview result = null;

            if (projectId != null && resourceDetails != null)
            {
                var doc = new HtmlAgilityPack.HtmlDocument();

                var compileResult = KitsuneCompiler.CompileHTML(new CompileResourceRequest
                {
                    UserEmail   = userEmail,
                    FileContent = resourceDetails.HtmlSourceString,
                    IsDev       = true,
                    IsPublish   = true,
                    ProjectId   = projectId,
                    SourcePath  = resourceDetails.SourcePath,
                    ClassName   = resourceDetails.ClassName,
                    IsPreview   = true
                }, out doc, projectDetails, languageEntity, partialPages);
                compilerTime += ", CompileHTML : " + watch.ElapsedMilliseconds.ToString();

                watch.Restart();
                var previewRes = "";
                if (!compileResult.Success)
                {
                    previewRes = ErrorCodeConstants.CompilationError;
                }
                else if (resourceDetails.IsStatic)
                {
                    previewRes = compileResult.CompiledString;
                }
                else
                {
                    try
                    {
                        var client = new HttpClient();
                        client.BaseAddress = new Uri("http://kitsune-demo-identifier-952747768.ap-south-1.elb.amazonaws.com");
                        byte[] bytes  = Encoding.UTF8.GetBytes(compileResult.CompiledString);
                        string base64 = Convert.ToBase64String(bytes);
                        var    obData = new KitsunePreviewModel
                        {
                            DeveloperId       = userId,
                            FileContent       = base64,
                            NoCacheQueryParam = null,
                            ProjectId         = projectId,
                            View       = resourceDetails.SourcePath,
                            WebsiteTag = fpTag
                        };
                        var jsonData = JsonConvert.SerializeObject(obData);

                        var response = await client.PostAsync("/home/kpreview", new StringContent(jsonData, Encoding.UTF8, "application/json"));

                        if (!((int)response.StatusCode >= 200 && (int)response.StatusCode <= 300))//If status code is not 20X, error.
                        {
                            // Log(response?.Content?.ReadAsStringAsync()?.Result);
                            throw new Exception(ErrorCodeConstants.UnableToGetPreview);
                        }
                        previewRes = response.Content.ReadAsStringAsync().Result;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                var previewTime = watch.ElapsedMilliseconds;
                result = new ProjectPreview
                {
                    HtmlString   = previewRes,
                    CompilerTime = compilerTime,
                    PreviewTime  = previewTime
                };
            }
            return(result);
        }