Add shared audio source discovery
This commit is contained in:
128
internal/audio/discovery.go
Normal file
128
internal/audio/discovery.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
)
|
||||
|
||||
// discoveryExtensions identifies candidates for automatic scans, not supported codecs.
|
||||
var discoveryExtensions = map[string]struct{}{
|
||||
".aac": {}, ".aif": {}, ".aifc": {}, ".aiff": {}, ".alac": {}, ".amr": {}, ".ape": {}, ".au": {}, ".caf": {}, ".flac": {},
|
||||
".m4a": {}, ".m4b": {}, ".mka": {}, ".mp2": {}, ".mp3": {}, ".mp4": {}, ".oga": {}, ".ogg": {}, ".opus": {}, ".snd": {},
|
||||
".wav": {}, ".wave": {}, ".weba": {}, ".webm": {}, ".wma": {}, ".wv": {},
|
||||
}
|
||||
|
||||
// IsCandidate reports whether a filename is eligible for automatic discovery.
|
||||
func IsCandidate(name string) bool {
|
||||
_, ok := discoveryExtensions[strings.ToLower(path.Ext(name))]
|
||||
return ok
|
||||
}
|
||||
|
||||
func resolveSourcePath(base, source string) (string, error) {
|
||||
source = strings.TrimSpace(source)
|
||||
if source == "" {
|
||||
return "", fmt.Errorf("path is required")
|
||||
}
|
||||
if filepath.IsAbs(source) {
|
||||
return filepath.Clean(source), nil
|
||||
}
|
||||
return filepath.Clean(filepath.Join(base, source)), nil
|
||||
}
|
||||
|
||||
// RequireRegularFile checks an audio source using ordinary stat semantics.
|
||||
func RequireRegularFile(name string) error {
|
||||
info, err := os.Stat(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat audio file %q: %w", name, err)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("audio file %q is not a regular file", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DiscoverLocal selects explicit files or scans a directory without writing workspace state.
|
||||
func DiscoverLocal(base string, files []string, dir string) ([]string, error) {
|
||||
var result []string
|
||||
if len(files) != 0 {
|
||||
seen := make(map[string]bool, len(files))
|
||||
for _, file := range files {
|
||||
resolved, err := resolveSourcePath(base, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if seen[resolved] {
|
||||
return nil, fmt.Errorf("duplicate audio source %q", resolved)
|
||||
}
|
||||
if err := RequireRegularFile(resolved); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen[resolved] = true
|
||||
result = append(result, resolved)
|
||||
}
|
||||
} else {
|
||||
resolved, err := resolveSourcePath(base, dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(resolved)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read audio directory %q: %w", resolved, err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !IsCandidate(entry.Name()) {
|
||||
continue
|
||||
}
|
||||
file := filepath.Join(resolved, entry.Name())
|
||||
if err := RequireRegularFile(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, file)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, fmt.Errorf("no audio candidates found in directory %q", resolved)
|
||||
}
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ObjectLister is the read-only storage boundary required for audio selection.
|
||||
type ObjectLister interface {
|
||||
List(context.Context, string) ([]storage.ObjectInfo, error)
|
||||
}
|
||||
|
||||
// DiscoverS3 selects audio candidates under an explicit bucket-relative prefix.
|
||||
func DiscoverS3(ctx context.Context, store ObjectLister, prefix string) ([]storage.ObjectInfo, error) {
|
||||
objects, err := store.List(ctx, prefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list s3 audio objects under %q: %w", prefix, err)
|
||||
}
|
||||
result := make([]storage.ObjectInfo, 0, len(objects))
|
||||
seen := map[string]string{}
|
||||
for _, obj := range objects {
|
||||
key := strings.TrimSpace(obj.Key)
|
||||
if key == "" || strings.HasSuffix(key, "/") || !IsCandidate(key) {
|
||||
continue
|
||||
}
|
||||
base := path.Base(key)
|
||||
if previous, ok := seen[base]; ok && previous != key {
|
||||
return nil, fmt.Errorf("duplicate s3 audio basename %q from %q and %q", base, previous, key)
|
||||
}
|
||||
seen[base] = key
|
||||
obj.Key = key
|
||||
result = append(result, obj)
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i].Key < result[j].Key })
|
||||
if len(result) == 0 {
|
||||
return nil, fmt.Errorf("no audio candidates found under s3 audio prefix %q", prefix)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
106
internal/audio/discovery_test.go
Normal file
106
internal/audio/discovery_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
)
|
||||
|
||||
type discoveryLister struct {
|
||||
objects []storage.ObjectInfo
|
||||
prefix string
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *discoveryLister) List(_ context.Context, prefix string) ([]storage.ObjectInfo, error) {
|
||||
s.prefix = prefix
|
||||
return s.objects, s.err
|
||||
}
|
||||
|
||||
func TestDiscoverLocalSelection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, name := range []string{"one.MP3", "two.flac", "note.txt", "unfamiliar.xyz", "extensionless"} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte("sample"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(root, "folder.wav"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := DiscoverLocal(root, nil, ".")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{filepath.Join(root, "one.MP3"), filepath.Join(root, "two.flac")}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
got, err = DiscoverLocal(root, []string{" extensionless ", "./unfamiliar.xyz"}, "missing")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want = []string{filepath.Join(root, "extensionless"), filepath.Join(root, "unfamiliar.xyz")}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
for _, files := range [][]string{{"one.MP3", "./one.MP3"}, {"missing"}, {"folder.wav"}, {" "}} {
|
||||
if _, err := DiscoverLocal(root, files, ""); err == nil {
|
||||
t.Errorf("expected error for %v", files)
|
||||
}
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "folder.wav"); err == nil {
|
||||
t.Fatal("expected empty directory error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverLocalRequiresRegularCandidates(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Symlink("missing", filepath.Join(root, "broken.wav")); err != nil {
|
||||
t.Skip(err)
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "."); err == nil {
|
||||
t.Fatal("expected missing target error")
|
||||
}
|
||||
if err := os.Remove(filepath.Join(root, "broken.wav")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(root, "target"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("target", filepath.Join(root, "link.wav")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "."); err == nil {
|
||||
t.Fatal("expected non-regular target error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverS3Selection(t *testing.T) {
|
||||
store := &discoveryLister{objects: []storage.ObjectInfo{{Key: " prefix/z.MP3 ", Size: 42, ETag: "etag"}, {Key: "prefix/dir/"}, {Key: " "}, {Key: "prefix/notes.txt"}, {Key: "prefix/a.wav"}}}
|
||||
got, err := DiscoverS3(context.Background(), store, "prefix/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.prefix != "prefix/" || len(got) != 2 || got[0].Key != "prefix/a.wav" || got[1].Key != "prefix/z.MP3" || got[1].Size != 42 || got[1].ETag != "etag" {
|
||||
t.Fatalf("unexpected selection: %#v", got)
|
||||
}
|
||||
store.objects = []storage.ObjectInfo{{Key: "a/same.flac"}, {Key: "b/same.flac"}}
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); err == nil || !strings.Contains(err.Error(), "a/same.flac") || !strings.Contains(err.Error(), "b/same.flac") {
|
||||
t.Fatalf("duplicate error: %v", err)
|
||||
}
|
||||
store.objects = nil
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); err == nil {
|
||||
t.Fatal("expected empty selection error")
|
||||
}
|
||||
sentinel := errors.New("list failed")
|
||||
store.err = sentinel
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); !errors.Is(err, sentinel) {
|
||||
t.Fatalf("listing error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user