From 5736b8de311f1078105df295fa2bd4add2c9dd1c Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Fri, 5 May 2023 21:32:41 +0300 Subject: [PATCH] File Deleting and new decoding --- apps/finder/finder.go | 2 +- resources/sys/finder/finder.js | 38 +++-- webfilesystem/directory.go | 66 ++++++-- webfilesystem/fileuploading.go | 8 +- webfilesystem/mongo.go | 57 +++++++ webfilesystem/newmethods.go | 31 +--- webfilesystem/webfilesystem.go | 274 +++++++++------------------------ 7 files changed, 217 insertions(+), 259 deletions(-) diff --git a/apps/finder/finder.go b/apps/finder/finder.go index e1bc81e..6399b67 100644 --- a/apps/finder/finder.go +++ b/apps/finder/finder.go @@ -49,7 +49,7 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H { { Label: "New Directory", - Action: strings.Join([]string{"newDir"}[:], ","), + Action: strings.Join([]string{"createDir"}[:], ","), }, }, } diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index 1795815..e8d7a60 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -155,17 +155,7 @@ class Finder{ case "blog-page": WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`]) break - case "deleteFile": - fetch(`/fs/delete` + new URLSearchParams({ - path: `${this.path}/${fileName}` //FIXME - })) - .then((response) => { - console.log(response.status) - }) - .catch((error) => { - WebDesktopEnvironment.Alert(error); - }) - break + // case "app": // //TODO get real id // WebDesktopEnvironment.Open("personal-properties", []) @@ -213,7 +203,33 @@ class Finder{ overlay.addEventListener('click',(event) => { if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks?? + let fileType = target.getAttribute("fileType") switch (event.target.children[0].getAttribute("action")) { + case "createDir": + fetch(`/fs/createDir?` + new URLSearchParams({ + path: `${this.path}/New Directory` + })) + .then((response) => { + console.log(response.status) + }) + .catch((error) => { + WebDesktopEnvironment.Alert(error); + }) + break + case "deleteFile": + let fileName = target.getAttribute("name") + console.log(fileName) + // break + fetch(`/fs/delete?` + new URLSearchParams({ + path: `${this.path}/${fileName}` //FIXME + })) + .then((response) => { + console.log(response.status) + }) + .catch((error) => { + WebDesktopEnvironment.Alert(error); + }) + break default: break; } diff --git a/webfilesystem/directory.go b/webfilesystem/directory.go index efd72e4..3bc52f6 100644 --- a/webfilesystem/directory.go +++ b/webfilesystem/directory.go @@ -1,7 +1,11 @@ package webfilesystem import ( + "context" + "strconv" + "github.com/mitchellh/mapstructure" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -10,22 +14,66 @@ type DirectoryData struct { Children []primitive.ObjectID `bson:"children"` } -// type Directory struct { -// Header Header `bson:"header"` -// Data DirectoryData `bson:"data"` -// } +func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) { + splittedPath := fs.SplitPath(dirPath) + parentPath := fs.GetParentPath(dirPath) -// func (d *Directory) Get() interface{} { -// return d -// } + parentDirRaw, err := fs.NewRead(parentPath) + if err != nil { + return primitive.NilObjectID, err + } + + newDir := WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: splittedPath[len(splittedPath)-1], + Type: "directory", + Data: DirectoryData{ + Parent: parentDirRaw.MongoId, + Children: []primitive.ObjectID{}, + }, + Icon: "", + } + objectId, err := fs.writeMongo(newDir, parentPath) + if err != nil { + return primitive.NilObjectID, err + } + + return objectId, nil +} + +func (fs *WebFileSystem) validateDir(dir *WebFSFile) error { + kek := dir.Data.(primitive.D).Map()["children"].(primitive.A) + _ = kek + + children := []primitive.ObjectID{} + counter := 0 + for _, v := range kek { + _, err := fs.ReadByObjectID(v.(primitive.ObjectID)) + if err != nil { + counter++ + } else { + children = append(children, v.(primitive.ObjectID)) + } + } + if counter > 0 { + println(dir.Name + " broken iDs: " + strconv.Itoa(counter)) + _, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}}) + if err != nil { + println(err.Error()) + return err + } + } + + return nil +} func castToFile(raw *interface{}) *WebFSFile { var dirPtr interface{} = *raw return dirPtr.(*WebFSFile) } -func castToDirectory(data interface{}) (*FolderData, error) { - dirData := FolderData{} +func castToDirectoryData(data interface{}) (*DirectoryData, error) { + dirData := DirectoryData{} err := mapstructure.Decode(data.(primitive.D).Map(), &dirData) if err != nil { return nil, err diff --git a/webfilesystem/fileuploading.go b/webfilesystem/fileuploading.go index 2448498..530e04f 100644 --- a/webfilesystem/fileuploading.go +++ b/webfilesystem/fileuploading.go @@ -29,7 +29,7 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error { Data: file, Icon: "", } - err := fs.CreateFile(&newFile, path) + err := fs.WriteFile(&newFile, path) return err case "png": newFile := WebFSFile{ @@ -38,7 +38,7 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error { Type: "png", Data: file, } - err := fs.CreateFile(&newFile, path) + err := fs.WriteFile(&newFile, path) return err default: return errors.New("this filetype not allowed") @@ -58,7 +58,7 @@ func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path str Data: file, Icon: "", } - err := fs.CreateFile(&newFile, path) + err := fs.WriteFile(&newFile, path) return err case "png": newFile := WebFSFile{ @@ -67,7 +67,7 @@ func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path str Type: "png", Data: file, } - err := fs.CreateFile(&newFile, path) + err := fs.WriteFile(&newFile, path) return err default: return errors.New("this filetype not allowed") diff --git a/webfilesystem/mongo.go b/webfilesystem/mongo.go index 67d1869..84b0074 100644 --- a/webfilesystem/mongo.go +++ b/webfilesystem/mongo.go @@ -2,6 +2,7 @@ package webfilesystem import ( "context" + "errors" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -35,3 +36,59 @@ func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitiv return fileId, nil } + +func (fs *WebFileSystem) removeMongo(filePath string) error { + file, err := fs.NewRead(filePath) + if err != nil { + return err + } + + if file.MongoId == primitive.NilObjectID { + return errors.New("TODO") //TODO + } + filter := primitive.M{ + "_id": file.MongoId, + } + res, err := fs.webfsCollection.DeleteOne(fs.ctx, filter) + if err != nil { + return err + } + + if res.DeletedCount < 1 { + return errors.New("no file removed") + } + + return nil +} + +func (fs *WebFileSystem) Validate() error { + filter := primitive.D{ + { + Key: "type", + Value: "directory", + }, + } + cur, err := fs.webfsCollection.Find(context.Background(), filter) + if err != nil { + return err + } + defer cur.Close(context.Background()) + + directories := []*WebFSFile{} + + for cur.Next(context.Background()) { + dir := &WebFSFile{} + + err = cur.Decode(dir) + if err != nil { + println(err.Error()) + return err + } + directories = append(directories, dir) + } + + for _, d := range directories { + fs.validateDir(d) + } + return nil +} diff --git a/webfilesystem/newmethods.go b/webfilesystem/newmethods.go index 153085b..f863082 100644 --- a/webfilesystem/newmethods.go +++ b/webfilesystem/newmethods.go @@ -24,33 +24,6 @@ func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) { return file, nil } -func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) { - splittedPath := fs.SplitPath(dirPath) - parentPath := fs.GetParentPath(dirPath) - - parentDirRaw, err := fs.NewRead(parentPath) - if err != nil { - return primitive.NilObjectID, err - } - - newDir := WebFSFile{ - MongoId: primitive.NewObjectID(), - Name: splittedPath[len(splittedPath)-1], - Type: "directory", - Data: FolderData{ - Parent: parentDirRaw.MongoId, - Children: []primitive.ObjectID{}, - }, - Icon: "", - } - objectId, err := fs.writeMongo(newDir, parentPath) - if err != nil { - return primitive.NilObjectID, err - } - - return objectId, nil -} - func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) { dirFile, err := fs.NewRead(dirPath) if err != nil { @@ -60,7 +33,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) { return nil, errors.New("file is not a directory") } - fileData, err := castToDirectory(dirFile.Data) + fileData, err := castToDirectoryData(dirFile.Data) if err != nil { return nil, err } @@ -77,7 +50,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) { return files, nil } -func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error { +func (fs *WebFileSystem) WriteFile(file *WebFSFile, parentPath string) error { fs.writeMongo(*file, parentPath) return errors.New("Not ready yet") } diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 70fbbc5..9d79468 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -2,8 +2,6 @@ package webfilesystem import ( "context" - "errors" - "strconv" "strings" "github.com/mitchellh/mapstructure" @@ -14,8 +12,7 @@ import ( type WebFileSystem struct { webfsCollection *mongo.Collection - // folders []*Folder - ctx context.Context + ctx context.Context } func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem { @@ -25,18 +22,6 @@ func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName } } -// func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) { -// splittedPath := fs.SplitPath(path) -// filter := primitive.D{ -// { -// Key: "name", -// Value: splittedPath[len(splittedPath)-1], -// }, -// } -// file, err := fs.findFileInMongo(filter) -// return file, err -// } - func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) { filter := primitive.D{ { @@ -44,10 +29,12 @@ func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile Value: objectId, }, } + //TODO to readMongo() file, err := fs.findFileInMongo(filter) return file, err } +// Deprecated func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) { res := fs.webfsCollection.FindOne(context.Background(), &filter) file := WebFSFile{} @@ -58,94 +45,6 @@ func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) return &file, nil } -// func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) { -// // dirFile, err := fs.Read(fs.GetParentPath(path)) -// dirFile, err := fs.Read(path) -// if err != nil { -// return nil, err -// } -// if dirFile.Type != "directory" { -// return nil, errors.New("file is not a directory") -// } -// fileData := FolderData{} -// err = mapstructure.Decode(dirFile.Data.(primitive.D).Map(), &fileData) -// if err != nil { -// return nil, err -// } -// files := []*WebFSFile{} -// for _, child := range fileData.Children { -// file, err := fs.ReadByObjectID(child) -// if err != nil { -// println(err.Error()) -// continue -// } -// files = append(files, file) -// } -// return files, nil -// } - -// 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: parentDir.MongoId, -// Children: []primitive.ObjectID{}, -// }, -// } - -// fs.CreateFile(&directory, parentPath) -// // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO -// // if err != nil { -// // return err -// // } - -// // fileId := fs.castInsertId(res) -// // fs.insertFileToDirectory(fileId, parentDir.MongoId) -// return nil -// } - -// func (fs *WebFileSystem) UpdateFile(filePath string, update primitive.M) error { -// file, err := fs.Read(filePath) -// if err != nil { -// return err -// } -// if file.MongoId.IsZero() { -// return errors.New("mongo id is zero") -// } -// filter := primitive.M{ -// "_id": file.MongoId, -// } -// _, err = fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$set": update}) -// if err != nil { -// return err -// } -// return nil -// } - -// func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error { -// //TODO Check file existance -// parentDir, err := fs.Read(parentPath) -// if err != nil { -// return err -// } -// 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) } @@ -156,7 +55,7 @@ func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, direct return err } //TODO check if file exist - fileData := FolderData{} + fileData := DirectoryData{} err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData) if err != nil { return err @@ -180,7 +79,6 @@ func (fs *WebFileSystem) SplitPath(path string) []string { } func (fs *WebFileSystem) GetExtension(filename string) string { - // extension := []string{} splittedName := strings.Split(filename, ".") return splittedName[len(splittedName)-1] @@ -193,125 +91,91 @@ func (fs *WebFileSystem) GetParentPath(path string) string { } func (fs *WebFileSystem) Delete(filePath string) error { - splittedPath := fs.SplitPath(filePath) - parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") + // splittedPath := fs.SplitPath(filePath) + // parentPath := fs.GetParentPath(filePath) - file, err := fs.NewRead(filePath) + _, err := fs.NewRead(filePath) if err != nil { return err } - parentDir, err := fs.NewRead(parentPath) + err = fs.removeMongo(filePath) if err != nil { return err } - parentDir.Data.(primitive.D).Map() + err = fs.Validate() //FIXME + if err != nil { + return err + } + + //Delete from parent folder + // parentDir, err := fs.NewRead(parentPath) + // if err != nil { + // return err + // } + + // parentDirData, err := castToDirectoryData(parentDir.Data) + // if err != nil { + // return err + // } + + // newChildrenSlice := []primitive.ObjectID{} + // for i := 0; i < len(parentDirData.Children); i++ { + // if parentDirData.Children[i] != file.MongoId { + // newChildrenSlice = append(newChildrenSlice, parentDirData.Children[i]) + // } + // } + + // parentDirData.Children = newChildrenSlice + // parentDir.Data = parentDirData + // parentParentDir := fs.GetParentPath(parentPath) + + // _, err = fs.writeMongo(*parentDir, parentParentDir) + // if err != nil { + // return err + // } + + return nil + + // for _, childId := range parentDirData.Children { + // // read := readStruct{ + // // File: filePath, + // // Filter: primitive.M{ + // // "_id": childId, + // // }, + // // } + // // childFile, err := fs.ReadByObjectID(childId) + // // if err != nil { + // // println(err.Error()) + // // continue + // // } + // // println(childFile.Name + " " + chil) + // if file.MongoId == childId { + // println(file.Name) + // parentDirData.Children. + // } + // } // update:= primitive.M{ // "data.children": // } // filter := primitive.M{} - res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) + // res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) // res, err := fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) - if err != nil { - return err - } - if res.MatchedCount < 1 { - return errors.New("no documents found") - } - return nil -} - -func (fs *WebFileSystem) Validate() error { - filter := primitive.D{ - { - Key: "type", - Value: "directory", - }, - } - cur, err := fs.webfsCollection.Find(context.Background(), filter) - if err != nil { - return err - } - defer cur.Close(context.Background()) - - directories := []*WebFSFile{} - - for cur.Next(context.Background()) { - dir := &WebFSFile{} - - err = cur.Decode(dir) - if err != nil { - println(err.Error()) - return err - } - directories = append(directories, dir) - } - - for _, d := range directories { - fs.validateDir(d) - } - return nil -} - -func (fs *WebFileSystem) validateDir(dir *WebFSFile) error { - kek := dir.Data.(primitive.D).Map()["children"].(primitive.A) - _ = kek - - children := []primitive.ObjectID{} - counter := 0 - for _, v := range kek { - _, err := fs.ReadByObjectID(v.(primitive.ObjectID)) - if err != nil { - counter++ - } else { - children = append(children, v.(primitive.ObjectID)) - } - } - if counter > 0 { - println(dir.Name + " broken iDs: " + strconv.Itoa(counter)) - _, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}}) - if err != nil { - println(err.Error()) - return err - } - } - - return nil + // if err != nil { + // return err + // } + // if res.MatchedCount < 1 { + // return errors.New("no documents found") + // } + // return nil } type WebFSFile struct { MongoId primitive.ObjectID `bson:"_id" json:"-"` Name string `bson:"name" json:"name"` Type string `bson:"type" json:"type"` - Data interface{} `bson:"data" json:"-"` Icon string `bson:"-" json:"icon"` + Data interface{} `bson:"data" json:"-"` } - -type FolderData struct { - Parent primitive.ObjectID `bson:"parent"` - Children []primitive.ObjectID `bson:"children"` -} - -type File interface { - GetUuid() string - GetFileName() string -} - -type Image struct { -} - -type Exec struct { - WebFSFile -} - -func (e *Exec) GetFileName() string { - return e.Name -} - -// type WebFSFile2 interface { -// GetName() string -// GetType() string -// GetData() interface{} -// }