From 451486f44b56107671fda79bf941f15450c190cb Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Tue, 25 Apr 2023 12:37:42 +0300 Subject: [PATCH] Add real webfs code --- main.go | 85 +++++++++++++++++++++++++++++++++- routewde/wde.go | 2 +- webfilesystem/webfilesystem.go | 70 +++++++++++++++++----------- 3 files changed, 129 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index 12deafd..d25d344 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "context" + "errors" "log" "net/http" + "os" "personalwebsite/routewde" + "personalwebsite/webfilesystem" "personalwebsite/websiteapp" "personalwebsite/websiteapp/blogviewer" "personalwebsite/websiteapp/finder" @@ -11,11 +15,43 @@ import ( "personalwebsite/websiteapp/personalprops" "github.com/gin-gonic/gin" + "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) func main() { - router := gin.New() + if err := godotenv.Load(); err != nil { + println("No .env file found") + } + + mongoConnect, err := FindEnv("MONGO_CONNECT") + if err != nil { + panic(err.Error()) + } + + dBName, err := FindEnv("DATABASE") + if err != nil { + panic(err.Error()) + } + + webFsCollection, err := FindEnv("COLLECTION_WEBFS") + if err != nil { + panic(err.Error()) + } + + clientOptions := options.Client().ApplyURI(mongoConnect) + client, err := mongo.Connect(context.TODO(), clientOptions) + if err != nil { + log.Fatal(err) + } + err = client.Ping(context.TODO(), nil) + if err != nil { + log.Fatal(err) + } + + router := gin.New() router.LoadHTMLGlob("templates/**/*") router.Static("/res", "resources") @@ -23,6 +59,7 @@ func main() { ctx.HTML(http.StatusOK, "index.tmpl", gin.H{}) }) + webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection) persPropsApp := personalprops.NewPersPropsApp() finderApp := finder.FinerApplication{} imgViewerApp := imgviewer.NewImgViewerApp() @@ -68,6 +105,40 @@ func main() { websiteapp.Route(apps.Group("/storage"), &appsStorage) } + fs := router.Group("fs") + { + fs.GET("list", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + files, err := webfs.List(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, &files) + }) + + fs.GET("read", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + file, err := webfs.Read(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, &file) + }) + } app := router.Group("application") { persPropApp := app.Group("personal-properties") @@ -137,3 +208,15 @@ func main() { func index(c *gin.Context) { c.HTML(http.StatusOK, "base.html", nil) } + +func FindEnv(parameter string) (string, error) { + path, exists := os.LookupEnv(parameter) + + if exists { + println("[ENV] Requsted " + parameter + " = " + path) //FIXME Only in Debug log + return path, nil + } else { + panic("[ENV] Requsted " + parameter + " not found") + return "", errors.New("env parameter not found") + } +} diff --git a/routewde/wde.go b/routewde/wde.go index 49e141e..118fcac 100644 --- a/routewde/wde.go +++ b/routewde/wde.go @@ -20,7 +20,7 @@ func Route(route *gin.RouterGroup) { { widgets.GET("/file-tile-view", func(ctx *gin.Context) { fs := webfilesystem.WebFileSystem{} - list := fs.List() + list, _ := fs.List("/") //TODO check errors ctx.HTML(http.StatusOK, "wde-widgets/file-tile-view.tmpl", gin.H{ "Name": "Greg Brzezinski", "Files": list, diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 27e968c..842c733 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -1,37 +1,55 @@ package webfilesystem -import "github.com/google/uuid" +import ( + "context" + "strings" + + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" +) type WebFileSystem struct { - folders []*Folder + webfsCollection *mongo.Collection + // folders []*Folder } -func (fs *WebFileSystem) List() []*WebFSFile { - // aboutMe := &Exec{ - // WebFSFile: WebFSFile{ - // Id: uuid.NewString(), - // FileName: "Aboutme.app", - // }, - // } - // testImg := - // mockFolder := Folder{ - // files: []File{aboutMe}, - // } - // kek := []*WebFSFile{} - return []*WebFSFile{{uuid.NewString(), "Aboutme", "app", "personal-properties"}, {uuid.NewString(), "lel.lol", "lol", nil}, {uuid.NewString(), "lel.img", "img", nil}} - // return mockFolder.files +func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem { + return &WebFileSystem{ + webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist + } } -type Folder struct { - files []File +func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) { + return nil, nil +} + +func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) { + splittedPath := strings.Split(path, "/") + filter := primitive.D{ + { + Key: "filename", + Value: splittedPath[len(splittedPath)-1], + }, + } + if splittedPath == []string{"",""} + res := fs.webfsCollection.FindOne(context.TODO(), &filter) + findedFile := WebFSFile{} + err := res.Decode(&findedFile) + return &findedFile, err } type WebFSFile struct { - Id string - FileName string - Type string - data interface{} + MongoId primitive.ObjectID `bson:"_id"` + Name string `bson:"name"` + Type string `bson:"type"` + data interface{} `bson:"data"` } + +type FolderData struct { + Parent primitive.ObjectID `bson:"parent"` + Childs []primitive.ObjectID `bson:"childs"` +} + type File interface { GetUuid() string GetFileName() string @@ -44,10 +62,10 @@ type Exec struct { WebFSFile } -func (e *Exec) GetUuid() string { - return e.Id -} +// func (e *Exec) GetUuid() string { +// return e.Id +// } func (e *Exec) GetFileName() string { - return e.FileName + return e.Name }