summaryrefslogtreecommitdiff
path: root/internal/gui/audio_paths.go
blob: 47371d49b4bd0a518164dc32779ec976f548fc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gui

import (
	"os"
	"path/filepath"
	"strings"

	"codeberg.org/snonux/totalrecall/internal/anki"
)

// resolveSingleAudioFile resolves the en-bg audio file path for a card directory.
// Delegates to the package-level helper used by CardService.
func (a *Application) resolveSingleAudioFile(wordDir string) string {
	return resolveSingleAudioFileInDir(wordDir)
}

// resolveBgBgAudioFiles resolves front and back audio file paths for a bg-bg
// card directory. Delegates to the package-level helper used by CardService.
func (a *Application) resolveBgBgAudioFiles(wordDir string) (string, string) {
	return resolveBgBgAudioFilesInDir(wordDir)
}

// resolveAudioFileFromMetadata reads audio_metadata.txt from wordDir and returns
// the path stored under key. Returns empty string when the key is absent or the
// file does not exist on disk.
func resolveAudioFileFromMetadata(wordDir, key string) string {
	metadata := readAudioMetadata(wordDir)
	value := strings.TrimSpace(metadata[key])
	if value == "" {
		return ""
	}

	if !filepath.IsAbs(value) {
		value = filepath.Join(wordDir, value)
	}

	if _, err := os.Stat(value); err == nil {
		return value
	}

	return ""
}

// readAudioMetadata parses audio_metadata.txt in wordDir into a key→value map.
// Returns an empty map when the file does not exist or cannot be read.
func readAudioMetadata(wordDir string) map[string]string {
	metadataFile := filepath.Join(wordDir, "audio_metadata.txt")
	data, err := os.ReadFile(metadataFile)
	if err != nil {
		return map[string]string{}
	}

	values := make(map[string]string)
	for _, line := range strings.Split(string(data), "\n") {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		key, value, found := strings.Cut(line, "=")
		if !found {
			continue
		}
		values[strings.TrimSpace(key)] = strings.TrimSpace(value)
	}

	return values
}

// anki.ResolveAudioFile is kept here to avoid importing anki in card_service.go
// directly. The package-level helpers reference it via this file.
var _ = anki.ResolveAudioFile