From 6c3bc32b599bff932e3bb7e849fe9fda32372b24 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Sun, 7 May 2023 03:14:30 +0300 Subject: [PATCH] massive commit for safe refactoring --- .env | 2 +- apps/blogviewer/blogviewer.go | 2 +- apps/finder/finder.go | 2 +- apps/personalprops/personalprops.go | 5 +- libs/imglib.go | 42 ++----- resources/sys/finder/finder.js | 17 +-- resources/wde.js | 4 +- test-img/folder.png | 3 + test-img/ohno.png | 3 + wde/wde.go | 5 +- webfilesystem/dir3.go | 140 +++++++++++++++++++++ webfilesystem/directory.go | 186 +++++++++++++++++++++++++++- webfilesystem/fileuploading.go | 44 ++++--- webfilesystem/mongo.go | 8 +- webfilesystem/newmethods.go | 8 +- webfilesystem/newmethods2.go | 82 ++++++++++++ webfilesystem/routes.go | 32 ++--- webfilesystem/webfilesystem.go | 41 ++++-- webfilesystem/webfs3.go | 181 +++++++++++++++++++++++++++ 19 files changed, 710 insertions(+), 97 deletions(-) create mode 100644 test-img/folder.png create mode 100644 test-img/ohno.png create mode 100644 webfilesystem/dir3.go create mode 100644 webfilesystem/newmethods2.go create mode 100644 webfilesystem/webfs3.go diff --git a/.env b/.env index 50a8248..7134cee 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ MONGO_CONNECT=mongodb://localhost:27017 DATABASE=personal-website -COLLECTION_WEBFS=webfs \ No newline at end of file +COLLECTION_WEBFS=webfs2 \ No newline at end of file diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index 0fad9fc..b95c37b 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -93,7 +93,7 @@ func (b *BlogViewerApplication) Route(route *gin.RouterGroup) { // } func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) { - file, err := b.fs.NewRead(path) + file, err := b.fs.NewReadDeprecated(path) if err != nil { println(err.Error()) return nil, err diff --git a/apps/finder/finder.go b/apps/finder/finder.go index b8961fe..df06e46 100644 --- a/apps/finder/finder.go +++ b/apps/finder/finder.go @@ -66,7 +66,7 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H } func (f *FinderApplication) RenderProps(filePath string) gin.H { - file, err := f.fs.NewRead(filePath) + file, err := f.fs.NewReadDeprecated(filePath) if err != nil { return nil } diff --git a/apps/personalprops/personalprops.go b/apps/personalprops/personalprops.go index c92ba85..e46f0c7 100644 --- a/apps/personalprops/personalprops.go +++ b/apps/personalprops/personalprops.go @@ -52,7 +52,7 @@ func (p *PersonalPropertiesApp) GetId() string { } func (p *PersonalPropertiesApp) Render() (gin.H, error) { - propsFile, err := p.fs.NewRead("/home/user/About Me.props") + propsFile, err := p.fs.NewReadDeprecated("/home/user/About Me.props") if err != nil { println(err.Error()) return nil, err @@ -91,8 +91,9 @@ func (p *PersonalPropertiesApp) Render() (gin.H, error) { } func castToPersPropsData(fileData interface{}) (*PropertiesFileData, error) { + // kek := fileData.(primitive.D).Map()["name"] propsData := PropertiesFileData{ - Header.Name: fileData.(primitive.D).Map()["name"].(string), + // Header.Name: , } err := mapstructure.Decode(fileData.(primitive.D).Map(), &propsData) if err != nil { diff --git a/libs/imglib.go b/libs/imglib.go index 49615fb..dce5a43 100644 --- a/libs/imglib.go +++ b/libs/imglib.go @@ -1,12 +1,10 @@ package libs import ( - "errors" "net/http" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" - "go.mongodb.org/mongo-driver/bson/primitive" ) type ImagLib struct { @@ -19,40 +17,26 @@ func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib { } } -func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) { - data, ok := img.Data.(primitive.D).Map()["srcdata"] - if !ok { - return nil, errors.New("error in file decoding") - } - bin := data.(primitive.Binary).Data - - min32Data, ok := img.Data.(primitive.D).Map()["min32data"] - if !ok { - return nil, errors.New("error in file decoding") - } - min32Bin := min32Data.(primitive.Binary).Data - return &Img{ - // Header: header, - Data: bin, - Miniature32: min32Bin, - }, nil -} - func (l *ImagLib) Route(route *gin.RouterGroup) { route.GET("get", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { - ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct return } - file, err := l.fs.NewRead(path) + fileId, err := l.fs.V3FindFile(path) if err != nil { - ctx.String(http.StatusInternalServerError, "TODO") //TODO + ctx.Status(http.StatusInternalServerError) + return + } + imgData := Img{} + _, err = l.fs.V3Read(fileId, &imgData) + if err != nil { + ctx.Status(http.StatusInternalServerError) } - data := file.Data.(primitive.Binary) - ctx.Data(http.StatusOK, "image/jpeg", data.Data) + ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin) }) } @@ -76,7 +60,7 @@ type Base64Img struct { } type Img struct { - Header string `bson:"header"` - Data []byte `bson:"srcdata"` - Miniature32 []byte `bson:"min32data"` + Header string `bson:"header"` + Bin []byte `bson:"bin"` + // BinMin32 []byte `bson:"binmin32"` } diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index ec201fd..3567bbe 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -68,16 +68,16 @@ class Finder{ FileUploading(filesList){ let formData = new FormData() - // console.log(filesList) + console.log(this.path) for (let i = 0; i < filesList.length; i++) { const element = filesList[i]; formData.append("file", element.getAsFile()) - console.log(formData) + // console.log(formData) } // console.log(formData) // formData.append("photo", photo); fetch('/fs/upload/?' + new URLSearchParams({ - path: '/home/user/', + parentPath: this.path, }), { method: "POST", @@ -125,15 +125,16 @@ class Finder{ * @param {string} path */ OpenDir(path){ + this.path = path this.fileView.OpenFolder(path) } - /** - */ - OpenNewDir(){ - WebDesktopEnvironment.Open("finder", [this.path]) - } + // /** + // */ + // OpenNewDir(){ + // WebDesktopEnvironment.Open("finder", [this.path]) + // } /** * @param {MouseEvent} event diff --git a/resources/wde.js b/resources/wde.js index 662e029..7759217 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -4,9 +4,9 @@ document.addEventListener('DOMContentLoaded', function() { // console.log(window.screen.width) wde = new WebDesktopEnvironment if (!WebDesktopEnvironment.isMobile){ - // WebDesktopEnvironment.Open("finder", ["/home/user"]) + WebDesktopEnvironment.Open("finder", ["/home/user"]) // WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"]) - WebDesktopEnvironment.Open("personal-properties", ["kek"]) + // WebDesktopEnvironment.Open("personal-properties", ["kek"]) } else { WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"]) } diff --git a/test-img/folder.png b/test-img/folder.png new file mode 100644 index 0000000..039d2e5 --- /dev/null +++ b/test-img/folder.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:26ee23cca095e2d7aff73d960b2e205a8e38ac61ff33f9acabe8ced78859d675 +size 6685 diff --git a/test-img/ohno.png b/test-img/ohno.png new file mode 100644 index 0000000..0581cdd --- /dev/null +++ b/test-img/ohno.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3e6b3bab266893bb392a45568132c034b5e39e4203a22f2de855f1f571111987 +size 4037 diff --git a/wde/wde.go b/wde/wde.go index c9500d7..ece3c62 100644 --- a/wde/wde.go +++ b/wde/wde.go @@ -22,7 +22,7 @@ type FilesWidget struct { } func (w *WDE) Render(path string) (gin.H, error) { - list, err := w.fs.NewList(path) + list, err := w.fs.NewListDeprecated(path) if err != nil { return nil, err } @@ -44,10 +44,11 @@ func (w *WDE) RenderContextMenu() (gin.H, error) { } func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) { - list, err := w.fs.NewList(directory) + list, err := w.fs.V3List(directory) if err != nil { return nil, err } + for _, file := range list { switch file.Type { case "directory": diff --git a/webfilesystem/dir3.go b/webfilesystem/dir3.go new file mode 100644 index 0000000..327931d --- /dev/null +++ b/webfilesystem/dir3.go @@ -0,0 +1,140 @@ +package webfilesystem + +import ( + "errors" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type V3DirectoryData struct { + MongoId primitive.ObjectID `bson:"_id"` + Parent primitive.ObjectID `bson:"parent_id"` + Children []primitive.ObjectID `bson:"children_id"` +} + +func (fs *WebFileSystem) V3CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) { + splittedPath := fs.SplitPath(dirPath) + parentPath := fs.GetParentPath(dirPath) + + parentDirId, err := fs.V3FindFile(parentPath) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + + parentDirHeader, err := fs.V3Read(parentDirId, nil) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + + newDir := V3FileHeader{ + MongoId: primitive.NewObjectID(), + Name: splittedPath[len(splittedPath)-1], + Type: "directory", + Icon: "", + } + newDirData := V3DirectoryData{ + MongoId: primitive.NewObjectID(), + Parent: parentDirHeader.MongoId, + Children: []primitive.ObjectID{}, + } + + headerId, dataId, err := fs.v3WriteFileToMongo(&newDir, &newDirData) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + + err = fs.V3AppendChildToDirectory(headerId, parentDirId) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + return headerId, dataId, nil +} + +func (fs *WebFileSystem) V3AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error { + parentDirData := V3DirectoryData{} + parentDir, err := fs.V3Read(parentId, &parentDirData) + if err != nil { + return err + } + + //TODO Check for duplicates? + parentDirData.Children = append(parentDirData.Children, childId) + + err = fs.V3WriteUpdateData(parentDir, parentDirData) + if err != nil { + return err + } + return nil +} + +func (fs *WebFileSystem) V3List(dirPath string) ([]*V3FileHeader, error) { + dirId, err := fs.V3FindFile(dirPath) + if err != nil { + return nil, err + } + + dirData := V3DirectoryData{} + _, err = fs.V3Read(dirId, &dirData) + if err != nil { + return nil, err + } + + // println(dirId.String()) + // println(dirData.MongoId.String()) + + children := []*V3FileHeader{} + for _, childID := range dirData.Children { + childFile, err := fs.V3Read(childID, nil) + if err != nil { + // println(err.Error()) + continue + } + + children = append(children, childFile) + + } + return children, nil +} + +func (fs *WebFileSystem) V3FindFile(filePath string) (primitive.ObjectID, error) { + splittedPath := fs.SplitPath(filePath) + rootDir, _, err := fs.V3GetRootDir() + if err != nil { + return primitive.NilObjectID, err + } + // println(rootDir.Name) + lastFileID := rootDir.MongoId + for _, pathPart := range splittedPath[1:] { + // println(pathPart) + id, err := fs.v3findInChildren(pathPart, lastFileID) + if err != nil { + return primitive.NilObjectID, err + } + lastFileID = id + } + return lastFileID, nil +} + +// func (fs *WebFileSystem) v3updateChildrenList(directoryID primitive.ObjectID, childrenList []primitive.ObjectID) (primitive.ObjectID, error) { + +// } + +func (fs *WebFileSystem) v3findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) { + parentDirData := V3DirectoryData{} + _, err := fs.V3Read(parentDirId, &parentDirData) + if err != nil { + return primitive.NilObjectID, err + } + + for _, childId := range parentDirData.Children { + childFileHeader, err := fs.V3Read(childId, nil) + if err != nil { + return primitive.NilObjectID, err + } + if childFileHeader.Name == fileName { + return childId, nil + } + } + + return primitive.NilObjectID, errors.New("file not found") +} diff --git a/webfilesystem/directory.go b/webfilesystem/directory.go index 3bc52f6..efdbe55 100644 --- a/webfilesystem/directory.go +++ b/webfilesystem/directory.go @@ -2,6 +2,7 @@ package webfilesystem import ( "context" + "errors" "strconv" "github.com/mitchellh/mapstructure" @@ -14,11 +15,150 @@ type DirectoryData struct { Children []primitive.ObjectID `bson:"children"` } +//Deprecated +func (fs *WebFileSystem) List(dirPath string) ([]*OnlyHeaderFile, error) { + fileId, err := fs.FindFile(dirPath) + if err != nil { + return nil, err + } + + read := ReadStruct2{ + File: &DirectoryFile{}, + Id: fileId, + } + fileRaw, err := fs.Read(read) + if err != nil { + return nil, err + } + + var dirPtr interface{} = *fileRaw + dir := dirPtr.(*DirectoryFile) + + children := []*OnlyHeaderFile{} + for _, childID := range dir.Data.Children { + read := ReadStruct2{ + File: &OnlyHeaderFile{}, + Id: childID, + } + childFile, err := fs.Read(read) + if err != nil { + // println(err.Error()) + continue + } + + var dirPtr interface{} = *childFile + children = append(children, dirPtr.(*OnlyHeaderFile)) + + } + return children, nil +} + +//Deprecated +func (fs *WebFileSystem) FindFile(filePath string) (primitive.ObjectID, error) { + splittedPath := fs.SplitPath(filePath) + _, rootDirID, err := fs.GetRootDir() + if err != nil { + return primitive.NilObjectID, err + } + // println(rootDir.Name) + lastFileID := rootDirID + for _, pathPart := range splittedPath[1:] { + // println(pathPart) + id, err := fs.findInChildren(pathPart, lastFileID) + if err != nil { + return primitive.NilObjectID, err + } + lastFileID = id + } + return lastFileID, nil +} + +//Deprecated +func (fs *WebFileSystem) findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) { + read := ReadStruct2{ + File: &DirectoryFile{ + FileHeader: FileHeader{}, + Data: DirectoryData{}, + }, + Id: parentDirId, + } + parentDirRaw, err := fs.Read(read) + if err != nil { + return primitive.NilObjectID, err + } + var dirPtr interface{} = *parentDirRaw + dir := dirPtr.(*DirectoryFile) + + for _, childId := range dir.Data.Children { + read := ReadStruct2{ + File: OnlyHeaderFile{}, + Id: childId, + } + childRaw, err := fs.Read(read) + if err != nil { + return primitive.NilObjectID, err + } + + var dirPtr interface{} = *childRaw + childFileHeader := dirPtr.(*OnlyHeaderFile) + + if childFileHeader.Name == fileName { + return childId, nil + } + } + + return primitive.NilObjectID, errors.New("file not found") +} + +//Deprecated +func (fs *WebFileSystem) IsChild(childID primitive.ObjectID, parent primitive.ObjectID) (bool, error) { + read := ReadStruct{ + File: DirectoryFile{}, //TODO May be a possible problem, if parent file would be not a directory type + } + dirRaw, err := fs.ReadMongo(read) + if err != nil { + return false, err + } + var dirPtr interface{} = *dirRaw + dir := dirPtr.(*DirectoryFile) + + for _, cID := range dir.Data.Children { + if cID == childID { + return true, nil + } + } + return false, nil +} + +//Deprecated +func (fs *WebFileSystem) GetRootDir() (*DirectoryFile, primitive.ObjectID, error) { + filter := primitive.M{ + "header.name": "/", + } + res := fs.webfsCollection.FindOne(fs.ctx, filter) + if res == nil { + return nil, primitive.NilObjectID, errors.New("TODO") //TODO + } + + rootDirID := FileId{} + rootDir := DirectoryFile{} + err := res.Decode(&rootDir) + if res == nil { + return nil, primitive.NilObjectID, err + } + err = res.Decode(&rootDirID) + if res == nil { + return nil, primitive.NilObjectID, err + } + return &rootDir, rootDirID.Id, err +} + +//Deprecated func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) { splittedPath := fs.SplitPath(dirPath) parentPath := fs.GetParentPath(dirPath) - parentDirRaw, err := fs.NewRead(parentPath) + parentDirRaw, err := fs.NewReadDeprecated(parentPath) if err != nil { return primitive.NilObjectID, err } @@ -41,6 +181,50 @@ func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, return objectId, nil } +func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, error) { + splittedPath := fs.SplitPath(dirPath) + parentPath := fs.GetParentPath(dirPath) + + parentDirID, err := fs.FindFile(parentPath) + if err != nil { + return primitive.NilObjectID, err + } + read := ReadStruct2{ + File: nil, + Id: parentDirID, + } + parentDirRaw, err := fs.Read(read) + if err != nil { + return primitive.NilObjectID, err + } + + var dirPtr interface{} = *parentDirRaw + parentDir := dirPtr.(*OnlyHeaderFile) + if parentDir.Type != "directory" { + return primitive.NilObjectID, errors.New("path is bad") + } + + newDir := DirectoryFile{ + FileHeader: FileHeader{ + Name: splittedPath[len(splittedPath)-1], + Type: "", + Icon: "", + }, + Data: DirectoryData{ + Parent: parentDirID, + Children: []primitive.ObjectID{}, + }, + } + resID, err := fs.Write(newDir, dirPath, false) + if err != nil { + return primitive.NilObjectID, err + } + + fs.insertFileToDirectory(resID, parentDirID) + + return resID, nil +} + func (fs *WebFileSystem) validateDir(dir *WebFSFile) error { kek := dir.Data.(primitive.D).Map()["children"].(primitive.A) _ = kek diff --git a/webfilesystem/fileuploading.go b/webfilesystem/fileuploading.go index 530e04f..664fa1f 100644 --- a/webfilesystem/fileuploading.go +++ b/webfilesystem/fileuploading.go @@ -5,11 +5,13 @@ package webfilesystem import ( "errors" "os" + "path" "go.mongodb.org/mongo-driver/bson/primitive" ) -func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error { +func (fs *WebFileSystem) UploadFile(realFilepath string, parentPath string) error { + //TODO check is file exists file, err := os.ReadFile(realFilepath) if err != nil { return err @@ -18,33 +20,43 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error { fileName := splittedPath[len(splittedPath)-1] extension := fs.GetExtension(fileName) + newFile2 := V3FileHeader{ + MongoId: primitive.NewObjectID(), + Name: fileName, + Type: "", + Icon: "", + Data: primitive.NilObjectID, + } + + newFileData := V3FileBinaryData{ + MongoId: primitive.NewObjectID(), + Bin: file, + } + + filePath := path.Join(parentPath, fileName) + switch extension { case "jpg": fallthrough case "jpeg": - newFile := WebFSFile{ - MongoId: primitive.NewObjectID(), - Name: fileName, - Type: "jpeg", - Data: file, - Icon: "", - } - err := fs.WriteFile(&newFile, path) + newFile2.Type = "jpeg" + _, _, err := fs.V3Write(filePath, &newFile2, &newFileData) return err case "png": - newFile := WebFSFile{ - MongoId: primitive.NewObjectID(), - Name: fileName, - Type: "png", - Data: file, + newFile2.Type = "png" + _, _, err := fs.V3Write(filePath, &newFile2, &newFileData) + if err != nil { + println(err.Error()) + return err } - err := fs.WriteFile(&newFile, path) - return err + return nil default: return errors.New("this filetype not allowed") } } + +// Deprecated func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error { extension := fs.GetExtension(fileName) switch extension { diff --git a/webfilesystem/mongo.go b/webfilesystem/mongo.go index 889156a..9d2e95c 100644 --- a/webfilesystem/mongo.go +++ b/webfilesystem/mongo.go @@ -12,6 +12,9 @@ type ReadStruct struct { Filter interface{} } +//TODO: Read files only by objectId, before reading file, search path by reading all parent folders + +// Deprecated func (fs *WebFileSystem) ReadMongo(read ReadStruct) (*interface{}, error) { err := fs.webfsCollection.FindOne(fs.ctx, read.Filter).Decode(read.File) if err != nil { @@ -20,9 +23,10 @@ func (fs *WebFileSystem) ReadMongo(read ReadStruct) (*interface{}, error) { return &read.File, nil } +// Deprecated func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitive.ObjectID, error) { //TODO Check file existance - parentDir, err := fs.NewRead(parentPath) + parentDir, err := fs.NewReadDeprecated(parentPath) if err != nil { return primitive.NilObjectID, err } @@ -38,7 +42,7 @@ func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitiv } func (fs *WebFileSystem) removeMongo(filePath string) error { - file, err := fs.NewRead(filePath) + file, err := fs.NewReadDeprecated(filePath) if err != nil { return err } diff --git a/webfilesystem/newmethods.go b/webfilesystem/newmethods.go index 5d933ef..ddd19a8 100644 --- a/webfilesystem/newmethods.go +++ b/webfilesystem/newmethods.go @@ -6,7 +6,8 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) -func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) { +// Deprecated +func (fs *WebFileSystem) NewReadDeprecated(filePath string) (*WebFSFile, error) { splittedPath := fs.SplitPath(filePath) read := ReadStruct{ File: &WebFSFile{}, @@ -24,8 +25,9 @@ func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) { return file, nil } -func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) { - dirFile, err := fs.NewRead(dirPath) +// Deprecated +func (fs *WebFileSystem) NewListDeprecated(dirPath string) ([]*WebFSFile, error) { + dirFile, err := fs.NewReadDeprecated(dirPath) if err != nil { return nil, err } diff --git a/webfilesystem/newmethods2.go b/webfilesystem/newmethods2.go new file mode 100644 index 0000000..9dbb927 --- /dev/null +++ b/webfilesystem/newmethods2.go @@ -0,0 +1,82 @@ +package webfilesystem + +import ( + "context" + "errors" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +// Deprecated +type ReadStruct2 struct { + File interface{} + Id primitive.ObjectID +} + +// Deprecated +func (fs *WebFileSystem) Read(read ReadStruct2) (*interface{}, error) { + //TODO Check nil Id + filter := primitive.M{ + "_id": read.Id, + } + err := fs.webfsCollection.FindOne(fs.ctx, filter).Decode(&read.File) + return &read.File, err +} + +// Deprecated +func (fs *WebFileSystem) Write(file interface{}, filePath string, force bool) (primitive.ObjectID, error) { + foundedFile, err := fs.FindFile(filePath) + if err == nil { //FIXME + return primitive.NilObjectID, errors.New("file already exists") + } + _ = foundedFile + + res, err := fs.webfsCollection.InsertOne(fs.ctx, file) + if err != nil { + return primitive.NilObjectID, err + } + _ = res.InsertedID + return primitive.NilObjectID, nil //FIXME +} + +// Deprecated +func (fs *WebFileSystem) InitFS() error { + rootDir := DirectoryFile{ + FileHeader: FileHeader{ + Name: "/", + Type: "directory", + Icon: "", + }, + Data: DirectoryData{ + // Parent: id, //FIXME don't work + Children: []primitive.ObjectID{}, + }, + } + _, err := fs.webfsCollection.InsertOne(context.Background(), &rootDir) + + return err +} + +// Deprecated +type FileId struct { + Id primitive.ObjectID `bson:"_id"` +} + +// Deprecated +type FileHeader struct { + Name string `bson:"name" json:"name"` + Type string `bson:"type" json:"type"` + Icon string `bson:"-" json:"icon"` +} + +// Deprecated +type DirectoryFile struct { + FileHeader `bson:"header"` + Data DirectoryData `bson:"data" json:"-"` +} + +// Deprecated +type OnlyHeaderFile struct { + FileHeader `bson:"header"` + // Data DirectoryData `bson:"data" json:"-"` +} diff --git a/webfilesystem/routes.go b/webfilesystem/routes.go index 79b0719..65e1ca2 100644 --- a/webfilesystem/routes.go +++ b/webfilesystem/routes.go @@ -13,8 +13,8 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { // ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct // return // } - path := ctx.Query("path") - if path == "" { + parentPath := ctx.Query("parentPath") + if parentPath == "" { ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct return } @@ -32,7 +32,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { ctx.SaveUploadedFile(file, dst) //TODO: Not Save to disk - err := fs.UploadFile(dst, path) + err := fs.UploadFile(dst, parentPath) if err != nil { ctx.String(http.StatusInternalServerError, "TODO") //TODO return @@ -65,14 +65,14 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { // 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("init", func(ctx *gin.Context) { + err := fs.V3InitFS() + if err != nil { + ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct + return + } + ctx.JSON(http.StatusOK, "Inited") + }) route.GET("createDir", func(ctx *gin.Context) { path := ctx.Query("path") @@ -81,7 +81,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { return } - _, err := fs.NewCreateDirectory(path) + _, _, err := fs.V3CreateDirectory(path) if err != nil { ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct return @@ -97,9 +97,9 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { return } - files, err := fs.NewList(path) + files, err := fs.V3List(path) if err != nil { - ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct + ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct return } @@ -113,7 +113,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { return } - file, err := fs.NewRead(path) + file, err := fs.NewReadDeprecated(path) if err != nil { ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct return @@ -138,7 +138,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { ctx.Status(http.StatusBadRequest) //TODO return } - err := fs.Delete(path) + err := fs.V3Remove(path) if err != nil { ctx.Status(http.StatusInternalServerError) return diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 9d79468..ddfb518 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -2,9 +2,9 @@ package webfilesystem import ( "context" + "errors" "strings" - "github.com/mitchellh/mapstructure" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -12,16 +12,21 @@ import ( type WebFileSystem struct { webfsCollection *mongo.Collection + webfsFilesTable *mongo.Collection + webfsFilesData *mongo.Collection 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 + webfsFilesTable: mongoClient.Database(dBName).Collection("webfs-table"), // TODO Check collection is exist, //FIXME + webfsFilesData: mongoClient.Database(dBName).Collection("webfs-data"), // TODO Check collection is exist, //FIXME ctx: context.Background(), } } +// Deprecated func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) { filter := primitive.D{ { @@ -50,19 +55,26 @@ func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.Obje } func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, directoryId primitive.ObjectID) error { - dir, err := fs.ReadByObjectID(directoryId) + read := ReadStruct2{ + File: DirectoryFile{}, + Id: directoryId, + } + parentDirRaw, err := fs.Read(read) if err != nil { return err } - //TODO check if file exist - fileData := DirectoryData{} - err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData) - if err != nil { - return err - } - fileData.Children = append(fileData.Children, fileId) + var dirPtr interface{} = *parentDirRaw + parentDir := dirPtr.(*DirectoryFile) - fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}}) + parentDir.Data.Children = append(parentDir.Data.Children, fileId) + + res, err := fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data.children": parentDir.Data.Children}}) + if err != nil { + return err + } + if res.ModifiedCount < 1 { + return errors.New("no parent folders edited") + } return nil } @@ -86,15 +98,18 @@ func (fs *WebFileSystem) GetExtension(filename string) string { func (fs *WebFileSystem) GetParentPath(path string) string { splittedPath := fs.SplitPath(path) - parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") - return parentPath + if len(splittedPath) > 1 { + return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/") + } + return "/" } +// Deprecated func (fs *WebFileSystem) Delete(filePath string) error { // splittedPath := fs.SplitPath(filePath) // parentPath := fs.GetParentPath(filePath) - _, err := fs.NewRead(filePath) + _, err := fs.NewReadDeprecated(filePath) if err != nil { return err } diff --git a/webfilesystem/webfs3.go b/webfilesystem/webfs3.go new file mode 100644 index 0000000..15aecce --- /dev/null +++ b/webfilesystem/webfs3.go @@ -0,0 +1,181 @@ +package webfilesystem + +import ( + "errors" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type V3FileHeader struct { + MongoId primitive.ObjectID `bson:"_id" json:"-"` + Name string `bson:"name" json:"name"` + Type string `bson:"type" json:"type"` + Icon string `bson:"-" json:"icon"` + Data primitive.ObjectID `bson:"data_id" json:"-"` +} +type V3FileBinaryData struct { + MongoId primitive.ObjectID `bson:"_id" json:"-"` + Bin []byte `bson:"bin" json:"-"` +} + +func (fs *WebFileSystem) V3ReadHeader(fileID primitive.ObjectID) (*V3FileHeader, error) { + file := &V3FileHeader{} + filter := primitive.M{ + "_id": fileID, + } + err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file) + return file, err +} + +func (fs *WebFileSystem) V3Read(fileID primitive.ObjectID, fileData interface{}) (*V3FileHeader, error) { + fileHeader := &V3FileHeader{} + filter := primitive.M{ + "_id": fileID, + } + err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader) + if err != nil { + return nil, err + } + if fileData != nil { + filter := primitive.M{ + "_id": fileHeader.Data, + } + err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData) + if err != nil { + return nil, err + } + } + return fileHeader, nil +} + +func (fs *WebFileSystem) V3Write(filePath string, file *V3FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { + headerId, dataId, err := fs.v3WriteFileToMongo(file, data) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + parentPath := fs.GetParentPath(filePath) + parentDir, err := fs.V3FindFile(parentPath) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + + if parentDir.IsZero() { + return primitive.NilObjectID, primitive.NilObjectID, errors.New("parent dir not found") + } + err = fs.V3AppendChildToDirectory(headerId, parentDir) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + return headerId, dataId, nil +} + +func (fs *WebFileSystem) v3WriteFileToMongo(file *V3FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { + resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + file.Data = resData.InsertedID.(primitive.ObjectID) + resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file) + if err != nil { + return primitive.NilObjectID, primitive.NilObjectID, err + } + return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err +} + +func (fs *WebFileSystem) V3WriteUpdateData(file *V3FileHeader, data interface{}) error { + update := bson.M{"$set": data} + // println("updating data file: " + file.MongoId.String()) + res, err := fs.webfsFilesData.UpdateByID(fs.ctx, file.Data, update) + if err != nil { + return err + } + if res.ModifiedCount < 1 { + return errors.New("no data updated") + } + return err +} + +func (fs *WebFileSystem) V3InitFS() error { //FIXME Can't set parent_id to itself + rootData := V3DirectoryData{ + MongoId: primitive.NewObjectID(), + Parent: primitive.NewObjectID(), + Children: []primitive.ObjectID{}, + } + rootHeader := V3FileHeader{ + MongoId: primitive.NewObjectID(), + Name: "/", + Type: "directory", + Icon: "", + } + _, _, err := fs.v3WriteFileToMongo(&rootHeader, &rootData) + if err != nil { + return err + } + return nil +} + +func (fs *WebFileSystem) V3GetRootDir() (*V3FileHeader, *V3DirectoryData, error) { + filter := primitive.M{ + "name": "/", + } + res := fs.webfsFilesTable.FindOne(fs.ctx, filter) + if res == nil { + return nil, nil, errors.New("TODO") //TODO + } + rootDir := V3FileHeader{} + err := res.Decode(&rootDir) + if res == nil { + return nil, nil, err + } + + filterData := primitive.M{ + "_id": rootDir.Data, + } + resData := fs.webfsFilesData.FindOne(fs.ctx, filterData) + if resData.Err() != nil { + return nil, nil, err + } + rootDirData := V3DirectoryData{} + err = resData.Decode(&rootDirData) + if err != nil { + return nil, nil, err + } + return &rootDir, &rootDirData, nil +} + +func (fs *WebFileSystem) V3Remove(filePath string) error { + parentPath := fs.GetParentPath(filePath) + parentDirId, err := fs.V3FindFile(parentPath) + if err != nil { + return err + } + //TODO: Check, if parent file is dir + parentDirData := V3DirectoryData{} + parentDir, err := fs.V3Read(parentDirId, &parentDirData) + if err != nil { + return err + } + + fileId, err := fs.V3FindFile(filePath) + if err != nil { + return err + } + newChildren := []primitive.ObjectID{} + for _, childId := range parentDirData.Children { + if childId != fileId { + newChildren = append(newChildren, childId) + } + } + + parentDirData.Children = newChildren + err = fs.V3WriteUpdateData(parentDir, parentDirData) + if err != nil { + return err + } + return nil +} + +func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error { + return errors.New("todo not implemented yet") +}