76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package check
|
|
|
|
import (
|
|
"errors"
|
|
"fusenapi/utils/format"
|
|
"regexp"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// CheckCategory 检查是否存在该类型
|
|
func CheckCategory(key string) bool {
|
|
_, ok := format.CategoryMap[key]
|
|
return ok
|
|
}
|
|
|
|
// CheckValidS3Key 检查s3的文件名是否符合要求
|
|
func CheckValidS3Key(key string) (bool, error) {
|
|
if utf8.RuneCountInString(key) > 1024 {
|
|
return false, errors.New("对象键长度超过 1024 字节")
|
|
}
|
|
|
|
if strings.HasPrefix(key, "/") || strings.HasPrefix(key, ".") {
|
|
return false, errors.New("对象键不应以 / 或 . 开头")
|
|
}
|
|
|
|
if !utf8.ValidString(key) {
|
|
return false, errors.New("对象键必须是 UTF-8 字符串")
|
|
}
|
|
|
|
if hasControlCharacters(key) {
|
|
return false, errors.New("对象键包含控制字符")
|
|
}
|
|
|
|
if hasInvalidPatterns(key) {
|
|
return false, errors.New("对象键包含无效的字符或模式")
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func hasControlCharacters(key string) bool {
|
|
controlCharPattern := "[\\x00-\\x1F\\x7F]"
|
|
match, _ := regexp.MatchString(controlCharPattern, key)
|
|
return match
|
|
}
|
|
|
|
func hasInvalidPatterns(key string) bool {
|
|
invalidPatterns := []string{"[\\s]+$", "^\\s+", "\\\\", "//", "\\.\\."}
|
|
for _, pattern := range invalidPatterns {
|
|
match, _ := regexp.MatchString(pattern, key)
|
|
if match {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
var checkModuleRe = regexp.MustCompile("[^\\.a-zA-Z_\\-0-9]")
|
|
|
|
// CheckModuleQuery 检查模块的json查询的格式
|
|
func CheckModuleQuery(moduleQuery string) bool {
|
|
return !checkModuleRe.MatchString(moduleQuery)
|
|
}
|
|
|
|
// 检测数据的范围是否符合要求
|
|
func CheckValueRange[T comparable](v T, rangevalues ...T) bool {
|
|
for _, rv := range rangevalues {
|
|
if v == rv {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|