personal-website/webfilesystem/webfilesystem.go

356 lines
8.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-04-29 09:02:55 +00:00
"errors"
2023-05-03 21:50:08 +00:00
"net/http"
2023-05-04 15:55:08 +00:00
"strconv"
2023-04-25 09:37:42 +00:00
"strings"
2023-05-03 21:50:08 +00:00
"github.com/gin-gonic/gin"
2023-04-29 09:02:55 +00:00
"github.com/mitchellh/mapstructure"
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
// 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) {
2023-04-29 10:17:25 +00:00
// dirFile, err := fs.Read(fs.GetParentPath(path))
dirFile, err := fs.Read(path)
2023-04-29 09:02:55 +00:00
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)
2023-04-29 10:17:25 +00:00
parentPath := fs.GetParentPath(path)
parentDir, err := fs.Read(parentPath)
if err != nil {
return err
}
2023-04-29 09:02:55 +00:00
directory := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedpath[len(splittedpath)-1],
Type: "directory",
Data: FolderData{
2023-04-29 10:17:25 +00:00
Parent: parentDir.MongoId,
Children: []primitive.ObjectID{},
2023-04-29 09:02:55 +00:00
},
}
2023-04-29 10:17:25 +00:00
fs.CreateFile(&directory, parentPath)
// res, err := fs.webfsCollection.InsertOne(context.Background(), &directory)
// if err != nil {
// return err
// }
// fileId := fs.castInsertId(res)
// fs.insertFileToDirectory(fileId, parentDir.MongoId)
return nil
}
func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error {
2023-05-01 12:32:41 +00:00
//TODO Check file existance
2023-04-29 10:17:25 +00:00
parentDir, err := fs.Read(parentPath)
if err != nil {
return err
}
res, err := fs.webfsCollection.InsertOne(context.Background(), &file)
if err != nil {
return err
}
_ = parentDir
fileId := fs.castInsertId(res)
fs.insertFileToDirectory(fileId, parentDir.MongoId)
return 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 := FolderData{}
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
2023-04-29 09:02:55 +00:00
if err != nil {
return err
}
2023-04-29 10:17:25 +00:00
fileData.Children = append(fileData.Children, fileId)
fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}})
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 {
// extension := []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)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
return parentPath
2023-03-22 21:53:06 +00:00
}
2023-05-03 21:50:08 +00:00
func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
route.GET("writeFile", func(ctx *gin.Context) {
parentPath := ctx.Query("parentPath")
if parentPath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: "pp",
Type: "test",
Data: nil,
}
err := fs.CreateFile(&file, parentPath)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "OK")
})
route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
err := fs.CreateDirectory(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "OK")
})
route.GET("list", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
files, err := fs.List(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &files)
})
route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := fs.Read(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &file)
})
2023-05-04 15:55:08 +00:00
route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate()
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}
func (fs *WebFileSystem) Validate() error {
filter := primitive.D{
{
Key: "type",
Value: "directory",
},
}
cur, err := fs.webfsCollection.Find(context.Background(), filter)
defer cur.Close(context.Background())
if err != nil {
return err
}
directories := []*WebFSFile{}
// err = cur.All(context.Background(), directories)
// if err != nil {
// fmt.Println(err.Error())
// }
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 {
// println(d.Name)
fs.validateDir(d)
}
return nil
}
func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
_ = kek
// FolderData
children := []primitive.ObjectID{}
counter := 0
for _, v := range kek {
// println(v.(primitive.ObjectID).String())
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
if err != nil {
counter++
} else {
children = append(children, v.(primitive.ObjectID))
}
}
if counter > 0 {
println(dir.Name + " broken iDs: " + strconv.Itoa(counter))
_, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}})
if err != nil {
println(err.Error())
return err
}
}
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"`
Data interface{} `bson:"data" json:"-"`
2023-05-01 12:32:41 +00:00
Icon string `bson:"-" json:"icon"`
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
}
func (e *Exec) GetFileName() string {
2023-04-25 09:37:42 +00:00
return e.Name
2023-03-22 21:53:06 +00:00
}
2023-04-29 10:17:25 +00:00
// type WebFSFile2 interface {
// GetName() string
// GetType() string
// GetData() interface{}
// }