package blogviewer import ( "net/http" "path" "personalwebsite/apps/appCtx" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) type BlogViewerApplication struct { fs *webfilesystem.WebFileSystem appID string } func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ fs: webFs, appID: "BlogViewer", } } func (b *BlogViewerApplication) GetAppID() string { return b.appID } func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) { b.PublicRoutes(route) } func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) { 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("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) 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 } for _, block := range data.Blocks { if block.Type == "image" { newData := []string{} for _, image := range block.Data { newData = append(newData, b.fs.RelativeToAbsolute(appCtx, image)) } block.Data = newData } } return gin.H{ "header": data.Header, "blocks": data.Blocks, }, nil } type Block struct { Type string `bson:"type"` Data []string `bson:"data"` }