Prevent collisions between prepared audio filenames

This commit is contained in:
2026-11-30 04:06:30 +00:00
parent 4d81a37638
commit 1f5c0c435e
3 changed files with 67 additions and 7 deletions

View File

@@ -380,23 +380,37 @@ func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolve
func localAudioDestinations(audioPaths []string) map[string]string {
byBase := make(map[string][]string, len(audioPaths))
occupied := make(map[string]bool, len(audioPaths))
for _, audioPath := range audioPaths {
base := filepath.Base(audioPath)
byBase[base] = append(byBase[base], audioPath)
occupied[base] = true
}
destinations := make(map[string]string, len(audioPaths))
for base, sources := range byBase {
if len(sources) == 1 {
destinations[sources[0]] = base
ordered := append([]string(nil), audioPaths...)
sort.Strings(ordered)
nextSuffix := make(map[string]int)
for _, source := range ordered {
if _, assigned := destinations[source]; assigned {
continue
}
base := filepath.Base(source)
if len(byBase[base]) == 1 {
destinations[source] = base
continue
}
extension := filepath.Ext(base)
stem := strings.TrimSuffix(base, extension)
for _, source := range sources {
digest := sha256.Sum256([]byte(filepath.Clean(source)))
destinations[source] = fmt.Sprintf("%s-%s%s", stem, hex.EncodeToString(digest[:8]), extension)
digest := sha256.Sum256([]byte(filepath.Clean(source)))
generatedStem := stem + "-" + hex.EncodeToString(digest[:8])
candidate := generatedStem + extension
for occupied[candidate] {
nextSuffix[generatedStem+extension]++
candidate = fmt.Sprintf("%s-%d%s", generatedStem, nextSuffix[generatedStem+extension], extension)
}
destinations[source] = candidate
occupied[candidate] = true
}
return destinations
}