diff --git a/wasm/main.go b/wasm/main.go index e9f1450c..445865e8 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -1,4 +1,4 @@ -//go:build js +//go:build wasm package main @@ -98,6 +98,18 @@ func registerWasmFunctions() { js.Global().Set("fileUpload", js.FuncOf(fileUpload)) js.Global().Set("fileDownload", js.FuncOf(fileDownload)) + js.Global().Set("groupDirPresent", js.FuncOf(groupDirPresent)) + js.Global().Set("groupDirMake", js.FuncOf(groupDirMake)) + js.Global().Set("groupDirRemove", js.FuncOf(groupDirRemove)) + js.Global().Set("groupDirList", js.FuncOf(groupDirList)) + js.Global().Set("groupDirStat", js.FuncOf(groupDirStat)) + + js.Global().Set("groupFileShare", js.FuncOf(groupFileShare)) + js.Global().Set("groupFileDelete", js.FuncOf(groupFileDelete)) + js.Global().Set("groupFileStat", js.FuncOf(groupFileStat)) + js.Global().Set("groupFileUpload", js.FuncOf(groupFileUpload)) + js.Global().Set("groupFileDownload", js.FuncOf(groupFileDownload)) + js.Global().Set("kvNewStore", js.FuncOf(kvNewStore)) js.Global().Set("kvList", js.FuncOf(kvList)) js.Global().Set("kvOpen", js.FuncOf(kvOpen)) @@ -1578,6 +1590,375 @@ func fileStat(_ js.Value, funcArgs []js.Value) interface{} { return promiseConstructor.New(handler) } +func groupDirPresent(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupDirPresent(sessionId, groupName, dirPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + + go func() { + present, err := api.IsDirPresent(groupName, dirPath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupDirRemove failed : %s", err.Error())) + return + } + + object := js.Global().Get("Object").New() + object.Set("present", present) + + resolve.Invoke(object) + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupDirMake(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupDirMake(sessionId, groupName, dirPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + + go func() { + err := api.Mkdir(groupName, dirPath, sessionId, 0, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupDirRemove failed : %s", err.Error())) + return + } + resolve.Invoke("directory created successfully") + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupDirRemove(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupDirRemove(sessionId, groupName, dirPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + + go func() { + err := api.RmDir(groupName, dirPath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupDirRemove failed : %s", err.Error())) + return + } + resolve.Invoke("directory removed successfully") + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupDirList(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupDirList(sessionId, groupName, dirPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + + go func() { + dirs, files, err := api.ListDir(groupName, dirPath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupDirList failed : %s", err.Error())) + return + } + filesList := js.Global().Get("Array").New(len(files)) + for i, v := range files { + file := js.Global().Get("Object").New() + file.Set("name", v.Name) + file.Set("contentType", v.ContentType) + file.Set("size", v.Size) + file.Set("blockSize", v.BlockSize) + file.Set("creationTime", v.CreationTime) + file.Set("modificationTime", v.ModificationTime) + file.Set("accessTime", v.AccessTime) + file.Set("mode", v.Mode) + filesList.SetIndex(i, file) + } + dirsList := js.Global().Get("Array").New(len(dirs)) + for i, v := range dirs { + dir := js.Global().Get("Object").New() + dir.Set("name", v.Name) + dir.Set("contentType", v.ContentType) + dir.Set("size", v.Size) + dir.Set("mode", v.Mode) + dir.Set("blockSize", v.BlockSize) + dir.Set("creationTime", v.CreationTime) + dir.Set("modificationTime", v.ModificationTime) + dir.Set("accessTime", v.AccessTime) + dirsList.SetIndex(i, dir) + } + object := js.Global().Get("Object").New() + object.Set("files", filesList) + object.Set("dirs", dirsList) + + resolve.Invoke(object) + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupDirStat(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupDirStat(sessionId, groupName, dirPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + + go func() { + stat, err := api.DirectoryStat(groupName, dirPath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupDirStat failed : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + object.Set("podName", stat.PodName) + object.Set("dirPath", stat.DirPath) + object.Set("dirName", stat.DirName) + object.Set("mode", stat.Mode) + object.Set("creationTime", stat.CreationTime) + object.Set("modificationTime", stat.ModificationTime) + object.Set("accessTime", stat.AccessTime) + object.Set("noOfDirectories", stat.NoOfDirectories) + object.Set("noOfFiles", stat.NoOfFiles) + + resolve.Invoke(object) + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupFileDownload(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupFileDownload(sessionId, groupName, filePath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + filePath := funcArgs[2].String() + + go func() { + r, _, err := api.DownloadFile(groupName, filePath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileDownload failed : %s", err.Error())) + return + } + defer r.Close() + + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(r) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileDownload failed : %s", err.Error())) + return + } + a := js.Global().Get("Uint8Array").New(buf.Len()) + js.CopyBytesToJS(a, buf.Bytes()) + resolve.Invoke(a) + }() + return nil + }) + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupFileUpload(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if len(funcArgs) != 8 { + reject.Invoke("not enough arguments. \"groupFileUpload(sessionId, groupName, dirPath, file, name, size, blockSize, compression)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + array := funcArgs[3] + fileName := funcArgs[4].String() + size := funcArgs[5].Int() + blockSize := funcArgs[6].String() + compression := funcArgs[7].String() + if compression != "" { + if compression != "snappy" && compression != "gzip" { + reject.Invoke("invalid compression value") + return nil + } + } + bs, err := humanize.ParseBytes(blockSize) + if err != nil { + reject.Invoke("invalid blockSize value") + return nil + } + + go func() { + inBuf := make([]uint8, array.Get("byteLength").Int()) + js.CopyBytesToGo(inBuf, array) + reader := bytes.NewReader(inBuf) + + err := api.UploadFile(groupName, fileName, sessionId, int64(size), reader, dirPath, compression, uint32(bs), 0, true, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileUpload failed : %s", err.Error())) + return + } + resolve.Invoke("file uploaded") + }() + return nil + }) + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupFileShare(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 4 { + reject.Invoke("not enough arguments. \"groupFileShare(sessionId, groupName, dirPath, destinationUser)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + dirPath := funcArgs[2].String() + destinationUser := funcArgs[3].String() + + go func() { + ref, err := api.ShareFile(groupName, dirPath, destinationUser, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileShare failed : %s", err.Error())) + return + } + + object := js.Global().Get("Object").New() + object.Set("fileSharingReference", ref) + + resolve.Invoke(object) + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupFileDelete(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupFileDelete(sessionId, groupName, podFileWithPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + filePath := funcArgs[2].String() + + go func() { + err := api.DeleteFile(groupName, filePath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileDelete failed : %s", err.Error())) + return + } + resolve.Invoke("file deleted successfully") + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func groupFileStat(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"groupFileStat(sessionId, groupName, podFileWithPath)\"") + return nil + } + sessionId := funcArgs[0].String() + groupName := funcArgs[1].String() + filePath := funcArgs[2].String() + + go func() { + stat, err := api.FileStat(groupName, filePath, sessionId, true) + if err != nil { + reject.Invoke(fmt.Sprintf("groupFileStat failed : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + object.Set("podName", stat.PodName) + object.Set("mode", stat.Mode) + object.Set("filePath", stat.FilePath) + object.Set("fileName", stat.FileName) + object.Set("fileSize", stat.FileSize) + object.Set("blockSize", stat.BlockSize) + object.Set("compression", stat.Compression) + object.Set("contentType", stat.ContentType) + object.Set("creationTime", stat.CreationTime) + object.Set("modificationTime", stat.ModificationTime) + object.Set("accessTime", stat.AccessTime) + + resolve.Invoke(object) + }() + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + func kvNewStore(_ js.Value, funcArgs []js.Value) interface{} { handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { resolve := args[0]