-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
107 lines (94 loc) · 3.1 KB
/
checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package upload
import (
"fmt"
"strings"
"time"
"github.com/webx-top/echo"
"github.com/admpub/color"
"github.com/admpub/log"
"github.com/admpub/nging/application/registry/upload/table"
)
// UploadLinkLifeTime 上传链接生存时间
var UploadLinkLifeTime int64 = 86400
// Checker 验证并生成子文件夹名称和文件名称
type Checker func(echo.Context, table.TableInfoStorer) (subdir string, name string, err error)
// DefaultChecker 默认Checker
var DefaultChecker = func(ctx echo.Context, t table.TableInfoStorer) (subdir string, name string, err error) {
refid := ctx.Formx(`refid`).String()
timestamp := ctx.Formx(`time`).Int64()
if len(refid) == 0 {
refid = `0`
}
// Token(ctx.Queries())
// 验证签名(避免上传接口被滥用)
if ctx.Form(`token`) != Token(`refid`, refid, `time`, timestamp) {
err = ctx.E(`令牌错误`)
return
}
if time.Now().Local().Unix()-timestamp > UploadLinkLifeTime {
err = ctx.E(`上传网址已过期`)
return
}
subdir = fmt.Sprint(refid) + `/`
//subdir = time.Now().Format(`2006/01/02/`)
t.SetTableID(refid)
return
}
// ConfigChecker 系统配置文件上传
func ConfigChecker(ctx echo.Context, t table.TableInfoStorer) (subdir string, name string, err error) {
group := ctx.Form(`group`)
key := ctx.Form(`key`)
timestamp := ctx.Formx(`time`).Int64()
// 验证签名(避免上传接口被滥用)
if ctx.Form(`token`) != Token(`group`, group, `key`, key, `refid`, `0`, `time`, timestamp) {
err = ctx.E(`令牌错误`)
return
}
if time.Now().Local().Unix()-timestamp > UploadLinkLifeTime {
err = ctx.E(`上传网址已过期`)
return
}
subdir = key + `/`
t.SetTableID(group + `.` + key)
t.SetTableName(`nging_config`)
t.SetFieldName(`value`)
return
}
func CheckerRegister(typ string, checker Checker, fieldNames ...string) {
log.Info(color.GreenString(`checker.register:`), typ)
if len(fieldNames) > 0 {
SubdirGet(typ).SetChecker(checker, fieldNames...)
return
}
tableName, fieldName, _ := GetTableInfo(typ)
info := SubdirGet(tableName)
info.SetChecker(checker, fieldName)
}
func CheckerGet(typ string, defaults ...string) Checker {
s := SubdirGet(typ)
if s != nil {
return s.MustChecker()
}
if len(defaults) == 0 {
tmp := strings.SplitN(typ, `.`, 2)
if len(tmp) == 2 {
return CheckerGet(tmp[0])
}
return DefaultChecker
}
return CheckerGet(defaults[0])
}