personal-website/webfilesystem/mongo.go

106 lines
2.4 KiB
Go
Raw 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-11 01:45:57 +00:00
//TODO Transparently split to single data and header reading methods
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 {
2023-05-17 13:09:22 +00:00
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
2023-05-05 18:32:41 +00:00
}
2023-05-10 12:39:49 +00:00
//Deprecated
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
}