personal-website/webfilesystem/webfilesystem.go
2023-04-25 12:37:42 +03:00

72 lines
1.5 KiB
Go

package webfilesystem
import (
"context"
"strings"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type WebFileSystem struct {
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
}
}
func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
return nil, nil
}
func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
splittedPath := strings.Split(path, "/")
filter := primitive.D{
{
Key: "filename",
Value: splittedPath[len(splittedPath)-1],
},
}
if splittedPath == []string{"",""}
res := fs.webfsCollection.FindOne(context.TODO(), &filter)
findedFile := WebFSFile{}
err := res.Decode(&findedFile)
return &findedFile, err
}
type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id"`
Name string `bson:"name"`
Type string `bson:"type"`
data interface{} `bson:"data"`
}
type FolderData struct {
Parent primitive.ObjectID `bson:"parent"`
Childs []primitive.ObjectID `bson:"childs"`
}
type File interface {
GetUuid() string
GetFileName() string
}
type Image struct {
}
type Exec struct {
WebFSFile
}
// func (e *Exec) GetUuid() string {
// return e.Id
// }
func (e *Exec) GetFileName() string {
return e.Name
}