assive commit

This commit is contained in:
cyber-dream 2023-05-05 17:44:03 +03:00
parent ccaebdc667
commit 2197356dcc
12 changed files with 619 additions and 409 deletions

View File

@ -33,15 +33,15 @@ func (b *BlogViewerApplication) GetId() string {
}
func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
route.GET("writeMockBlog", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
b.WriteMock(path)
ctx.JSON(http.StatusOK, "OK")
})
// route.GET("writeMockBlog", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "no path provided")
// return
// }
// b.WriteMock(path)
// ctx.JSON(http.StatusOK, "OK")
// })
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
@ -67,33 +67,33 @@ func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
})
}
func (b *BlogViewerApplication) WriteMock(path string) {
blogFile := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(),
Name: "test-1.blog",
Type: "blog-page",
Data: BlogFileData{
Header: "OMG THIS IS BLOG",
Blocks: []Block{
{
Type: "plain-text",
Data: []string{
"Apoqiwepoqiwepo",
".,mas;dakls;d",
"q[poqwieqpipoi]",
},
},
},
},
}
err := b.fs.CreateFile(&blogFile, path)
if err != nil {
println(err.Error())
}
}
// func (b *BlogViewerApplication) WriteMock(path string) {
// blogFile := webfilesystem.WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: "test-1.blog",
// Type: "blog-page",
// Data: BlogFileData{
// Header: "OMG THIS IS BLOG",
// Blocks: []Block{
// {
// Type: "plain-text",
// Data: []string{
// "Apoqiwepoqiwepo",
// ".,mas;dakls;d",
// "q[poqwieqpipoi]",
// },
// },
// },
// },
// }
// err := b.fs.CreateFile(&blogFile, path)
// if err != nil {
// println(err.Error())
// }
// }
func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) {
file, err := b.fs.Read(path)
file, err := b.fs.NewRead(path)
if err != nil {
println(err.Error())
return nil, err

View File

@ -1,7 +1,6 @@
package finder
import (
"net/http"
"personalwebsite/apps"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
@ -108,38 +107,38 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H
}
}
func (f *FinderApplication) Routes(routes *gin.RouterGroup) {
routes.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
admin := true
if isMobile {
ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile))
return
}
if admin {
ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile))
return
}
ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile))
// func (f *FinderApplication) Routes(routes *gin.RouterGroup) {
// routes.GET("render", func(ctx *gin.Context) {
// isMobileParam := ctx.Query("isMobile")
// isMobile := isMobileParam == "true"
// admin := true
// if isMobile {
// ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile))
// return
// }
// if admin {
// ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile))
// return
// }
// ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile))
})
routes.GET("renderMobileDesktop", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{})
})
routes.GET("renderDesktop", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{})
})
// })
// routes.GET("renderMobileDesktop", func(ctx *gin.Context) {
// ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{})
// })
// routes.GET("renderDesktop", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "no path provided")
// return
// }
// ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{})
// })
routes.GET("contextMenu", func(ctx *gin.Context) {
context := ctx.Query("context")
data := ctx.Query("data")
ginH := f.RenderContextMenu(context, data)
ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
})
}
// routes.GET("contextMenu", func(ctx *gin.Context) {
// context := ctx.Query("context")
// data := ctx.Query("data")
// ginH := f.RenderContextMenu(context, data)
// ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
// })
// }

43
apps/finder/routes.go Normal file
View File

@ -0,0 +1,43 @@
package finder
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (f *FinderApplication) Routes(routes *gin.RouterGroup) {
routes.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
admin := true
if isMobile {
ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile))
return
}
if admin {
ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile))
return
}
ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile))
})
routes.GET("renderMobileDesktop", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{})
})
routes.GET("renderDesktop", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{})
})
routes.GET("contextMenu", func(ctx *gin.Context) {
context := ctx.Query("context")
data := ctx.Query("data")
ginH := f.RenderContextMenu(context, data)
ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
})
}

View File

