182 lines
4.6 KiB
Go
182 lines
4.6 KiB
Go
package webfilesystem
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
"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
|
|
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
|
|
ctx: context.Background(),
|
|
}
|
|
}
|
|
|
|
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 {
|
|
dir, err := fs.ReadByObjectID(directoryId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//TODO check if file exist
|
|
fileData := DirectoryData{}
|
|
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fileData.Children = append(fileData.Children, fileId)
|
|
|
|
fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}})
|
|
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)
|
|
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
|
|
return parentPath
|
|
}
|
|
|
|
func (fs *WebFileSystem) Delete(filePath string) error {
|
|
// splittedPath := fs.SplitPath(filePath)
|
|
// parentPath := fs.GetParentPath(filePath)
|
|
|
|
_, err := fs.NewRead(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:"-"`
|
|
}
|