[add] upload & download compute
This commit is contained in:
parent
a1f1d74f01
commit
ae6c234dd1
3 changed files with 156 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ var defaults = []CArg{
|
||||||
{IsExp: true, key: "props", value: nil},
|
{IsExp: true, key: "props", value: nil},
|
||||||
{IsExp: true, key: "type", value: nil},
|
{IsExp: true, key: "type", value: nil},
|
||||||
{IsExp: true, key: "id", value: nil},
|
{IsExp: true, key: "id", value: nil},
|
||||||
|
{IsExp: true, key: "path", value: nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExp create a new exp CArg
|
// NewExp create a new exp CArg
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -10,6 +11,8 @@ var hanlders = map[string]ComputeHanlder{
|
||||||
"Trim": Trim,
|
"Trim": Trim,
|
||||||
"Hide": Hide,
|
"Hide": Hide,
|
||||||
"Concat": Concat,
|
"Concat": Concat,
|
||||||
|
"Download": Download,
|
||||||
|
"Upload": Upload,
|
||||||
"QueryString": Trim,
|
"QueryString": Trim,
|
||||||
"ImagesView": Trim,
|
"ImagesView": Trim,
|
||||||
"ImagesEdit": Trim,
|
"ImagesEdit": Trim,
|
||||||
|
|
@ -61,3 +64,153 @@ func Get(args ...interface{}) (interface{}, error) {
|
||||||
func Hide(args ...interface{}) (interface{}, error) {
|
func Hide(args ...interface{}) (interface{}, error) {
|
||||||
return nil, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,7 @@ func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, g
|
||||||
|
|
||||||
data.Set("id", id)
|
data.Set("id", id)
|
||||||
data.Set("value", res[key])
|
data.Set("value", res[key])
|
||||||
|
data.Set("path", fmt.Sprintf("%s.%s", path, unit.Name))
|
||||||
data.Merge(any.MapOf(field.Edit.Map()).MapStrAny.Dot())
|
data.Merge(any.MapOf(field.Edit.Map()).MapStrAny.Dot())
|
||||||
new, err := field.Edit.Compute.Value(data, process.Sid, process.Global)
|
new, err := field.Edit.Compute.Value(data, process.Sid, process.Global)
|
||||||
if err != nil {
|
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("value", res[key])
|
||||||
data.Set("id", id)
|
data.Set("id", id)
|
||||||
|
data.Set("path", fmt.Sprintf("%s.%s", path, unit.Name))
|
||||||
data.Merge(any.MapOf(field.View.Map()).MapStrAny.Dot())
|
data.Merge(any.MapOf(field.View.Map()).MapStrAny.Dot())
|
||||||
new, err := field.View.Compute.Value(data, process.Sid, process.Global)
|
new, err := field.View.Compute.Value(data, process.Sid, process.Global)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue