65 Star 396 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
checker.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-10-27 01:21 . WIP: improved file upload
/*
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(`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])
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.0

搜索帮助