Add file uploading
This commit is contained in:
parent
313be711a9
commit
b496ce2ab2
@ -5,6 +5,7 @@ import (
|
|||||||
"personalwebsite/apps"
|
"personalwebsite/apps"
|
||||||
"personalwebsite/wde"
|
"personalwebsite/wde"
|
||||||
"personalwebsite/webfilesystem"
|
"personalwebsite/webfilesystem"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"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 {
|
func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H {
|
||||||
islands := [][]wde.ContexMenuRow{}
|
islands := [][]wde.ContexMenuRow{} //FIXME
|
||||||
switch context {
|
switch context {
|
||||||
case "FileTileView":
|
case "FileTileView":
|
||||||
islands = [][]wde.ContexMenuRow{
|
islands = [][]wde.ContexMenuRow{
|
||||||
|
{
|
||||||
|
{
|
||||||
|
Label: "Get Info",
|
||||||
|
Action: strings.Join([]string{"getInfo"}[:], ","),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
Label: "New Directory",
|
Label: "New Directory",
|
||||||
Action: []string{},
|
Action: strings.Join([]string{"newDir"}[:], ","),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
case "directory":
|
case "directory":
|
||||||
islands = [][]wde.ContexMenuRow{
|
islands = [][]wde.ContexMenuRow{
|
||||||
{
|
{
|
||||||
{Label: "Test"},
|
{
|
||||||
{Label: "Delete"},
|
Label: "Test",
|
||||||
|
Action: strings.Join([]string{""}[:], ","),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Label: "Delete",
|
||||||
|
Action: strings.Join([]string{""}[:], ","),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
Label: "Get Info",
|
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{
|
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{
|
return gin.H{
|
||||||
"Islands": islands,
|
"Islands": islands,
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,9 @@ class Finder{
|
|||||||
|
|
||||||
this.fileView = new FileView(newWindow.querySelector(".FileTileView"),
|
this.fileView = new FileView(newWindow.querySelector(".FileTileView"),
|
||||||
(event) =>{ this.Open(event, false) },
|
(event) =>{ this.Open(event, false) },
|
||||||
(event) =>{ this.RightClick(event) })
|
(event) =>{ this.RightClick(event) },
|
||||||
|
(event) =>{ this.FileUploading(event) },
|
||||||
|
)
|
||||||
this.OpenDir(this.path)
|
this.OpenDir(this.path)
|
||||||
|
|
||||||
newWindow.querySelector("#BackButton").addEventListener('click', () =>{
|
newWindow.querySelector("#BackButton").addEventListener('click', () =>{
|
||||||
@ -47,6 +49,7 @@ class Finder{
|
|||||||
if (!WebDesktopEnvironment.isMobile){
|
if (!WebDesktopEnvironment.isMobile){
|
||||||
// let scrollBar = new WdeScrollBar(newWindow.children[1].children[1], newWindow.children[1].children[0])// TODO to querry selector
|
// let scrollBar = new WdeScrollBar(newWindow.children[1].children[1], newWindow.children[1].children[0])// TODO to querry selector
|
||||||
// console.log(newWindow.querySelector("#closeWindowButton"))
|
// console.log(newWindow.querySelector("#closeWindowButton"))
|
||||||
|
|
||||||
newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) {
|
newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) {
|
||||||
|
|
||||||
WebDesktopEnvironment.CloseWindow(newWindow)
|
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
|
* @param {string[]} args
|
||||||
*/
|
*/
|
||||||
@ -128,6 +157,17 @@ class Finder{
|
|||||||
case "blog-page":
|
case "blog-page":
|
||||||
WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName])
|
WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName])
|
||||||
break
|
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":
|
// case "app":
|
||||||
// //TODO get real id
|
// //TODO get real id
|
||||||
// WebDesktopEnvironment.Open("personal-properties", [])
|
// WebDesktopEnvironment.Open("personal-properties", [])
|
||||||
@ -143,13 +183,11 @@ class Finder{
|
|||||||
}
|
}
|
||||||
|
|
||||||
RightClick(event){
|
RightClick(event){
|
||||||
// console.log(event)
|
|
||||||
// console.log()
|
|
||||||
this.CreateContextMenu(event.target, [event.clientY, event.clientX])
|
this.CreateContextMenu(event.target, [event.clientY, event.clientX])
|
||||||
}
|
}
|
||||||
|
|
||||||
CreateContextMenu(target, pos){
|
CreateContextMenu(target, pos){
|
||||||
let context
|
let context = ""
|
||||||
if (target.classList.contains("FileTileView"))
|
if (target.classList.contains("FileTileView"))
|
||||||
{
|
{
|
||||||
context = "FileTileView"
|
context = "FileTileView"
|
||||||
@ -181,7 +219,10 @@ class Finder{
|
|||||||
|
|
||||||
overlay.addEventListener('click',(event) => {
|
overlay.addEventListener('click',(event) => {
|
||||||
if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks??
|
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()
|
overlay.remove()
|
||||||
})
|
})
|
||||||
|
@ -44,6 +44,35 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
|
|||||||
return errors.New("this filetype not allowed")
|
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 {
|
// func (fs *WebFileSystem) CreateMiniatures(parentDir string, file string) error {
|
||||||
|
@ -176,6 +176,44 @@ func (fs *WebFileSystem) GetParentPath(path string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
|
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) {
|
route.GET("writeFile", func(ctx *gin.Context) {
|
||||||
parentPath := ctx.Query("parentPath")
|
parentPath := ctx.Query("parentPath")
|
||||||
if parentPath == "" {
|
if parentPath == "" {
|
||||||
@ -263,16 +301,13 @@ func (fs *WebFileSystem) Validate() error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
cur, err := fs.webfsCollection.Find(context.Background(), filter)
|
cur, err := fs.webfsCollection.Find(context.Background(), filter)
|
||||||
defer cur.Close(context.Background())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer cur.Close(context.Background())
|
||||||
|
|
||||||
directories := []*WebFSFile{}
|
directories := []*WebFSFile{}
|
||||||
// err = cur.All(context.Background(), directories)
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Println(err.Error())
|
|
||||||
// }
|
|
||||||
for cur.Next(context.Background()) {
|
for cur.Next(context.Background()) {
|
||||||
dir := &WebFSFile{}
|
dir := &WebFSFile{}
|
||||||
|
|
||||||
@ -285,7 +320,6 @@ func (fs *WebFileSystem) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range directories {
|
for _, d := range directories {
|
||||||
// println(d.Name)
|
|
||||||
fs.validateDir(d)
|
fs.validateDir(d)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -295,11 +329,9 @@ func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
|
|||||||
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
|
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
|
||||||
_ = kek
|
_ = kek
|
||||||
|
|
||||||
// FolderData
|
|
||||||
children := []primitive.ObjectID{}
|
children := []primitive.ObjectID{}
|
||||||
counter := 0
|
counter := 0
|
||||||
for _, v := range kek {
|
for _, v := range kek {
|
||||||
// println(v.(primitive.ObjectID).String())
|
|
||||||
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
|
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
counter++
|
counter++
|
||||||
|
Loading…
Reference in New Issue
Block a user