From 8d424398dd241ac3b558f2d0dc97d794da2a0f7f Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Sat, 29 Apr 2023 13:17:25 +0300 Subject: [PATCH] Add work with directories and files --- main.go | 21 ++++++++++ webfilesystem/webfilesystem.go | 71 +++++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 4767ef2..457cfdc 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "github.com/gin-gonic/gin" "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -107,6 +108,26 @@ func main() { fs := router.Group("fs") { + fs.GET("writeFile", func(ctx *gin.Context) { + parentPath := ctx.Query("parentPath") + if parentPath == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + file := webfilesystem.WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: "pp", + Type: "test", + Data: nil, + } + err := webfs.CreateFile(&file, parentPath) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + return + } + + ctx.JSON(http.StatusOK, "OK") + }) fs.GET("createDir", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index f66b837..49f59be 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/mitchellh/mapstructure" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) @@ -55,7 +56,8 @@ func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) } func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) { - dirFile, err := fs.Read(fs.GetParentPath(path)) + // dirFile, err := fs.Read(fs.GetParentPath(path)) + dirFile, err := fs.Read(path) if err != nil { return nil, err } @@ -82,24 +84,65 @@ func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) { func (fs *WebFileSystem) CreateDirectory(path string) error { splittedpath := fs.SplitPath(path) + parentPath := fs.GetParentPath(path) + parentDir, err := fs.Read(parentPath) + if err != nil { + return err + } directory := WebFSFile{ MongoId: primitive.NewObjectID(), Name: splittedpath[len(splittedpath)-1], Type: "directory", Data: FolderData{ - // Parent: , - Children: []primitive.ObjectID{ - primitive.NewObjectID(), - primitive.NewObjectID(), - primitive.NewObjectID(), - }, + Parent: parentDir.MongoId, + Children: []primitive.ObjectID{}, }, } - _, err := fs.webfsCollection.InsertOne(context.Background(), &directory) + + fs.CreateFile(&directory, parentPath) + // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) + // if err != nil { + // return err + // } + + // fileId := fs.castInsertId(res) + // fs.insertFileToDirectory(fileId, parentDir.MongoId) + return nil +} + +func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error { + parentDir, err := fs.Read(parentPath) if err != nil { return err } - // res.InsertedID //TODO Insert to parent folder data + res, err := fs.webfsCollection.InsertOne(context.Background(), &file) + if err != nil { + return err + } + _ = parentDir + fileId := fs.castInsertId(res) + fs.insertFileToDirectory(fileId, parentDir.MongoId) + return nil +} + +func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID { + return res.InsertedID.(primitive.ObjectID) +} + +func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, directoryId primitive.ObjectID) error { + dir, err := fs.ReadByObjectID(directoryId) + if err != nil { + return err + } + //TODO check if file exist + fileData := FolderData{} + err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData) + if err != nil { + return err + } + fileData.Children = append(fileData.Children, fileId) + + fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}}) return nil } @@ -145,10 +188,12 @@ type Exec struct { WebFSFile } -// func (e *Exec) GetUuid() string { -// return e.Id -// } - func (e *Exec) GetFileName() string { return e.Name } + +// type WebFSFile2 interface { +// GetName() string +// GetType() string +// GetData() interface{} +// }