Add work with directories and files

This commit is contained in:
cyber-dream 2023-04-29 13:17:25 +03:00
parent 38e4058840
commit 8d424398dd
2 changed files with 79 additions and 13 deletions

21
main.go
View File

@ -16,6 +16,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
@ -107,6 +108,26 @@ func main() {
fs := router.Group("fs") 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) { fs.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path") path := ctx.Query("path")
if path == "" { if path == "" {

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "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) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -82,24 +84,65 @@ func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
func (fs *WebFileSystem) CreateDirectory(path string) error { func (fs *WebFileSystem) CreateDirectory(path string) error {
splittedpath := fs.SplitPath(path) splittedpath := fs.SplitPath(path)
parentPath := fs.GetParentPath(path)
parentDir, err := fs.Read(parentPath)
if err != nil {
return err
}
directory := WebFSFile{ directory := WebFSFile{
MongoId: primitive.NewObjectID(), MongoId: primitive.NewObjectID(),
Name: splittedpath[len(splittedpath)-1], Name: splittedpath[len(splittedpath)-1],
Type: "directory", Type: "directory",
Data: FolderData{ Data: FolderData{
// Parent: , Parent: parentDir.MongoId,
Children: []primitive.ObjectID{ Children: []primitive.ObjectID{},
primitive.NewObjectID(),
primitive.NewObjectID(),
primitive.NewObjectID(),
},
}, },
} }
_, 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 { if err != nil {
return err 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 return nil
} }
@ -145,10 +188,12 @@ type Exec struct {
WebFSFile WebFSFile
} }
// func (e *Exec) GetUuid() string {
// return e.Id
// }
func (e *Exec) GetFileName() string { func (e *Exec) GetFileName() string {
return e.Name return e.Name
} }
// type WebFSFile2 interface {
// GetName() string
// GetType() string
// GetData() interface{}
// }