[add] upload & download compute

This commit is contained in:
Max 2022-11-16 13:39:17 +08:00
parent a1f1d74f01
commit ae6c234dd1
3 changed files with 156 additions and 0 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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 {