@ -55,7 +55,7 @@ func (p *ImgViewerApp) GetId() string {
}
func (p *ImgViewerApp) Render(path string, isMobile bool) (gin.H, error) {
img, err := p.fs.Read(path)
img, err := p.fs.NewRead(path)
if err != nil {
return nil, err
}

View File

@ -50,113 +50,113 @@ func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId
}
func (p *PersonalPropertiesApp) WriteToDb() {
allProps := make([]PropIsland, 0)
// func (p *PersonalPropertiesApp) WriteToDb() {
// allProps := make([]PropIsland, 0)
// careerProps := make([]Prop, 0)
expertiseIsland := PropIsland{
Header: "Area Of Expertise",
Props: []PropElement{{
Key: "Programming",
Values: []string{
"Creating tools and plugins for artists",
"Editor and basic gameplay scripting",
},
},
{
Key: "Game Art",
Values: []string{
"Professional modeling",
"Complete knowledge in CG render theory",
},
},
},
}
eduIsland := PropIsland{
Header: "Education",
Props: []PropElement{
{
Key: "Gymnasium 526",
KeyComments: []string{"2005-2015"},
Values: []string{"Extended natural sciences course", "Additional C++, media production and computer graphics courses", "Winner of conference “The future of a strong Russia is in high technology” in programming section"},
},
{
Key: "Lyceum 281",
KeyComments: []string{"2015-2016"},
Values: []string{"Extended IT and Physical sciences course"},
},
{
Key: "University",
KeyComments: []string{"2017-2019"},
Values: []string{"Faculty: Info-communication Networks and Systems", "Specialty: Information Security"},
},
},
}
careerProps := PropIsland{
Header: "Career",
Props: []PropElement{
{
Key: "VR lab assistant",
KeyComments: []string{"Academy of Digital Technologies", "2019-2020"},
Values: []string{"Teaching lessons for students in Unreal Engine 4, Unity and Blender editor courses", "Training students for World Skills Russia"},
},
{
Key: "3d artist",
KeyComments: []string{"Space Time VR", "2020-2020"},
Values: []string{"Creating 3d assets for VR game"},
},
{
Key: "Jr. Techartist",
KeyComments: []string{"MP Games"},
Values: []string{"Game content integration and production", "Shader coding", "Optimization asset production in artists pipeline"},
},
{
Key: "Techartist",
Values: []string{"Game content optimization and production", "Profiling and debugging render pipeline", "Creating in-house tools for pipeline software", "Working process pipeline integration and maintenance", "Linux servers administration"},
},
},
}
volunteerProps := PropIsland{
Header: "Volunteer Experience",
Props: []PropElement{
{
Key: "Metrostroi Mod",
Values: []string{
"Help unite fragmented community on one social platform",
"Worked on social elements of official site, create ranking",
"Took a part on creating ranking system for players",
"Creating models and maps for Steam Workshop"},
},
{
Key: "Age of Silence",
Values: []string{
"Start as tech-artist, create naming system in big ue4 project",
"Promoted to chief of 3d Department",
"Project win Unreal Day 2019",
},
},
},
}
// // careerProps := make([]Prop, 0)
// expertiseIsland := PropIsland{
// Header: "Area Of Expertise",
// Props: []PropElement{{
// Key: "Programming",
// Values: []string{
// "Creating tools and plugins for artists",
// "Editor and basic gameplay scripting",
// },
// },
// {
// Key: "Game Art",
// Values: []string{
// "Professional modeling",
// "Complete knowledge in CG render theory",
// },
// },
// },
// }
// eduIsland := PropIsland{
// Header: "Education",
// Props: []PropElement{
// {
// Key: "Gymnasium 526",
// KeyComments: []string{"2005-2015"},
// Values: []string{"Extended natural sciences course", "Additional C++, media production and computer graphics courses", "Winner of conference “The future of a strong Russia is in high technology” in programming section"},
// },
// {
// Key: "Lyceum 281",
// KeyComments: []string{"2015-2016"},
// Values: []string{"Extended IT and Physical sciences course"},
// },
// {
// Key: "University",
// KeyComments: []string{"2017-2019"},
// Values: []string{"Faculty: Info-communication Networks and Systems", "Specialty: Information Security"},
// },
// },
// }
// careerProps := PropIsland{
// Header: "Career",
// Props: []PropElement{
// {
// Key: "VR lab assistant",
// KeyComments: []string{"Academy of Digital Technologies", "2019-2020"},
// Values: []string{"Teaching lessons for students in Unreal Engine 4, Unity and Blender editor courses", "Training students for World Skills Russia"},
// },
// {
// Key: "3d artist",
// KeyComments: []string{"Space Time VR", "2020-2020"},
// Values: []string{"Creating 3d assets for VR game"},
// },
// {
// Key: "Jr. Techartist",
// KeyComments: []string{"MP Games"},
// Values: []string{"Game content integration and production", "Shader coding", "Optimization asset production in artists pipeline"},
// },
// {
// Key: "Techartist",
// Values: []string{"Game content optimization and production", "Profiling and debugging render pipeline", "Creating in-house tools for pipeline software", "Working process pipeline integration and maintenance", "Linux servers administration"},
// },
// },
// }
// volunteerProps := PropIsland{
// Header: "Volunteer Experience",
// Props: []PropElement{
// {
// Key: "Metrostroi Mod",
// Values: []string{
// "Help unite fragmented community on one social platform",
// "Worked on social elements of official site, create ranking",
// "Took a part on creating ranking system for players",
// "Creating models and maps for Steam Workshop"},
// },
// {
// Key: "Age of Silence",
// Values: []string{
// "Start as tech-artist, create naming system in big ue4 project",
// "Promoted to chief of 3d Department",
// "Project win Unreal Day 2019",
// },
// },
// },
// }
allProps = append(allProps, expertiseIsland, careerProps, eduIsland, volunteerProps)
file := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(),
Name: "personal.props",
Type: "props",
Data: PropertiesFile{
Props: allProps,
},
}
err := p.fs.CreateFile(&file, "/home/user/")
if err != nil {
println(err.Error())
} else {
println("Ok")
}
}
// allProps = append(allProps, expertiseIsland, careerProps, eduIsland, volunteerProps)
// file := webfilesystem.WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: "personal.props",
// Type: "props",
// Data: PropertiesFile{
// Props: allProps,
// },
// }
// err := p.fs.CreateFile(&file, "/home/user/")
// if err != nil {
// println(err.Error())
// } else {
// println("Ok")
// }
// }
func (p *PersonalPropertiesApp) Render() (gin.H, error) {
props, err := p.fs.Read("/home/user/personal.props")
props, err := p.fs.NewRead("/home/user/personal.props")
if err != nil {
return nil, err
}

View File

@ -46,7 +46,7 @@ func (l *ImagLib) Route(route *gin.RouterGroup) {
return
}
file, err := l.fs.Read(path)
file, err := l.fs.NewRead(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
}

View File

@ -22,7 +22,7 @@ type FilesWidget struct {
}
func (w *WDE) Render(path string) (gin.H, error) {
list, err := w.fs.List(path)
list, err := w.fs.NewList(path)
if err != nil {
return nil, err
}
@ -44,7 +44,7 @@ func (w *WDE) RenderContextMenu() (gin.H, error) {
}
func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
list, err := w.fs.List(directory)
list, err := w.fs.NewList(directory)
if err != nil {
return nil, err
}

View File

@ -0,0 +1,36 @@
package webfilesystem
import (
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type DirectoryData struct {
Parent primitive.ObjectID `bson:"parent"`
Children []primitive.ObjectID `bson:"children"`
}
// type Directory struct {
// Header Header `bson:"header"`
// Data DirectoryData `bson:"data"`
// }
// func (d *Directory) Get() interface{} {
// return d
// }
func castToFile(raw *interface{}) *WebFSFile {
var dirPtr interface{} = *raw
return dirPtr.(*WebFSFile)
}
func castToDirectory(data interface{}) (*FolderData, error) {
dirData := FolderData{}
err := mapstructure.Decode(data.(primitive.D).Map(), &dirData)
if err != nil {
return nil, err
}
return &dirData, nil
}
// func List()

37
webfilesystem/mongo.go Normal file
View File

@ -0,0 +1,37 @@
package webfilesystem
import (
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type readStruct struct {
File interface{}
Filter interface{}
}
func (fs *WebFileSystem) readMongo(read readStruct) (*interface{}, error) {
err := fs.webfsCollection.FindOne(fs.ctx, read.Filter).Decode(read.File)
if err != nil {
return nil, err
}
return &read.File, nil
}
func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitive.ObjectID, error) {
//TODO Check file existance
parentDir, err := fs.NewRead(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
res, err := fs.webfsCollection.InsertOne(context.Background(), &file)
if err != nil {
return primitive.NilObjectID, err
}
fileId := fs.castInsertId(res)
fs.insertFileToDirectory(fileId, parentDir.MongoId)
return fileId, nil
}

View File

@ -0,0 +1,83 @@
package webfilesystem
import (
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
splittedPath := fs.SplitPath(filePath)
read := readStruct{
File: &WebFSFile{},
Filter: primitive.M{
"name": splittedPath[len(splittedPath)-1],
},
}
fileRaw, err := fs.readMongo(read)
if err != nil {
return nil, err
}
file := castToFile(fileRaw)
return file, nil
}
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirRaw, err := fs.NewRead(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
newDir := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Data: FolderData{
Parent: parentDirRaw.MongoId,
Children: []primitive.ObjectID{},
},
Icon: "",
}
objectId, err := fs.writeMongo(newDir, parentPath)
if err != nil {
return primitive.NilObjectID, err
}
return objectId, nil
}
func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewRead(dirPath)
if err != nil {
return nil, err
}
if dirFile.Type != "directory" {
return nil, errors.New("file is not a directory")
}
fileData, err := castToDirectory(dirFile.Data)
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) CreateFile(file *WebFSFile, parentPath string) error {
fs.writeMongo(*file, parentPath)
return errors.New("Not ready yet")
}

149
webfilesystem/routes.go Normal file
View File

@ -0,0 +1,149 @@
package webfilesystem
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request
// fileName := ctx.Query("fileName")
// if fileName == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// single file
file, _ := ctx.FormFile("file")
if file == nil {
ctx.String(http.StatusBadRequest, "file is nil")
return
}
// generateMins := c.Param("generateMins")
// log.Println(file.Filename)
// Upload the file to specific dst.
dst := "./test-img/" + file.Filename
ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk
err := fs.UploadFile(dst, path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
}
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
// webfs.CreateFile(&img, "/home/user/")
ctx.Status(http.StatusCreated)
})
// 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",
// }
// err := fs.NewCreateFile(&file, parentPath)
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
// return
// }
// ctx.JSON(http.StatusOK, "OK")
// })
// route.GET("init", func(ctx *gin.Context) {
// err := fs.InitFS()
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, err.Error()) //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.NewCreateDirectory(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //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.NewList(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.NewRead(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &file)
})
route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate()
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
route.GET("delete", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Delete(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}

View File

@ -3,11 +3,9 @@ package webfilesystem
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -17,25 +15,27 @@ import (
type WebFileSystem struct {
webfsCollection *mongo.Collection
// folders []*Folder
ctx context.Context
}
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist
ctx: context.Background(),
}
}
func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
splittedPath := fs.SplitPath(path)
filter := primitive.D{
{
Key: "name",
Value: splittedPath[len(splittedPath)-1],
},
}
file, err := fs.findFileInMongo(filter)
return file, err
}
// func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
// splittedPath := fs.SplitPath(path)
// filter := primitive.D{
// {
// Key: "name",
// Value: splittedPath[len(splittedPath)-1],
// },
// }
// file, err := fs.findFileInMongo(filter)
// return file, err
// }
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{
@ -58,97 +58,93 @@ func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error)
return &file, nil
}
func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
// dirFile, err := fs.Read(fs.GetParentPath(path))
dirFile, err := fs.Read(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
}
// func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
// // dirFile, err := fs.Read(fs.GetParentPath(path))
// dirFile, err := fs.Read(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
// }
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)
// parentPath := fs.GetParentPath(path)
// parentDir, err := fs.Read(parentPath)
// if err != nil {
// return err
// }
// directory := WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: splittedpath[len(splittedpath)-1],
// Type: "directory",
// Data: FolderData{
// Parent: parentDir.MongoId,
// Children: []primitive.ObjectID{},
// },
// }
func (fs *WebFileSystem) CreateDirectory(path string) error {
splittedpath := fs.SplitPath(path)
parentPath := fs.GetParentPath(path)
parentDir, err := fs.Read(parentPath)
if err != nil {
return err
}
directory := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedpath[len(splittedpath)-1],
Type: "directory",
Data: FolderData{
Parent: parentDir.MongoId,
Children: []primitive.ObjectID{},
},
}
// fs.CreateFile(&directory, parentPath)
// // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO
// // if err != nil {
// // return err
// // }
fs.CreateFile(&directory, parentPath)
// res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO
// if err != nil {
// return err
// }
// // fileId := fs.castInsertId(res)
// // fs.insertFileToDirectory(fileId, parentDir.MongoId)
// return nil
// }
// fileId := fs.castInsertId(res)
// fs.insertFileToDirectory(fileId, parentDir.MongoId)
return nil
}
// func (fs *WebFileSystem) UpdateFile(filePath string, update primitive.M) error {
// file, err := fs.Read(filePath)
// if err != nil {
// return err
// }
// if file.MongoId.IsZero() {
// return errors.New("mongo id is zero")
// }
// filter := primitive.M{
// "_id": file.MongoId,
// }
// _, err = fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$set": update})
// if err != nil {
// return err
// }
// return nil
// }
func (fs *WebFileSystem) UpdateFile(filePath string, update primitive.M) error {
file, err := fs.Read(filePath)
if err != nil {
return err
}
if file.MongoId.IsZero() {
return errors.New("mongo id is zero")
}
filter := primitive.M{
"_id": file.MongoId,
}
_, err = fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$set": update})
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error {
//TODO Check file existance
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) CreateFile(file *WebFSFile, parentPath string) error {
// //TODO Check file existance
// 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)
@ -200,12 +196,12 @@ func (fs *WebFileSystem) Delete(filePath string) error {
splittedPath := fs.SplitPath(filePath)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
file, err := fs.Read(filePath)
file, err := fs.NewRead(filePath)
if err != nil {
return err
}
parentDir, err := fs.Read(parentPath)
parentDir, err := fs.NewRead(parentPath)
if err != nil {
return err
}
@ -227,139 +223,6 @@ func (fs *WebFileSystem) Delete(filePath string) error {
return nil
}
func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request
// fileName := ctx.Query("fileName")
// if fileName == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// single file
file, _ := ctx.FormFile("file")
if file == nil {
ctx.String(http.StatusBadRequest, "file is nil")
return
}
// generateMins := c.Param("generateMins")
// log.Println(file.Filename)
// Upload the file to specific dst.
dst := "./test-img/" + file.Filename
ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk
err := fs.UploadFile(dst, path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
}
// webFsCollection.CreateMiniatures("./test-img/", file.Filename)
// webfs.CreateFile(&img, "/home/user/")
ctx.Status(http.StatusCreated)
})
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)
})
route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate()
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
route.GET("delete", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Delete(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}
func (fs *WebFileSystem) Validate() error {
filter := primitive.D{
{