package webfilesystem import ( "context" "errors" "strconv" "strings" "github.com/mitchellh/mapstructure" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) type WebFileSystem struct { webfsCollection *mongo.Collection // folders []*Folder ctx context.Context } func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem { return &WebFileSystem{ webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist ctx: context.Background(), } } // 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{ { Key: "_id", Value: objectId, }, } file, err := fs.findFileInMongo(filter) return file, err } func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) { res := fs.webfsCollection.FindOne(context.Background(), &filter) file := WebFSFile{} err := res.Decode(&file) if err != nil { return nil, err } 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) } 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 } func (fs *WebFileSystem) SplitPath(path string) []string { resPath := []string{} splittedPath := strings.Split(path, "/") splittedPath[0] = "/" for _, split := range splittedPath { if split != "" { resPath = append(resPath, split) } } return resPath } func (fs *WebFileSystem) GetExtension(filename string) string { // extension := []string{} splittedName := strings.Split(filename, ".") return splittedName[len(splittedName)-1] } func (fs *WebFileSystem) GetParentPath(path string) string { splittedPath := fs.SplitPath(path) parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") return parentPath } func (fs *WebFileSystem) Delete(filePath string) error { splittedPath := fs.SplitPath(filePath) parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") file, err := fs.NewRead(filePath) if err != nil { return err } parentDir, err := fs.NewRead(parentPath) if err != nil { return err } parentDir.Data.(primitive.D).Map() // 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.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 } 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"` } 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{} // }