personal-website/webfilesystem/webfilesystem.go

155 lines
3.4 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-04-29 09:02:55 +00:00
"errors"
2023-04-25 09:37:42 +00:00
"strings"
2023-04-29 09:02:55 +00:00
"github.com/mitchellh/mapstructure"
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
// folders []*Folder
}
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist
}
2023-03-22 21:53:06 +00:00
}
2023-04-25 09:37:42 +00:00
func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
2023-04-29 09:02:55 +00:00
splittedPath := fs.SplitPath(path)
2023-04-25 09:37:42 +00:00
filter := primitive.D{
{
2023-04-29 09:02:55 +00:00
Key: "name",
2023-04-25 09:37:42 +00:00
Value: splittedPath[len(splittedPath)-1],
},
}
2023-04-29 09:02:55 +00:00
file, err := fs.findFileInMongo(filter)
return file, err
}
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{
{
Key: "_id",
Value: objectId,
},
}
file, err := fs.findFileInMongo(filter)
return file, err
}
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) List(path string) ([]*WebFSFile, error) {
dirFile, err := fs.Read(fs.GetParentPath(path))
if err != nil {
return nil, err
}
if dirFile.Type != "directory" {
return nil, errors.New("file is not a directory")
}
fileData := FolderData{}
err = mapstructure.Decode(dirFile.Data.(primitive.D).Map(), &fileData)
if err != nil {
return nil, err
}
files := []*WebFSFile{}
for _, child := range fileData.Children {
file, err := fs.ReadByObjectID(child)
if err != nil {
println(err.Error())
continue
}
files = append(files, file)
}
return files, nil
}
func (fs *WebFileSystem) CreateDirectory(path string) error {
splittedpath := fs.SplitPath(path)
directory := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedpath[len(splittedpath)-1],
Type: "directory",
Data: FolderData{
// Parent: ,
Children: []primitive.ObjectID{
primitive.NewObjectID(),
primitive.NewObjectID(),
primitive.NewObjectID(),
},
},
}
_, err := fs.webfsCollection.InsertOne(context.Background(), &directory)
if err != nil {
return err
}
// res.InsertedID //TODO Insert to parent folder data
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) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
return parentPath
2023-03-22 21:53:06 +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"`
Data interface{} `bson:"data" json:"-"`
2023-04-13 01:09:07 +00:00
}
2023-04-25 09:37:42 +00:00
type FolderData struct {
2023-04-29 09:02:55 +00:00
Parent primitive.ObjectID `bson:"parent"`
Children []primitive.ObjectID `bson:"children"`
2023-04-25 09:37:42 +00:00
}
2023-04-13 01:09:07 +00:00
type File interface {
GetUuid() string
GetFileName() string
}
type Image struct {
}
type Exec struct {
WebFSFile
}
2023-04-25 09:37:42 +00:00
// func (e *Exec) GetUuid() string {
// return e.Id
// }
2023-04-13 01:09:07 +00:00
func (e *Exec) GetFileName() string {
2023-04-25 09:37:42 +00:00
return e.Name
2023-03-22 21:53:06 +00:00
}