Prevent collisions between prepared audio filenames
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user