personal-website/apps/blogviewer/blogviewer.go

267 lines
5.9 KiB
Go
Raw Permalink Normal View History

2023-04-14 15:53:23 +00:00
package blogviewer
import (
2023-05-23 23:09:16 +00:00
"errors"
2023-05-03 21:50:08 +00:00
"net/http"
2023-05-18 01:57:44 +00:00
"path"
2023-07-23 03:12:17 +00:00
"personalwebsite/apps"
2023-05-18 01:57:44 +00:00
"personalwebsite/apps/appCtx"
2023-05-23 23:09:16 +00:00
"personalwebsite/errormessage"
2023-06-06 22:49:26 +00:00
"personalwebsite/libs"
2023-04-29 13:58:39 +00:00
"personalwebsite/webfilesystem"
2023-04-14 15:53:23 +00:00
"github.com/gin-gonic/gin"
2023-04-29 13:58:39 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-04-14 15:53:23 +00:00
)
type BlogViewerApplication struct {
2023-07-23 03:12:17 +00:00
fs *webfilesystem.WebFileSystem
appID string
mLib libs.MarkdownLib
path string
manifest apps.ApplicationManifest
2023-04-14 15:53:23 +00:00
}
2023-04-29 13:58:39 +00:00
func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication {
2023-04-14 15:53:23 +00:00
return &BlogViewerApplication{
fs: webFs,
appID: "BlogViewer",
2023-06-06 22:49:26 +00:00
mLib: libs.MarkdownLib{},
2023-04-14 15:53:23 +00:00
}
}
2023-07-23 03:12:17 +00:00
func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest {
return b.manifest
}
2023-04-14 15:53:23 +00:00
func (b *BlogViewerApplication) GetAppID() string {
return b.appID
2023-04-14 15:53:23 +00:00
}
2023-07-23 03:12:17 +00:00
func (b *BlogViewerApplication) GetPath() string {
return b.path
}
func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) {
b.PublicRoutes(route)
2023-05-23 23:09:16 +00:00
2023-06-06 22:49:26 +00:00
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)
2023-06-07 00:42:50 +00:00
mLib.Render(test, true)
2023-06-06 22:49:26 +00:00
}
}
})
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")
})
2023-05-23 23:09:16 +00:00
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)
})
2023-04-14 15:53:23 +00:00
}
2023-06-06 22:49:26 +00:00
func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) {
2023-05-03 21:50:08 +00:00
2023-05-18 01:57:44 +00:00
route.POST("render", func(ctx *gin.Context) {
2023-05-03 21:50:08 +00:00
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
2023-05-18 01:57:44 +00:00
appCtx := appCtx.AppContext{}
err := ctx.BindJSON(&appCtx)
if err != nil {
ctx.Status(http.StatusBadRequest)
return
}
ginH, err := b.Render(path, appCtx)
2023-05-03 21:50:08 +00:00
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO")
return
}
2023-05-18 01:57:44 +00:00
if appCtx.IsMobile {
2023-05-03 21:50:08 +00:00
ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", ginH)
}
})
}
2023-05-07 01:00:22 +00:00
func (b *BlogViewerApplication) WriteMock(path string) error {
2023-05-18 01:57:44 +00:00
blogFileHeader := webfilesystem.FileHeader{ //TODO to fs.CreateDirectory()
2023-05-07 01:00:22 +00:00
MongoId: primitive.NewObjectID(),
Name: "blog1.blog",
2023-05-18 01:57:44 +00:00
Type: "directory",
2023-05-07 01:00:22 +00:00
Icon: "",
Data: [12]byte{},
}
2023-05-18 01:57:44 +00:00
blogFileData := webfilesystem.DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: [12]byte{},
Children: []primitive.ObjectID{},
2023-05-07 01:00:22 +00:00
}
_, _, err := b.fs.Write("/home/user/blog1.blog", &blogFileHeader, blogFileData)
2023-04-29 13:58:39 +00:00
if err != nil {
println(err.Error())
2023-05-07 01:00:22 +00:00
return err
2023-04-29 13:58:39 +00:00
}
2023-05-18 01:57:44 +00:00
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
}
2023-05-07 01:00:22 +00:00
return nil
}
2023-05-23 23:09:16 +00:00
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
}
2023-05-18 01:57:44 +00:00
func (b *BlogViewerApplication) Render(filePath string, appCtx appCtx.AppContext) (gin.H, error) {
data := &BlogFileData{}
_, err := b.fs.Read(path.Join(filePath, ".content"), &data)
2023-05-07 01:00:22 +00:00
if err != nil {
return nil, err
2023-04-14 15:53:23 +00:00
}
2023-04-29 13:58:39 +00:00
2023-06-06 23:17:29 +00:00
newData := []RenderedBlock{}
2023-05-18 01:57:44 +00:00
for _, block := range data.Blocks {
2023-06-06 23:17:29 +00:00
newBlock := RenderedBlock{}
2023-06-06 22:49:26 +00:00
switch block.Type {
case "image":
2023-06-06 23:17:29 +00:00
// for _, image := range block.Data {
// newData = append(newData, b.fs.RelativeToAbsolute(appCtx, image))
// }
2023-06-06 22:49:26 +00:00
case "markdown":
for _, data := range block.Data {
2023-06-07 00:42:50 +00:00
renderedMD := b.mLib.Render([]byte(data), true)
2023-06-06 23:17:29 +00:00
newBlock.Data = append(newBlock.Data, renderedMD)
2023-06-06 22:49:26 +00:00
}
2023-06-06 23:17:29 +00:00
newData = append(newData, newBlock)
2023-05-18 01:57:44 +00:00
}
2023-06-06 23:17:29 +00:00
// block.Data = newData
2023-05-18 01:57:44 +00:00
}
2023-06-06 22:49:26 +00:00
2023-04-29 13:58:39 +00:00
return gin.H{
2023-05-07 01:00:22 +00:00
"header": data.Header,
2023-06-06 23:17:29 +00:00
"blocks": newData,
2023-06-06 22:49:26 +00:00
// "html": html,
2023-04-29 13:58:39 +00:00
}, nil
2023-04-14 15:53:23 +00:00
}