personal-website/webfilesystem/webfilesystem.go

197 lines
5.1 KiB
Go
Raw Normal View History

2023-03-22 21:53:06 +00:00
package webfilesystem
2023-04-25 09:37:42 +00:00
import (
"context"
2023-05-07 00:14:30 +00:00
"errors"
2023-04-25 09:37:42 +00:00
"strings"
2023-04-29 10:17:25 +00:00
"go.mongodb.org/mongo-driver/bson"
2023-04-25 09:37:42 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
2023-04-12 17:04:25 +00:00
2023-03-22 21:53:06 +00:00
type WebFileSystem struct {
2023-04-25 09:37:42 +00:00
webfsCollection *mongo.Collection
2023-05-07 00:14:30 +00:00
webfsFilesTable *mongo.Collection
webfsFilesData *mongo.Collection
2023-05-05 18:32:41 +00:00
ctx context.Context
2023-04-25 09:37:42 +00:00
}
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist
2023-05-07 00:14:30 +00:00
webfsFilesTable: mongoClient.Database(dBName).Collection("webfs-table"), // TODO Check collection is exist, //FIXME
webfsFilesData: mongoClient.Database(dBName).Collection("webfs-data"), // TODO Check collection is exist, //FIXME
2023-05-05 14:44:03 +00:00
ctx: context.Background(),
2023-04-25 09:37:42 +00:00
}
2023-03-22 21:53:06 +00:00
}
2023-05-07 00:14:30 +00:00
// Deprecated
2023-04-29 09:02:55 +00:00
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{
{
Key: "_id",
Value: objectId,
},
}
2023-05-05 18:32:41 +00:00
//TODO to readMongo()
2023-04-29 09:02:55 +00:00
file, err := fs.findFileInMongo(filter)
return file, err
}
2023-05-05 18:32:41 +00:00
// Deprecated
2023-04-29 09:02:55 +00:00
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
}
2023-04-29 10:17:25 +00:00
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 {
2023-05-07 00:14:30 +00:00
read := ReadStruct2{
File: DirectoryFile{},
Id: directoryId,
}
parentDirRaw, err := fs.Read(read)
2023-04-29 10:17:25 +00:00
if err != nil {
return err
}
2023-05-07 00:14:30 +00:00
var dirPtr interface{} = *parentDirRaw
parentDir := dirPtr.(*DirectoryFile)
parentDir.Data.Children = append(parentDir.Data.Children, fileId)
res, err := fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data.children": parentDir.Data.Children}})
2023-04-29 09:02:55 +00:00
if err != nil {
return err
}
2023-05-07 00:14:30 +00:00
if res.ModifiedCount < 1 {
return errors.New("no parent folders edited")
}
2023-04-29 09:02:55 +00:00
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
}
2023-05-02 00:12:43 +00:00
func (fs *WebFileSystem) GetExtension(filename string) string {
splittedName := strings.Split(filename, ".")
return splittedName[len(splittedName)-1]
}
2023-04-29 09:02:55 +00:00
func (fs *WebFileSystem) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path)
2023-05-07 00:14:30 +00:00
if len(splittedPath) > 1 {
return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/")
}
return "/"
2023-03-22 21:53:06 +00:00
}
2023-05-07 00:14:30 +00:00
// Deprecated
2023-05-04 22:11:11 +00:00
func (fs *WebFileSystem) Delete(filePath string) error {
2023-05-05 18:32:41 +00:00
// splittedPath := fs.SplitPath(filePath)
// parentPath := fs.GetParentPath(filePath)
2023-05-04 22:11:11 +00:00
2023-05-07 00:14:30 +00:00
_, err := fs.NewReadDeprecated(filePath)
2023-05-04 22:11:11 +00:00
if err != nil {
return err
}
2023-05-05 18:32:41 +00:00
err = fs.removeMongo(filePath)
2023-05-04 22:11:11 +00:00
if err != nil {
return err
}
2023-05-05 18:32:41 +00:00
err = fs.Validate() //FIXME
2023-05-04 22:11:11 +00:00
if err != nil {
return err
}
2023-05-05 18:32:41 +00:00
//Delete from parent folder
// parentDir, err := fs.NewRead(parentPath)
// if err != nil {
// return err
// }
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
// parentDirData, err := castToDirectoryData(parentDir.Data)
// if err != nil {
// return err
// }
2023-05-04 19:49:22 +00:00
2023-05-05 18:32:41 +00:00
// newChildrenSlice := []primitive.ObjectID{}
// for i := 0; i < len(parentDirData.Children); i++ {
// if parentDirData.Children[i] != file.MongoId {
// newChildrenSlice = append(newChildrenSlice, parentDirData.Children[i])
// }
// }
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
// parentDirData.Children = newChildrenSlice
// parentDir.Data = parentDirData
// parentParentDir := fs.GetParentPath(parentPath)
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
// _, err = fs.writeMongo(*parentDir, parentParentDir)
// if err != nil {
// return err
// }
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
return nil
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
// 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.
// }
// }
2023-05-04 15:55:08 +00:00
2023-05-05 18:32:41 +00:00
// 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
2023-05-03 21:50:08 +00:00
}
2023-04-13 01:09:07 +00:00
type WebFSFile struct {
2023-04-29 09:02:55 +00:00
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
2023-05-01 12:32:41 +00:00
Icon string `bson:"-" json:"icon"`
2023-05-05 18:32:41 +00:00
Data interface{} `bson:"data" json:"-"`
2023-04-13 01:09:07 +00:00
}