package webfilesystem import ( "errors" "go.mongodb.org/mongo-driver/bson/primitive" ) //TODO Transparently split to single data and header reading methods func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) { fileHeader := &FileHeader{} filter := primitive.M{ "_id": fileID, } err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader) if err != nil { return nil, err } if fileData != nil { filter := primitive.M{ "_id": fileHeader.Data, } err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData) if err != nil { return nil, err } } return fileHeader, nil } func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data) if err != nil { return primitive.NilObjectID, primitive.NilObjectID, err } file.Data = resData.InsertedID.(primitive.ObjectID) resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file) if err != nil { return primitive.NilObjectID, primitive.NilObjectID, err } return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err } func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error { fileHeader, err := fs.readFSDocs(fileId, nil) if err != nil { return err } filterHeader := primitive.M{ "_id": fileHeader.MongoId, } res, err := fs.webfsFilesTable.DeleteOne(fs.ctx, filterHeader) if err != nil { return err } if res.DeletedCount < 1 { return errors.New("no file header deleted") } filterData := primitive.M{ "_id": fileHeader.Data, } res, err = fs.webfsFilesData.DeleteOne(fs.ctx, filterData) if err != nil { return err } if res.DeletedCount < 1 { return errors.New("no file data deleted") } return nil } //Deprecated 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 }