package webfilesystem import ( "context" "errors" "strings" "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 webfsFilesTable *mongo.Collection webfsFilesData *mongo.Collection 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 webfsFilesTable: mongoClient.Database(dBName).Collection("webfs-table"), // TODO Check collection is exist, //FIXME webfsFilesData: mongoClient.Database(dBName).Collection("webfs-data"), // TODO Check collection is exist, //FIXME ctx: context.Background(), } } // Deprecated func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) { filter := primitive.D{ { Key: "_id", 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{} err := res.Decode(&file) if err != nil { return nil, err } return &file, 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 { read := ReadStruct2{ File: DirectoryFile{}, Id: directoryId, } parentDirRaw, err := fs.Read(read) if err != nil { return err } var dirPtr interface{} = *parentDirRaw parentDir := dirPtr.(*DirectoryFile) parentDir.Data.Children = append(parentDir.Data.Children, fileId) res, err := fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data.children": parentDir.Data.Children}}) if err != nil { return err } if res.ModifiedCount < 1 { return errors.New("no parent folders edited") } 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 { splittedName := strings.Split(filename, ".") return splittedName[len(splittedName)-1] } func (fs *WebFileSystem) GetParentPath(path string) string { splittedPath := fs.SplitPath(path) if len(splittedPath) > 1 { return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/") } return "/" } // Deprecated func (fs *WebFileSystem) Delete(filePath string) error { // splittedPath := fs.SplitPath(filePath) // parentPath := fs.GetParentPath(filePath) _, err := fs.NewReadDeprecated(filePath) if err != nil { return err } err = fs.removeMongo(filePath) if err != nil { return err } 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.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 } type WebFSFile struct { MongoId primitive.ObjectID `bson:"_id" json:"-"` Name string `bson:"name" json:"name"` Type string `bson:"type" json:"type"` Icon string `bson:"-" json:"icon"` Data interface{} `bson:"data" json:"-"` }