From b496ce2ab2d3ed7bf85ec152c8a5f3ce45fee53f Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 4 May 2023 22:49:22 +0300 Subject: [PATCH] Add file uploading --- apps/finder/finder.go | 43 ++++++++++++++++++++++------ resources/sys/finder/finder.js | 51 ++++++++++++++++++++++++++++++---- webfilesystem/fileuploading.go | 29 +++++++++++++++++++ webfilesystem/webfilesystem.go | 48 ++++++++++++++++++++++++++------ 4 files changed, 149 insertions(+), 22 deletions(-) diff --git a/apps/finder/finder.go b/apps/finder/finder.go index c8de1e8..7b4e487 100644 --- a/apps/finder/finder.go +++ b/apps/finder/finder.go @@ -5,6 +5,7 @@ import ( "personalwebsite/apps" "personalwebsite/wde" "personalwebsite/webfilesystem" + "strings" "github.com/gin-gonic/gin" ) @@ -36,27 +37,39 @@ func (f *FinderApplication) Render(isMobile bool) gin.H { } func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H { - islands := [][]wde.ContexMenuRow{} + islands := [][]wde.ContexMenuRow{} //FIXME switch context { case "FileTileView": islands = [][]wde.ContexMenuRow{ + { + { + Label: "Get Info", + Action: strings.Join([]string{"getInfo"}[:], ","), + }, + }, { { Label: "New Directory", - Action: []string{}, + Action: strings.Join([]string{"newDir"}[:], ","), }, }, } case "directory": islands = [][]wde.ContexMenuRow{ { - {Label: "Test"}, - {Label: "Delete"}, + { + Label: "Test", + Action: strings.Join([]string{""}[:], ","), + }, + { + Label: "Delete", + Action: strings.Join([]string{""}[:], ","), + }, }, { { Label: "Get Info", - Action: []string{"openApp", "properties"}, + Action: strings.Join([]string{""}[:], ","), }, }, } @@ -64,20 +77,32 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H islands = [][]wde.ContexMenuRow{ { { - Label: "temp Menu 1", + Label: "temp Menu 1", + Action: strings.Join([]string{""}[:], ","), }, { - Label: "temp Menu 2", + Label: "temp Menu 2", + Action: strings.Join([]string{""}[:], ","), }, { - Label: "temp Menu 3", + Label: "temp Menu 3", + Action: strings.Join([]string{""}[:], ","), }, { - Label: "temp Menu 4", + Label: "temp Menu 4", + Action: strings.Join([]string{""}[:], ","), }, }, } } + + islands = append(islands, []wde.ContexMenuRow{ + { + Label: "Delete File", + Action: strings.Join([]string{"deleteFile"}[:], ";"), + }, + }) + return gin.H{ "Islands": islands, } diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index f7a347f..9c021d2 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -31,7 +31,9 @@ class Finder{ this.fileView = new FileView(newWindow.querySelector(".FileTileView"), (event) =>{ this.Open(event, false) }, - (event) =>{ this.RightClick(event) }) + (event) =>{ this.RightClick(event) }, + (event) =>{ this.FileUploading(event) }, + ) this.OpenDir(this.path) newWindow.querySelector("#BackButton").addEventListener('click', () =>{ @@ -47,6 +49,7 @@ class Finder{ if (!WebDesktopEnvironment.isMobile){ // let scrollBar = new WdeScrollBar(newWindow.children[1].children[1], newWindow.children[1].children[0])// TODO to querry selector // console.log(newWindow.querySelector("#closeWindowButton")) + newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) { WebDesktopEnvironment.CloseWindow(newWindow) @@ -59,6 +62,32 @@ class Finder{ }) } + /** + * @param {DataTransferItemList} filesList + */ + + FileUploading(filesList){ + let formData = new FormData() + // console.log(filesList) + for (let i = 0; i < filesList.length; i++) { + const element = filesList[i]; + formData.append("file", element.getAsFile()) + console.log(formData) + } + // console.log(formData) + // formData.append("photo", photo); + fetch('/fs/upload/?' + new URLSearchParams({ + path: '/home/user/', + }), + { + method: "POST", + body: formData + }) + .catch((error) => { + WebDesktopEnvironment.Alert(error); + }) + } + /** * @param {string[]} args */ @@ -128,6 +157,17 @@ class Finder{ case "blog-page": WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName]) break + case "deleteFile": + fetch(`/fs/delete` + new URLSearchParams({ + path: "/home/user/" + fileName //FIXME + })) + .then((response) => { + console.log(response.status) + }) + .catch((error) => { + WebDesktopEnvironment.Alert(error); + }) + break // case "app": // //TODO get real id // WebDesktopEnvironment.Open("personal-properties", []) @@ -143,13 +183,11 @@ class Finder{ } RightClick(event){ - // console.log(event) - // console.log() this.CreateContextMenu(event.target, [event.clientY, event.clientX]) } CreateContextMenu(target, pos){ - let context + let context = "" if (target.classList.contains("FileTileView")) { context = "FileTileView" @@ -181,7 +219,10 @@ class Finder{ overlay.addEventListener('click',(event) => { if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks?? - //TODO + switch (event.target.children[0].getAttribute("action")) { + default: + break; + } } overlay.remove() }) diff --git a/webfilesystem/fileuploading.go b/webfilesystem/fileuploading.go index 2a2ca26..2448498 100644 --- a/webfilesystem/fileuploading.go +++ b/webfilesystem/fileuploading.go @@ -44,6 +44,35 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error { return errors.New("this filetype not allowed") } +} +func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error { + extension := fs.GetExtension(fileName) + switch extension { + case "jpg": + fallthrough + case "jpeg": + newFile := WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: fileName, + Type: "jpeg", + Data: file, + Icon: "", + } + err := fs.CreateFile(&newFile, path) + return err + case "png": + newFile := WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: fileName, + Type: "png", + Data: file, + } + err := fs.CreateFile(&newFile, path) + return err + default: + return errors.New("this filetype not allowed") + } + } // func (fs *WebFileSystem) CreateMiniatures(parentDir string, file string) error { diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 6dfdbc0..d258e16 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -176,6 +176,44 @@ func (fs *WebFileSystem) GetParentPath(path string) string { } 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 == "" { @@ -263,16 +301,13 @@ func (fs *WebFileSystem) Validate() error { }, } cur, err := fs.webfsCollection.Find(context.Background(), filter) - defer cur.Close(context.Background()) if err != nil { return err } + defer cur.Close(context.Background()) directories := []*WebFSFile{} - // err = cur.All(context.Background(), directories) - // if err != nil { - // fmt.Println(err.Error()) - // } + for cur.Next(context.Background()) { dir := &WebFSFile{} @@ -285,7 +320,6 @@ func (fs *WebFileSystem) Validate() error { } for _, d := range directories { - // println(d.Name) fs.validateDir(d) } return nil @@ -295,11 +329,9 @@ 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++