fusenapi/utils/check/check.go
2023-07-06 19:47:27 +08:00

59 lines
1.3 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
}