personal-website/webfilesystem/mongo.go

78 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-05-05 14:44:03 +00:00
package webfilesystem
import (
2023-05-05 18:32:41 +00:00
"errors"
2023-05-05 14:44:03 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
)
2023-05-07 01:00:22 +00:00
func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) {
fileHeader := &FileHeader{}
filter := primitive.M{
"_id": fileID,
2023-05-05 14:44:03 +00:00
}
2023-05-07 01:00:22 +00:00
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader)
2023-05-05 14:44:03 +00:00
if err != nil {
2023-05-07 01:00:22 +00:00
return nil, err
2023-05-05 14:44:03 +00:00
}
2023-05-07 01:00:22 +00:00
if fileData != nil {
filter := primitive.M{
"_id": fileHeader.Data,
}
err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData)
if err != nil {
return nil, err
}
2023-05-05 14:44:03 +00:00
}
2023-05-07 01:00:22 +00:00
return fileHeader, nil
2023-05-05 14:44:03 +00:00
}
2023-05-05 18:32:41 +00:00
2023-05-07 01:00:22 +00:00
func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data)
2023-05-05 18:32:41 +00:00
if err != nil {
2023-05-07 01:00:22 +00:00
return primitive.NilObjectID, primitive.NilObjectID, err
2023-05-05 18:32:41 +00:00
}
2023-05-07 01:00:22 +00:00
file.Data = resData.InsertedID.(primitive.ObjectID)
resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file)
2023-05-05 18:32:41 +00:00
if err != nil {
2023-05-07 01:00:22 +00:00
return primitive.NilObjectID, primitive.NilObjectID, err
2023-05-05 18:32:41 +00:00
}
2023-05-07 01:00:22 +00:00
return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err
}
2023-05-05 18:32:41 +00:00
2023-05-07 01:00:22 +00:00
func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error {
return errors.New("todo not implemented yet")
2023-05-05 18:32:41 +00:00
}
func (fs *WebFileSystem) Validate() error {
2023-05-07 01:00:22 +00:00
// 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)
// }
2023-05-05 18:32:41 +00:00
return nil
}