feat: recursive WAV browsing in subdirectories

Handle ListWavs now uses filepath.WalkDir to find .wav files
in all subdirectories. Returns relative paths (e.g. Kicks/kick.wav).
Serves them correctly via /wavs/ prefix.

Fixed: WalkDir swallows root errors differently than ReadDir —
now detects and reports non-existent root directory errors.

Added test for recursive subdirectory traversal.
This commit is contained in:
2026-06-22 01:05:44 -07:00
parent 371feaf0cb
commit fe3e92ce33
2 changed files with 58 additions and 8 deletions
+20 -8
View File
@@ -273,18 +273,30 @@ func handleListWavs(w http.ResponseWriter, r *http.Request) {
dir = abs
}
entries, err := os.ReadDir(dir)
files := []string{}
var walkErr error
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
if path == dir {
walkErr = err
return filepath.SkipAll
}
return nil
}
if !d.IsDir() && strings.HasSuffix(strings.ToLower(d.Name()), ".wav") {
rel, _ := filepath.Rel(dir, path)
files = append(files, rel)
}
return nil
})
if walkErr != nil {
writeJSON(w, map[string]any{"error": walkErr.Error()})
return
}
if err != nil {
writeJSON(w, map[string]any{"error": err.Error()})
return
}
files := []string{}
for _, e := range entries {
if !e.IsDir() && strings.HasSuffix(strings.ToLower(e.Name()), ".wav") {
files = append(files, e.Name())
}
}
writeJSON(w, files)
}