This commit is contained in:
cyber-dream 2023-04-29 12:04:02 +03:00
commit 38e4058840
5 changed files with 122 additions and 17 deletions

3
.env Normal file
View File

@ -0,0 +1,3 @@
MONGO_CONNECT=mongodb://localhost:27017
DATABASE=personal-website
COLLECTION_WEBFS=webfs

1
go.mod
View File

@ -30,6 +30,7 @@ require (
github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect

2
go.sum
View File

@ -41,6 +41,8 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=

16
main.go
View File

@ -107,6 +107,22 @@ func main() {
fs := router.Group("fs") fs := router.Group("fs")
{ {
fs.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
err := webfs.CreateDirectory(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "OK")
})
fs.GET("list", func(ctx *gin.Context) { fs.GET("list", func(ctx *gin.Context) {
path := ctx.Query("path") path := ctx.Query("path")
if path == "" { if path == "" {

View File

@ -2,8 +2,10 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"strings" "strings"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
@ -19,35 +21,116 @@ func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName
} }
} }
func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
return nil, nil
}
func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) { func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
splittedPath := strings.Split(path, "/") splittedPath := fs.SplitPath(path)
filter := primitive.D{ filter := primitive.D{
{ {
Key: "filename", Key: "name",
Value: splittedPath[len(splittedPath)-1], Value: splittedPath[len(splittedPath)-1],
}, },
} }
if splittedPath == []string{"",""} file, err := fs.findFileInMongo(filter)
res := fs.webfsCollection.FindOne(context.TODO(), &filter) return file, err
findedFile := WebFSFile{} }
err := res.Decode(&findedFile)
return &findedFile, 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
} }
type WebFSFile struct { type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id"` MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name"` Name string `bson:"name" json:"name"`
Type string `bson:"type"` Type string `bson:"type" json:"type"`
data interface{} `bson:"data"` Data interface{} `bson:"data" json:"-"`
} }
type FolderData struct { type FolderData struct {
Parent primitive.ObjectID `bson:"parent"` Parent primitive.ObjectID `bson:"parent"`
Childs []primitive.ObjectID `bson:"childs"` Children []primitive.ObjectID `bson:"children"`
} }
type File interface { type File interface {