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

@@ -0,0 +1,41 @@
package stage
import (
"crypto/sha256"
"encoding/hex"
"path/filepath"
"reflect"
"testing"
)
func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) {
env, m := setupPrepareEnv(t)
paths := sessionPathsForEnv(env, m.SessionID)
first := filepath.Join(t.TempDir(), "one.flac")
second := filepath.Join(t.TempDir(), "one.flac")
digest := sha256.Sum256([]byte(filepath.Clean(first)))
generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac"
third := filepath.Join(t.TempDir(), generated)
inputs := []string{first, second, third}
destinations := localAudioDestinations(inputs)
if reversed := localAudioDestinations([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
t.Fatal("destination ownership depends on input order")
}
if destinations[third] != generated {
t.Fatal("noncolliding original filename changed")
}
owners := make(map[string]bool)
for i, source := range inputs {
if owners[destinations[source]] {
t.Fatal("distinct recordings share a destination")
}
owners[destinations[source]] = true
writeFile(t, source, string(rune('a'+i)))
}
if err := materializeLocalAudioInputs(env, paths, inputs, func(string, string, string) {}); err != nil {
t.Fatal(err)
}
for i, source := range inputs {
mustReadFileEquals(t, filepath.Join(paths.AudioDir, destinations[source]), string(rune('a'+i)))
}
}

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
}