public IActionResult UploadMultipartResource(string projectid, [FromForm] ApplicationUploadModel req, IFormFile file = null)
        {
            try
            {
                if (req == null)
                {
                    return(BadRequest("Request object can not be null"));
                }
                byte[] byteArrayStream = null;
                if (file != null)
                {
                    Stream stream = Request.Body;
                    byteArrayStream = Kitsune.API2.EnvConstants.Constants.ReadInputStream(file);
                }


                req.ProjectId = projectid;

                var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                {
                    ProjectId        = req.ProjectId,
                    ExcludeResources = false,
                    UserEmail        = req.UserEmail
                };

                var compilerService   = new CompilerService();
                var updatePageRequest = new CreateOrUpdateResourceRequestModel
                {
                    Errors          = null,
                    SourcePath      = req.SourcePath.Trim(),
                    ProjectId       = req.ProjectId,
                    UserEmail       = req.UserEmail,
                    PageType        = req._PageType,
                    ResourceType    = req._ResourceType,
                    Configuration   = !string.IsNullOrEmpty(req.Configuration) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(req.Configuration) : null,
                    ByteArrayStream = byteArrayStream
                };

                var validationResult = updatePageRequest.Validate();
                if (validationResult.Any())
                {
                    return(BadRequest(validationResult));
                }

                if (MongoConnector.CreateOrUpdateResource(updatePageRequest))
                {
                    return(Ok(new ResourceCompilationResult {
                        Success = true
                    }));
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult GetPreview(string projectId, [FromQuery] string sourcePath, [FromQuery] string dataset, [FromQuery] string userEmail)
        {
            try
            {
                if (string.IsNullOrEmpty(projectId) || string.IsNullOrEmpty(sourcePath) || string.IsNullOrEmpty(dataset) || string.IsNullOrEmpty(userEmail))
                {
                    return(BadRequest("Invalid request parameters"));
                }

                var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                {
                    ProjectId        = projectId,
                    ExcludeResources = false,
                    UserEmail        = userEmail
                };
                var projectDetails = MongoConnector.GetProjectDetails(projectDetailsRequestModel);
                var requestModel   = new GetResourceDetailsRequestModel
                {
                    ProjectId  = projectId,
                    SourcePath = sourcePath,
                    UserEmail  = userEmail
                };

                var resourceDetails = MongoConnector.GetResourceDetails(requestModel);
                var userId          = MongoConnector.GetUserIdFromUserEmail(new GetUserIdRequestModel {
                    UserEmail = userEmail
                });

                var languageEntity = MongoConnector.GetLanguageEntity(new GetLanguageEntityRequestModel
                {
                    EntityId = projectDetails.SchemaId,
                });
                var partialModel = new GetPartialPagesDetailsRequestModel
                {
                    ProjectId = projectId,
                    UserEmail = userEmail
                };

                GetPartialPagesDetailsResponseModel partialPages = MongoConnector.GetPartialPagesDetails(partialModel);

                languageEntity = new KLanguageBase().GetKitsuneLanguage(userEmail, projectId, projectDetails, languageEntity, userId.Id);

                //Compile and get the call the single page preview api
                var result = _service.GetPreviewAsync(userEmail, projectId, resourceDetails, projectDetails, languageEntity, partialPages, dataset, userId.Id);

                if (result != null)
                {
                    return(Ok(result));
                }
                return(BadRequest("Unable to get the preview"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public IActionResult UploadTemplate(string projectid, [FromBody] CompileResourceRequest req)
        {
            try
            {
                if (req == null)
                {
                    return(BadRequest("Request object can not be null"));
                }

                req.ProjectId = projectid;

                var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                {
                    ProjectId        = req.ProjectId,
                    ExcludeResources = false,
                    UserEmail        = req.UserEmail
                };

                var compileResult = CompilerHelper.CompileProjectResource(req);

                var compilerService   = new CompilerService();
                var updatePageRequest = new CreateOrUpdateResourceRequestModel
                {
                    Errors        = null,
                    FileContent   = req.FileContent,
                    SourcePath    = req.SourcePath.Trim(),
                    ClassName     = req.ClassName,
                    ProjectId     = req.ProjectId,
                    UserEmail     = req.UserEmail,
                    UrlPattern    = req.UrlPattern,
                    IsStatic      = req.IsStatic,
                    IsDefault     = req.IsDefault,
                    PageType      = req.PageType,
                    KObject       = req.KObject,
                    ResourceType  = null,
                    Configuration = !string.IsNullOrEmpty(req.Configuration) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(req.Configuration) : null,
                };

                var validationResult = updatePageRequest.Validate();
                if (validationResult.Any())
                {
                    return(BadRequest(validationResult));
                }

                if (MongoConnector.CreateOrUpdateResource(updatePageRequest))
                {
                    return(Ok(compileResult));
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult GetProjectDetails(string projectId, [FromQuery] string userEmail)
        {
            try
            {
                var requestModel = new GetProjectDetailsRequestModel
                {
                    ProjectId        = projectId,
                    ExcludeResources = false,
                    UserEmail        = userEmail
                };
                var validationResult = requestModel.Validate();
                if (validationResult.Any())
                {
                    return(BadRequest(validationResult));
                }

                var projectDetails = MongoConnector.GetProjectDetails(requestModel);
                var themePagesTree = _service.ProjectResourceTree(projectDetails.Resources.Select(x => x.SourcePath).ToList(), projectDetails.ProjectName);

                GetIDEProjectDetailsResponseModel response = new GetIDEProjectDetailsResponseModel
                {
                    Assets           = themePagesTree,
                    CreatedOn        = projectDetails.CreatedOn,
                    ProjectName      = projectDetails.ProjectName,
                    UpdatedOn        = projectDetails.UpdatedOn,
                    UserEmail        = projectDetails.UserEmail,
                    Version          = projectDetails.Version,
                    ProjectId        = projectDetails.ProjectId,
                    ArchivedOn       = projectDetails.ArchivedOn,
                    BucketNames      = projectDetails.BucketNames,
                    FaviconIconUrl   = projectDetails.FaviconIconUrl,
                    IsArchived       = projectDetails.IsArchived,
                    ProjectStatus    = projectDetails.ProjectStatus,
                    ProjectType      = projectDetails.ProjectType,
                    PublishedVersion = projectDetails.PublishedVersion,
                    SchemaId         = projectDetails.SchemaId,
                    ScreenShotUrl    = projectDetails.ScreenShotUrl,
                    Components       = projectDetails.Components
                };

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Ejemplo n.º 5
0
        public static ResourceCompilationResult CompileProjectResource(CompileResourceRequest req,
                                                                       GetPartialPagesDetailsResponseModel partialPages = null)
        {
            //Source path should always start with / (to make sure its from root folder)
            if (!string.IsNullOrEmpty(req.SourcePath))
            {
                req.SourcePath = string.Format("/{0}", req.SourcePath.Trim('/'));
            }
            else
            {
                return new ResourceCompilationResult {
                           Success = false, ErrorMessages = new List <CompilerError> {
                               new CompilerError {
                                   LineNumber = 1, LinePosition = 1, Message = "Source path can not be empty."
                               }
                           }
                }
            };

            if (!string.IsNullOrEmpty(req.FileContent))
            {
                var document = new HtmlDocument();

                CompileResult compileResult = null;
                try
                {
                    //If the file is html extension then only compile
                    if (Kitsune.Helper.Constants.DynamicFileExtensionRegularExpression.IsMatch(req.SourcePath.ToLower()))
                    {
                        try
                        {
                            var byteStream = Convert.FromBase64String(req.FileContent);
                            req.FileContent = System.Text.Encoding.UTF8.GetString(byteStream);
                        }
                        catch
                        {
                            return(new ResourceCompilationResult {
                                Success = false, ErrorMessages = new List <CompilerError> {
                                    new CompilerError {
                                        Message = "Input string is not valid base64 string"
                                    }
                                }
                            });
                        }

                        var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                        {
                            ProjectId        = req.ProjectId,
                            ExcludeResources = false,
                            UserEmail        = req.UserEmail
                        };
                        var projectDetails = MongoConnector.GetProjectDetails(projectDetailsRequestModel);
                        if (projectDetails == null)
                        {
                            return new ResourceCompilationResult
                                   {
                                       Success       = false,
                                       ErrorMessages = new List <CompilerError> {
                                           new CompilerError {
                                               Message = "Project not found."
                                           }
                                       }
                                   }
                        }
                        ;



                        var user = MongoConnector.GetUserIdFromUserEmail(new GetUserIdRequestModel
                        {
                            UserEmail = req.UserEmail
                        });
                        var languageEntity = MongoConnector.GetLanguageEntity(new GetLanguageEntityRequestModel
                        {
                            EntityId = projectDetails.SchemaId,
                        });
                        languageEntity = new KLanguageBase().GetKitsuneLanguage(req.UserEmail, req.ProjectId, projectDetails, languageEntity, user.Id);

                        #region Handle Project Components
                        var componentEntities = GetComponentReference(projectDetails.Components);
                        #endregion

                        //Compile only if the file type is supported
                        compileResult = KitsuneCompiler.CompileHTML(req, out document, projectDetails, languageEntity, partialPages, componentEntities: componentEntities);
                    }
                    //Validate the config file
                    else if (Helper.Constants.ConfigFileRegularExpression.IsMatch(req.SourcePath))
                    {
                        var byteStream = Convert.FromBase64String(req.FileContent);
                        req.FileContent = System.Text.Encoding.UTF8.GetString(byteStream);
                        compileResult   = KitsuneCompiler.ValidateJsonConfig(new ValidateConfigRequestModel
                        {
                            File = new ConfigFile {
                                Content = req.FileContent
                            },
                            UserEmail = req.UserEmail
                        });
                        req.UrlPattern = req.SourcePath;
                        req.IsStatic   = false;
                    }
                }
                catch (Exception ex)
                {
                }

                if (compileResult != null && compileResult.ErrorMessages?.Count() > 0)
                {
                    return(new ResourceCompilationResult {
                        Success = false, ErrorMessages = compileResult.ErrorMessages
                    });
                }
                else
                {
                    return new ResourceCompilationResult {
                               Success = true
                    }
                };
            }

            return(new ResourceCompilationResult {
                Success = false, ErrorMessages = new List <CompilerError> {
                    new CompilerError {
                        Message = "Input should not be empty"
                    }
                }
            });
        }