From ae6c234dd10bd193bb6dece434e2ba51d5fa3e9d Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 16 Nov 2022 13:39:17 +0800 Subject: [PATCH] [add] upload & download compute --- widgets/component/compute.go | 1 + widgets/component/handlers.go | 153 ++++++++++++++++++++++++++++++++++ widgets/compute/compute.go | 2 + 3 files changed, 156 insertions(+) diff --git a/widgets/component/compute.go b/widgets/component/compute.go index 785397f0..bf6636c2 100644 --- a/widgets/component/compute.go +++ b/widgets/component/compute.go @@ -16,6 +16,7 @@ var defaults = []CArg{ {IsExp: true, key: "props", value: nil}, {IsExp: true, key: "type", value: nil}, {IsExp: true, key: "id", value: nil}, + {IsExp: true, key: "path", value: nil}, } // NewExp create a new exp CArg diff --git a/widgets/component/handlers.go b/widgets/component/handlers.go index 7695c592..70d8a60d 100644 --- a/widgets/component/handlers.go +++ b/widgets/component/handlers.go @@ -2,6 +2,7 @@ package component import ( "fmt" + "net/url" "strings" ) @@ -10,6 +11,8 @@ var hanlders = map[string]ComputeHanlder{ "Trim": Trim, "Hide": Hide, "Concat": Concat, + "Download": Download, + "Upload": Upload, "QueryString": Trim, "ImagesView": Trim, "ImagesEdit": Trim, @@ -61,3 +64,153 @@ func Get(args ...interface{}) (interface{}, error) { func Hide(args ...interface{}) (interface{}, error) { return nil, nil } + +// Upload return the file download path +func Upload(args ...interface{}) (interface{}, error) { + + if len(args) < 5 { + return nil, fmt.Errorf("Upload args[0]~args[4] is required") + } + + if args[0] == nil { + return "", nil + } + + files := []string{} + switch values := args[0].(type) { + case []interface{}: + for i := range values { + file := fmt.Sprintf("%v", values[i]) + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + + case []string: + for _, file := range values { + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + + case string: + if values != "" { + files = append(files, fmt.Sprintf("%v", values)) + } + break + + case map[string]interface{}: + for name := range values { + file := fmt.Sprintf("%v", values[name]) + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + } + + id, ok := args[3].(string) + if !ok { + return nil, fmt.Errorf("Upload args[3] is not string") + } + + path, ok := args[4].(string) + if !ok { + return nil, fmt.Errorf("Upload args[4] is not string") + } + + preifx := fmt.Sprintf("/api/__yao/table/%s/download/%s?name=", id, url.QueryEscape(path)) + res := []string{} + for _, file := range files { + file = strings.TrimSpace(file) + if strings.HasPrefix(file, "http") { + res = append(res, file) + continue + } + res = append(res, strings.TrimPrefix(file, preifx)) + } + + if len(res) == 0 { + return nil, nil + } + + return res, nil +} + +// Download return the file download path +func Download(args ...interface{}) (interface{}, error) { + + if len(args) < 5 { + return nil, fmt.Errorf("Download args[0]~args[4] is required") + } + + if args[0] == nil { + return "", nil + } + + files := []string{} + switch values := args[0].(type) { + case []interface{}: + for i := range values { + file := fmt.Sprintf("%v", values[i]) + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + + case []string: + for _, file := range values { + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + + case string: + if values != "" { + files = append(files, fmt.Sprintf("%v", values)) + } + break + + case map[string]interface{}: + for name := range values { + file := fmt.Sprintf("%v", values[name]) + if file != "" { + files = append(files, fmt.Sprintf("%v", file)) + } + } + break + } + + id, ok := args[3].(string) + if !ok { + return nil, fmt.Errorf("Download args[3] is not string") + } + + path, ok := args[4].(string) + if !ok { + return nil, fmt.Errorf("Download args[4] is not string") + } + + res := []string{} + for _, file := range files { + + file = strings.TrimSpace(file) + if strings.HasPrefix(file, "http") { + res = append(res, file) + continue + } + + file = fmt.Sprintf("/api/__yao/table/%s/download/%s?name=%s", id, url.QueryEscape(path), file) + res = append(res, file) + } + + if len(res) == 0 { + return nil, nil + } + + return res, nil +} diff --git a/widgets/compute/compute.go b/widgets/compute/compute.go index 5e672f50..901d7a60 100644 --- a/widgets/compute/compute.go +++ b/widgets/compute/compute.go @@ -102,6 +102,7 @@ func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, g data.Set("id", id) data.Set("value", res[key]) + data.Set("path", fmt.Sprintf("%s.%s", path, unit.Name)) data.Merge(any.MapOf(field.Edit.Map()).MapStrAny.Dot()) new, err := field.Edit.Compute.Value(data, process.Sid, process.Global) if err != nil { @@ -243,6 +244,7 @@ func (c *Computable) viewRow(name string, process *gou.Process, res map[string]i data.Set("value", res[key]) data.Set("id", id) + data.Set("path", fmt.Sprintf("%s.%s", path, unit.Name)) data.Merge(any.MapOf(field.View.Map()).MapStrAny.Dot()) new, err := field.View.Compute.Value(data, process.Sid, process.Global) if err != nil {