150 lines
4.5 KiB
Go
150 lines
4.5 KiB
Go
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
|
|
}
|
|
|
|
// Source associates a selected local path with its prepared basename.
|
|
type Source struct {
|
|
Path string
|
|
PreparedName string
|
|
}
|
|
|
|
// DiscoverLocal selects explicit files or scans a directory without writing workspace state.
|
|
func DiscoverLocal(base string, files []string, dir string) ([]Source, 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)
|
|
names := PreparedNames(result)
|
|
sources := make([]Source, 0, len(result))
|
|
for _, source := range result {
|
|
sources = append(sources, Source{Path: source, PreparedName: names[source]})
|
|
}
|
|
if err := ValidateSpeakerIDs(sources); err != nil {
|
|
return nil, err
|
|
}
|
|
return sources, 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)
|
|
}
|
|
sources := make([]Source, 0, len(result))
|
|
for _, obj := range result {
|
|
sources = append(sources, Source{Path: obj.Key, PreparedName: path.Base(obj.Key)})
|
|
}
|
|
if err := ValidateSpeakerIDs(sources); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|