Configuration hardening
This commit is contained in:
@@ -8,7 +8,14 @@ import (
|
||||
)
|
||||
|
||||
func GenerateStem(fileName, prefix string, used map[string]struct{}) (string, error) {
|
||||
maxCore := MaxStemLen - len(prefix)
|
||||
return GenerateStemWithMax(fileName, prefix, MaxStemLen, used)
|
||||
}
|
||||
|
||||
func GenerateStemWithMax(fileName, prefix string, maxStemLen int, used map[string]struct{}) (string, error) {
|
||||
if maxStemLen <= 0 {
|
||||
maxStemLen = MaxStemLen
|
||||
}
|
||||
maxCore := maxStemLen - len(prefix)
|
||||
if maxCore < 3 {
|
||||
return "", fmt.Errorf("prefix too long: %q", prefix)
|
||||
}
|
||||
@@ -19,11 +26,11 @@ func GenerateStem(fileName, prefix string, used map[string]struct{}) (string, er
|
||||
sum := sha1.Sum([]byte(fileName))
|
||||
core = fmt.Sprintf("%x", sum[:])[:maxCore]
|
||||
}
|
||||
stem := strings.TrimRight((prefix + core)[:Min(MaxStemLen, len(prefix)+len(core))], "_")
|
||||
stem := strings.TrimRight((prefix + core)[:Min(maxStemLen, len(prefix)+len(core))], "_")
|
||||
if stem == "" {
|
||||
return "", fmt.Errorf("could not derive music stem for %s", fileName)
|
||||
}
|
||||
return UniqueName(stem, used), nil
|
||||
return UniqueNameWithMax(stem, maxStemLen, used), nil
|
||||
}
|
||||
|
||||
func SlugWords(value string) []string {
|
||||
@@ -119,13 +126,20 @@ func AbbreviateWord(word string, maxLen int) string {
|
||||
}
|
||||
|
||||
func UniqueName(stem string, used map[string]struct{}) string {
|
||||
return UniqueNameWithMax(stem, MaxStemLen, used)
|
||||
}
|
||||
|
||||
func UniqueNameWithMax(stem string, maxStemLen int, used map[string]struct{}) string {
|
||||
if maxStemLen <= 0 {
|
||||
maxStemLen = MaxStemLen
|
||||
}
|
||||
if _, exists := used[stem]; !exists {
|
||||
used[stem] = struct{}{}
|
||||
return stem
|
||||
}
|
||||
for index := 1; ; index++ {
|
||||
suffix := fmt.Sprintf("_%d", index)
|
||||
prefixLen := Min(len(stem), Max(0, MaxStemLen-len(suffix)))
|
||||
prefixLen := Min(len(stem), Max(0, maxStemLen-len(suffix)))
|
||||
candidate := strings.TrimRight(stem[:prefixLen], "_") + suffix
|
||||
if _, exists := used[candidate]; exists {
|
||||
continue
|
||||
@@ -148,16 +162,27 @@ func ReserveName(stem string, used map[string]struct{}) error {
|
||||
}
|
||||
|
||||
func ValidateManualOutputFile(name string) error {
|
||||
return ValidateManualOutputFileWithRules(name, ".bmu", MaxStemLen)
|
||||
}
|
||||
|
||||
func ValidateManualOutputFileWithRules(name, extension string, maxStemLen int) error {
|
||||
name = strings.ToLower(strings.TrimSpace(name))
|
||||
if !strings.HasSuffix(name, ".bmu") {
|
||||
return fmt.Errorf("output file must end with .bmu")
|
||||
extension = strings.ToLower(strings.TrimSpace(extension))
|
||||
if extension == "" {
|
||||
extension = ".bmu"
|
||||
}
|
||||
stem := strings.TrimSuffix(name, ".bmu")
|
||||
if maxStemLen <= 0 {
|
||||
maxStemLen = MaxStemLen
|
||||
}
|
||||
if !strings.HasSuffix(name, extension) {
|
||||
return fmt.Errorf("output file must end with %s", extension)
|
||||
}
|
||||
stem := strings.TrimSuffix(name, extension)
|
||||
if stem == "" {
|
||||
return fmt.Errorf("output file stem is empty")
|
||||
}
|
||||
if len(stem) > MaxStemLen {
|
||||
return fmt.Errorf("output file stem %q exceeds %d characters", stem, MaxStemLen)
|
||||
if len(stem) > maxStemLen {
|
||||
return fmt.Errorf("output file stem %q exceeds %d characters", stem, maxStemLen)
|
||||
}
|
||||
for _, r := range stem {
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user