78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package webfilesystem
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
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 {
|
|
return errors.New("todo not implemented yet")
|
|
}
|
|
|
|
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
|
|
}
|