fix: 中文摄像头名直接用SHA256 hash避免数字提取碰撞

This commit is contained in:
tian 2026-07-24 15:38:34 +08:00
parent 20261dad96
commit c6591d1eae
2 changed files with 3 additions and 13 deletions

View File

@ -606,24 +606,14 @@ func cleanSourceNames(names []string) []string {
// deriveSafeInstanceName converts a user-facing video source name into a valid
// recognition unit instance name ([A-Za-z0-9_.-]+). If the source name is already
// valid, it's used as-is. Otherwise, a sanitised version is generated.
// valid, it's used as-is. Otherwise, a unique hash-based name is generated.
func deriveSafeInstanceName(srcName string) string {
// If already valid per validateConfigName rules, use as-is.
if safeConfigName.MatchString(srcName) {
return srcName
}
// Build a safe name from the source: keep only ASCII letters, digits, underscores.
var b strings.Builder
for _, r := range srcName {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' || r == '-' || r == '.' {
b.WriteRune(r)
}
}
derived := strings.Trim(b.String(), "-_.")
if derived != "" && safeConfigName.MatchString(derived) {
return derived
}
// Fallback: use a short hash suffix to guarantee uniqueness.
// For non-ASCII names, use a hash prefix to guarantee uniqueness
// (extracting digits/ASCII from Chinese names causes collisions e.g. "5跨主通道" and "5跨焙烧区" both → "5").
sum := sha256.Sum256([]byte(srcName))
return "src_" + strings.ToLower(hex.EncodeToString(sum[:4]))
}

Binary file not shown.