diff --git a/biz/config/config.go b/biz/config/config.go index d330775..8d10f8a 100644 --- a/biz/config/config.go +++ b/biz/config/config.go @@ -67,14 +67,14 @@ func (j Job) GetFileName(params map[string]interface{}) string { field := string(b[1 : len(b)-1]) if val, ok := params[field]; ok { - return []byte(toString(val, false)) + return []byte(toString(val, "20060102")) } return b }) //安全命名 path, name := filepath.Split(string(fileName)) - name, err := filenamify.Filenamify(name, filenamify.Options{Replacement: ""}) + name, err := filenamify.Filenamify(name, filenamify.Options{Replacement: "-"}) if err != nil { log.Printf("不安全的文件名:%s", err.Error()) } @@ -105,14 +105,22 @@ func (t Task) GetSql(params map[string]interface{}) string { return sql } -func toString(parm interface{}, timestamp bool) string { +func toString(parm interface{}, timestamp interface{}) string { switch p := parm.(type) { case time.Time: - if timestamp { - return strconv.FormatInt(p.Unix(), 10) - } else { - return p.Format("2006-01-02 15:04:05") + switch t := timestamp.(type) { + case bool: + if t { + return strconv.FormatInt(p.Unix(), 10) + } else { + return p.Format("2006-01-02 15:04:05") + } + case string: + return p.Format(t) + default: + return "" } + case string: return p case int: diff --git a/biz/config/job_test.go b/biz/config/job_test.go new file mode 100644 index 0000000..3912a74 --- /dev/null +++ b/biz/config/job_test.go @@ -0,0 +1,49 @@ +package config + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestJob_GetFileName(t *testing.T) { + t.Run("normal", func(t *testing.T) { + + j := &Job{ + File: "/var/www/aa-{begin}-{end}-{task}.xls", + } + + name := j.GetFileName(map[string]interface{}{ + "begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + "end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + "task": 1, + }) + assert.Equal(t, "/var/www/aa-20230101-20230101-1.xls", name) + }) + + t.Run("deletion", func(t *testing.T) { + + j := &Job{ + File: "/var/www/aa-{begin}-{end}-{task}*.xls", + } + + name := j.GetFileName(map[string]interface{}{ + "begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + "end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + }) + assert.Equal(t, "/var/www/aa-20230101-20230101-{task}-.xls", name) + }) + + t.Run("err", func(t *testing.T) { + + j := &Job{ + File: "/var/www/aa-{begin}-{end}-{task}*.xls", + } + + name := j.GetFileName(map[string]interface{}{ + "begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + "end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + }) + assert.Equal(t, "/var/www/aa-20230101-20230101-{task}-.xls", name) + }) +}