From ccaebdc667c533627f193eb62d2f08cf63d166b2 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Fri, 5 May 2023 01:11:11 +0300 Subject: [PATCH] Add file deletion (not work) --- resources/sys/finder/finder.js | 66 +++++++++++++++----------------- webfilesystem/webfilesystem.go | 69 +++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 37 deletions(-) diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index 9c021d2..1795815 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -30,19 +30,19 @@ class Finder{ newWindow.innerHTML = html this.fileView = new FileView(newWindow.querySelector(".FileTileView"), - (event) =>{ this.Open(event, false) }, + (event) =>{ this.Click(event) }, (event) =>{ this.RightClick(event) }, (event) =>{ this.FileUploading(event) }, ) this.OpenDir(this.path) - newWindow.querySelector("#BackButton").addEventListener('click', () =>{ - this.OpenPreviousDir() - }) + // newWindow.querySelector("#BackButton").addEventListener('click', () =>{ + // this.OpenPreviousDir() + // }) - newWindow.querySelector("#HomeButton").addEventListener('click', () =>{ - this.OpenDir(this.homePath) - }) + // newWindow.querySelector("#HomeButton").addEventListener('click', () =>{ + // this.OpenDir(this.homePath) + // }) this.windowElement = newWindow @@ -102,7 +102,7 @@ class Finder{ args[2].innerHTML = html this.fileView = new FileView(args[2].querySelector(".FileTileView"), (event) =>{ - this.Open(event, true) + this.Click(event, true) }) this.OpenDir(this.path) }) @@ -111,55 +111,53 @@ class Finder{ }) } - OpenPreviousDir(){ - if (this.pathHistory.length > 0){ - // console.log(this.pathHistory) - let path = this.pathHistory[this.pathHistory.length - 1] - // console.log(typeof( this.pathHistory)) - this.pathHistory.pop() - this.OpenDir(this.path) - } - } + // OpenPreviousDir(){ + // if (this.pathHistory.length > 0){ + // // console.log(this.pathHistory) + // let path = this.pathHistory[this.pathHistory.length - 1] + // // console.log(typeof( this.pathHistory)) + // this.pathHistory.pop() + // this.OpenDir(this.path) + // } + // } /** * @param {string} path */ OpenDir(path){ - this.pathHistory += this.path - this.path = path this.fileView.OpenFolder(this.path) } /** - * @param {MouseEvent} event - * @param {boolean} inNewWindow */ - Click(event, inNewWindow){ - + OpenNewDir(){ + WebDesktopEnvironment.Open("finder", [this.path]) + } + + /** + * @param {MouseEvent} event + */ + RightClick(event){ + this.CreateContextMenu(event.target, [event.clientY, event.clientX]) } /** * @param {MouseEvent} event - * @param {boolean} inNewWindow */ - Open(event, inNewWindow){ + Click(event){ let fileType = event.target.getAttribute("fileType") let fileName = event.target.getAttribute("name") switch (fileType) { case "directory": - if (inNewWindow){ - WebDesktopEnvironment.Open("finder", ["/home/user/" + fileName]) //FIXME this.path is shared for all windows - break - } - this.OpenDir(this.path +"/" + fileName) + WebDesktopEnvironment.Open("finder", [`${this.path}/${fileName}`]) //FIXME this.path is shared for all windows break case "blog-page": - WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName]) + WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`]) break case "deleteFile": fetch(`/fs/delete` + new URLSearchParams({ - path: "/home/user/" + fileName //FIXME + path: `${this.path}/${fileName}` //FIXME })) .then((response) => { console.log(response.status) @@ -181,10 +179,6 @@ class Finder{ break; } } - - RightClick(event){ - this.CreateContextMenu(event.target, [event.clientY, event.clientX]) - } CreateContextMenu(target, pos){ let context = "" diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index d258e16..be53e24 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -103,7 +103,7 @@ func (fs *WebFileSystem) CreateDirectory(path string) error { } fs.CreateFile(&directory, parentPath) - // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) + // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO // if err != nil { // return err // } @@ -113,6 +113,27 @@ func (fs *WebFileSystem) CreateDirectory(path string) error { 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) @@ -175,6 +196,37 @@ func (fs *WebFileSystem) GetParentPath(path string) string { return parentPath } +func (fs *WebFileSystem) Delete(filePath string) error { + splittedPath := fs.SplitPath(filePath) + parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") + + file, err := fs.Read(filePath) + if err != nil { + return err + } + + parentDir, err := fs.Read(parentPath) + if err != nil { + return err + } + + parentDir.Data.(primitive.D).Map() + + // update:= primitive.M{ + // "data.children": + // } + // filter := primitive.M{} + res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) + // res, err := fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) + if err != nil { + return err + } + if res.MatchedCount < 1 { + return errors.New("no documents found") + } + return nil +} + func (fs *WebFileSystem) Route(route *gin.RouterGroup) { route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request // fileName := ctx.Query("fileName") @@ -291,6 +343,21 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { 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 {