feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
+27
-27
@@ -192,20 +192,20 @@ func sanitizeLikePattern(input string) (string, error) {
|
||||
func validateLikePattern(input string) error {
|
||||
// 1. 连续的 % 直接拒绝
|
||||
if strings.Contains(input, "%%") {
|
||||
return errors.New("搜索模式中不允许包含连续的 % 通配符")
|
||||
return common.NewMessage("The search pattern cannot contain consecutive % wildcards")
|
||||
}
|
||||
|
||||
// 2. 统计 % 数量,不得超过 2
|
||||
count := strings.Count(input, "%")
|
||||
if count > 2 {
|
||||
return errors.New("搜索模式中最多允许包含 2 个 % 通配符")
|
||||
return common.NewMessage("The search pattern can contain at most 2 % wildcards")
|
||||
}
|
||||
|
||||
// 3. 含 % 时,去掉 % 后关键词长度必须 >= 2
|
||||
if count > 0 {
|
||||
stripped := strings.ReplaceAll(input, "%", "")
|
||||
if len(stripped) < 2 {
|
||||
return errors.New("使用模糊搜索时,关键词长度至少为 2 个字符")
|
||||
return common.NewMessage("Fuzzy search keywords must be at least 2 characters long")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,11 +233,11 @@ func SearchUserTokens(userId int, keyword string, token string, offset int, limi
|
||||
if hasFuzzy {
|
||||
count, err := CountUserTokens(userId)
|
||||
if err != nil {
|
||||
common.SysLog("failed to count user tokens: " + err.Error())
|
||||
return nil, 0, errors.New("获取令牌数量失败")
|
||||
common.SysLog(common.LogText("failed to count user tokens: %s", err.Error()))
|
||||
return nil, 0, common.NewMessage("Failed to get the token count")
|
||||
}
|
||||
if int(count) > maxTokens {
|
||||
return nil, 0, errors.New("令牌数量超过上限,仅允许精确搜索,请勿使用 % 通配符")
|
||||
return nil, 0, common.NewMessage("Token count exceeds the limit. Only exact search is allowed, do not use the % wildcard")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,15 +262,15 @@ func SearchUserTokens(userId int, keyword string, token string, offset int, limi
|
||||
// 先查匹配总数(用于分页,受 maxTokens 上限保护,避免全表 COUNT)
|
||||
err = baseQuery.Limit(maxTokens).Count(&total).Error
|
||||
if err != nil {
|
||||
common.SysError("failed to count search tokens: " + err.Error())
|
||||
return nil, 0, errors.New("搜索令牌失败")
|
||||
common.SysError(common.LogText("failed to count search tokens: %s", err.Error()))
|
||||
return nil, 0, common.NewMessage("Failed to search tokens")
|
||||
}
|
||||
|
||||
// 再分页查数据
|
||||
err = baseQuery.Order("id desc").Offset(offset).Limit(limit).Find(&tokens).Error
|
||||
if err != nil {
|
||||
common.SysError("failed to search tokens: " + err.Error())
|
||||
return nil, 0, errors.New("搜索令牌失败")
|
||||
common.SysError(common.LogText("failed to search tokens: %s", err.Error()))
|
||||
return nil, 0, common.NewMessage("Failed to search tokens")
|
||||
}
|
||||
return tokens, total, nil
|
||||
}
|
||||
@@ -291,7 +291,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
token.Status = common.TokenStatusExpired
|
||||
err := token.SelectUpdate()
|
||||
if err != nil {
|
||||
common.SysLog("failed to update token status" + err.Error())
|
||||
common.SysLog(common.LogText("failed to update token status%s", err.Error()))
|
||||
}
|
||||
}
|
||||
return token, ErrTokenInvalid
|
||||
@@ -301,14 +301,14 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
token.Status = common.TokenStatusExhausted
|
||||
err := token.SelectUpdate()
|
||||
if err != nil {
|
||||
common.SysLog("failed to update token status" + err.Error())
|
||||
common.SysLog(common.LogText("failed to update token status%s", err.Error()))
|
||||
}
|
||||
}
|
||||
return token, ErrTokenInvalid
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
common.SysLog("ValidateUserToken: failed to get token: " + err.Error())
|
||||
common.SysLog(common.LogText("ValidateUserToken: failed to get token: %s", err.Error()))
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrTokenInvalid
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
|
||||
func GetTokenByIds(id int, userId int) (*Token, error) {
|
||||
if id == 0 || userId == 0 {
|
||||
return nil, errors.New("id 或 userId 为空!")
|
||||
return nil, common.NewMessage("Token ID or user ID is empty")
|
||||
}
|
||||
token := Token{Id: id, UserId: userId}
|
||||
var err error = nil
|
||||
@@ -327,7 +327,7 @@ func GetTokenByIds(id int, userId int) (*Token, error) {
|
||||
|
||||
func GetTokenById(id int) (*Token, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, errors.New("token id is empty")
|
||||
}
|
||||
token := Token{Id: id}
|
||||
var err error = nil
|
||||
@@ -352,7 +352,7 @@ func GetTokenByKey(key string, fromDB bool) (token *Token, err error) {
|
||||
// 冷缓存时用数据库快照初始化;已存在的哈希只刷新 TTL,
|
||||
// 避免快照覆盖 Redis 中已被原子预扣的余额。初始化失败不影响本次读取。
|
||||
if _, cacheErr := cacheInitToken(*token); cacheErr != nil {
|
||||
common.SysLog("failed to init token cache: " + cacheErr.Error())
|
||||
common.SysLog(common.LogText("failed to init token cache: %s", cacheErr.Error()))
|
||||
}
|
||||
}
|
||||
return token, nil
|
||||
@@ -368,7 +368,7 @@ func (token *Token) Insert() error {
|
||||
func (token *Token) Update() (err error) {
|
||||
// 写库前失效缓存并设置 fence,防止并发读者把过期快照重新写回缓存。
|
||||
if cacheErr := invalidateTokenCacheForMutation(token.Key); cacheErr != nil {
|
||||
common.SysLog("failed to invalidate token cache before update: " + cacheErr.Error())
|
||||
common.SysLog(common.LogText("failed to invalidate token cache before update: %s", cacheErr.Error()))
|
||||
}
|
||||
return DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota",
|
||||
"model_limits_enabled", "model_limits", "allow_ips", "group", "cross_group_retry", "astra_auto_switch", "astra_detection_enabled", "auto_groups", "routing_policy").Updates(token).Error
|
||||
@@ -376,7 +376,7 @@ func (token *Token) Update() (err error) {
|
||||
|
||||
func (token *Token) SelectUpdate() (err error) {
|
||||
if cacheErr := invalidateTokenCacheForMutation(token.Key); cacheErr != nil {
|
||||
common.SysLog("failed to invalidate token cache before status update: " + cacheErr.Error())
|
||||
common.SysLog(common.LogText("failed to invalidate token cache before status update: %s", cacheErr.Error()))
|
||||
}
|
||||
// This can update zero values
|
||||
return DB.Model(token).Select("accessed_time", "status").Updates(token).Error
|
||||
@@ -384,7 +384,7 @@ func (token *Token) SelectUpdate() (err error) {
|
||||
|
||||
func (token *Token) Delete() (err error) {
|
||||
if cacheErr := invalidateTokenCacheForMutation(token.Key); cacheErr != nil {
|
||||
common.SysLog("failed to invalidate token cache before delete: " + cacheErr.Error())
|
||||
common.SysLog(common.LogText("failed to invalidate token cache before delete: %s", cacheErr.Error()))
|
||||
}
|
||||
return DB.Delete(token).Error
|
||||
}
|
||||
@@ -422,7 +422,7 @@ func DisableModelLimits(tokenId int) error {
|
||||
func DeleteTokenById(id int, userId int) (err error) {
|
||||
// Why we need userId here? In case user want to delete other's token.
|
||||
if id == 0 || userId == 0 {
|
||||
return errors.New("id 或 userId 为空!")
|
||||
return common.NewMessage("Token ID or user ID is empty")
|
||||
}
|
||||
token := Token{Id: id, UserId: userId}
|
||||
err = DB.Where(token).First(&token).Error
|
||||
@@ -434,14 +434,14 @@ func DeleteTokenById(id int, userId int) (err error) {
|
||||
|
||||
func IncreaseTokenQuota(tokenId int, key string, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative")
|
||||
}
|
||||
if common.RedisEnabled {
|
||||
gopool.Go(func() {
|
||||
// 守卫式增量:哈希不存在时跳过,由下次读取从数据库水合,
|
||||
// 绝不创建只有配额字段的残缺哈希。
|
||||
if _, err := cacheApplyTokenQuotaDelta(tokenId, key, int64(quota)); err != nil {
|
||||
common.SysLog("failed to increase token quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to increase token quota: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -465,12 +465,12 @@ func increaseTokenQuota(id int, quota int) (err error) {
|
||||
|
||||
func DecreaseTokenQuota(id int, key string, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative")
|
||||
}
|
||||
if common.RedisEnabled {
|
||||
gopool.Go(func() {
|
||||
if _, err := cacheApplyTokenQuotaDelta(id, key, int64(-quota)); err != nil {
|
||||
common.SysLog("failed to decrease token quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to decrease token quota: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -502,7 +502,7 @@ func CountUserTokens(userId int) (int64, error) {
|
||||
// BatchDeleteTokens 删除指定用户的一组令牌,返回成功删除数量
|
||||
func BatchDeleteTokens(ids []int, userId int) (int, error) {
|
||||
if len(ids) == 0 {
|
||||
return 0, errors.New("ids 不能为空!")
|
||||
return 0, common.NewMessage("Token IDs cannot be empty")
|
||||
}
|
||||
|
||||
tx := DB.Begin()
|
||||
@@ -513,7 +513,7 @@ func BatchDeleteTokens(ids []int, userId int) (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
if err := invalidateTokensCache(tokens); err != nil {
|
||||
common.SysLog("failed to invalidate token cache before batch delete: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to invalidate token cache before batch delete: %s", err.Error()))
|
||||
}
|
||||
|
||||
if err := tx.Where("user_id = ? AND id IN (?)", userId, ids).Delete(&Token{}).Error; err != nil {
|
||||
@@ -544,7 +544,7 @@ func InvalidateUserTokensCache(userId int) error {
|
||||
return nil
|
||||
}
|
||||
if userId <= 0 {
|
||||
return errors.New("userId 无效")
|
||||
return errors.New("invalid user id")
|
||||
}
|
||||
var tokens []Token
|
||||
if err := DB.Unscoped().
|
||||
|
||||
Reference in new issue
Block a user