package blogviewer import ( "errors" "net/http" "path" "personalwebsite/apps" "personalwebsite/apps/appCtx" "personalwebsite/errormessage" "personalwebsite/libs" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) type BlogViewerApplication struct { fs *webfilesystem.WebFileSystem appID string mLib libs.MarkdownLib path string manifest apps.ApplicationManifest } func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ fs: webFs, appID: "BlogViewer", mLib: libs.MarkdownLib{}, } } func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest { return b.manifest } func (b *BlogViewerApplication) GetAppID() string { return b.appID } func (b *BlogViewerApplication) GetPath() string { return b.path } func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) { b.PublicRoutes(route) route.POST("test", func(ctx *gin.Context) { blogData := BlogFileData{} err := ctx.BindJSON(&blogData) if err != nil { ctx.Status(http.StatusBadRequest) return } mLib := libs.MarkdownLib{} for _, block := range blogData.Blocks { for _, str := range block.Data { test := []byte(str) mLib.Render(test, true) } } }) route.GET("writeMockBlog", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { ctx.JSON(http.StatusBadRequest, "no path provided") return } err := b.WriteMock(path) if err != nil { ctx.Status(http.StatusInternalServerError) return } ctx.JSON(http.StatusOK, "OK") }) route.POST("edit", func(ctx *gin.Context) { filePath := ctx.Query("path") if filePath == "" { ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{ Message: "File path is empty", }) } blogData := BlogFileData{} err := ctx.BindJSON(&blogData) if err != nil { ctx.Status(http.StatusBadRequest) return } err = b.Edit(filePath, blogData) if err != nil { ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{ Message: err.Error(), }) return } ctx.Status(http.StatusOK) }) route.GET("read", func(ctx *gin.Context) { filePath := ctx.Query("path") if filePath == "" { ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{ Message: "File path is empty", }) } blogData, err := b.Read(filePath) if err != nil { ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{ Message: err.Error(), }) return } ctx.JSON(http.StatusOK, blogData) }) } func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) { route.POST("render", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { ctx.JSON(http.StatusBadRequest, "no path provided") return } appCtx := appCtx.AppContext{} err := ctx.BindJSON(&appCtx) if err != nil { ctx.Status(http.StatusBadRequest) return } ginH, err := b.Render(path, appCtx) if err != nil { ctx.JSON(http.StatusInternalServerError, "TODO") return } if appCtx.IsMobile { ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", ginH) } else { ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", ginH) } }) } func (b *BlogViewerApplication) WriteMock(path string) error { blogFileHeader := webfilesystem.FileHeader{ //TODO to fs.CreateDirectory() MongoId: primitive.NewObjectID(), Name: "blog1.blog", Type: "directory", Icon: "", Data: [12]byte{}, } blogFileData := webfilesystem.DirectoryData{ MongoId: primitive.NewObjectID(), Parent: [12]byte{}, Children: []primitive.ObjectID{}, } _, _, err := b.fs.Write("/home/user/blog1.blog", &blogFileHeader, blogFileData) if err != nil { println(err.Error()) return err } blogContentFileHeader := webfilesystem.FileHeader{ MongoId: primitive.NewObjectID(), Name: ".content", Type: "blog-content", Icon: "", Data: [12]byte{}, } blogContentFileData := BlogFileData{ Header: "OMG THIS IS BLOG", } blogContentFileData.Blocks = append(blogContentFileData.Blocks, &Block{ Type: "plain-text", Data: []string{ "Apoqiwepoqiwepo", ".,mas;dakls;d", "q[poqwieqpipoi]", }, }) _, _, err = b.fs.Write("/home/user/blog1.blog/.content", &blogContentFileHeader, blogContentFileData) if err != nil { println(err.Error()) return err } return nil } func (b *BlogViewerApplication) Read(filePath string) (*BlogFileData, error) { fileData := BlogFileData{} fileHeader, err := b.fs.Read(path.Join(filePath, ".content"), &fileData) if err != nil { return nil, err } if fileHeader.Type != "blog-content" { return nil, errors.New("wrong file type") } return &fileData, nil } func (b *BlogViewerApplication) Edit(filePath string, blogData BlogFileData) error { contentPath := path.Join(filePath, ".content") fileHeader, err := b.fs.Read(contentPath, nil) if err != nil { return err } if fileHeader.Type != "blog-content" { return errors.New("wrong file type") } err = b.fs.Remove(contentPath) if err != nil { return err } fileHeader.MongoId = primitive.NewObjectID() _, _, err = b.fs.Write(contentPath, fileHeader, blogData) if err != nil { return err } return nil } func (b *BlogViewerApplication) Render(filePath string, appCtx appCtx.AppContext) (gin.H, error) { data := &BlogFileData{} _, err := b.fs.Read(path.Join(filePath, ".content"), &data) if err != nil { return nil, err } newData := []RenderedBlock{} for _, block := range data.Blocks { newBlock := RenderedBlock{} switch block.Type { case "image": // for _, image := range block.Data { // newData = append(newData, b.fs.RelativeToAbsolute(appCtx, image)) // } case "markdown": for _, data := range block.Data { renderedMD := b.mLib.Render([]byte(data), true) newBlock.Data = append(newBlock.Data, renderedMD) } newData = append(newData, newBlock) } // block.Data = newData } return gin.H{ "header": data.Header, "blocks": newData, // "html": html, }, nil }