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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
package gui
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"codeberg.org/snonux/totalrecall/internal"
"codeberg.org/snonux/totalrecall/internal/anki"
"codeberg.org/snonux/totalrecall/internal/image"
"codeberg.org/snonux/totalrecall/internal/store"
)
// CardService manages card file discovery, directory creation, persistence,
// and state loading from the output directory. It is responsible for all
// non-UI file I/O related to cards, decoupled from UI event-wiring.
// Directory-scanning and creation are delegated to a store.CardStore so the
// underlying algorithm is shared with the processor package (DRY).
type CardService struct {
config *Config
cardStore *store.CardStore
}
// NewCardService constructs a CardService for the given configuration.
// It initialises an internal store.CardStore rooted at config.OutputDir.
func NewCardService(config *Config) *CardService {
return &CardService{
config: config,
cardStore: store.New(config.OutputDir),
}
}
// FindCardDirectory finds the directory for a given Bulgarian word.
// Delegates to the shared store.CardStore which also handles the legacy
// _word.txt fallback for backward compatibility.
func (cs *CardService) FindCardDirectory(word string) string {
return cs.cardStore.FindCardDirectory(word)
}
// EnsureWordDirectoryAndMetadata creates a new card directory and writes word
// metadata to word.txt inside it. Returns the directory path.
// Uses store.FindOrCreateCardDirectory so the creation logic is not duplicated.
func (cs *CardService) EnsureWordDirectoryAndMetadata(word string) (string, error) {
wordDir := cs.cardStore.FindOrCreateCardDirectory(word)
if wordDir == "" || wordDir == cs.config.OutputDir {
return "", fmt.Errorf("failed to create card directory for %q", word)
}
return wordDir, nil
}
// EnsureCardDirectory ensures a card directory exists for the given word and
// returns its path. Creates it with metadata if it does not exist yet.
func (cs *CardService) EnsureCardDirectory(word string) (string, error) {
wordDir := cs.FindCardDirectory(word)
if wordDir != "" {
return wordDir, nil
}
return cs.EnsureWordDirectoryAndMetadata(word)
}
// ScanExistingWords scans the output directory for existing card subdirectories
// and returns a sorted list of the Bulgarian words found. A directory counts
// only if it contains at least one of: an audio file, an image, or a
// translation file. Delegates iteration and word-file reading to the shared
// store.CardStore so that logic is not duplicated here.
func (cs *CardService) ScanExistingWords() []string {
return cs.cardStore.ScanWords(cs.dirHasContent)
}
// dirHasContent returns true if the card directory contains at least one audio
// file, image file, or translation file.
func (cs *CardService) dirHasContent(wordDir string) bool {
// Reuse the Application audio-path helpers via package-level functions to
// avoid duplicating the metadata-parsing logic.
if hasAnyAudioFileInDir(wordDir) {
return true
}
for _, pattern := range []string{"image.jpg", "image.png"} {
if _, err := os.Stat(filepath.Join(wordDir, pattern)); err == nil {
return true
}
}
if _, err := os.Stat(filepath.Join(wordDir, "translation.txt")); err == nil {
return true
}
return false
}
// SaveTranslation persists the translation for the given word to disk.
// It finds or creates the card directory as necessary.
func (cs *CardService) SaveTranslation(word, translation string) error {
if word == "" || translation == "" {
return nil
}
wordDir, err := cs.EnsureCardDirectory(word)
if err != nil {
return err
}
translationFile := filepath.Join(wordDir, "translation.txt")
content := fmt.Sprintf("%s = %s\n", word, translation)
if err := os.WriteFile(translationFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to save translation: %w", err)
}
return nil
}
// SavePhoneticInfo persists phonetic information for the given word to disk.
func (cs *CardService) SavePhoneticInfo(word, phoneticText string) error {
if word == "" || phoneticText == "" || phoneticText == "Failed to fetch phonetic information" {
return nil
}
wordDir, err := cs.EnsureCardDirectory(word)
if err != nil {
return err
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
if err := os.WriteFile(phoneticFile, []byte(phoneticText), 0644); err != nil {
return fmt.Errorf("failed to save phonetic info: %w", err)
}
return nil
}
// LoadPhoneticInfo reads phonetic information from disk for the given word.
// Returns empty string if not found.
func (cs *CardService) LoadPhoneticInfo(word string) string {
wordDir := cs.FindCardDirectory(word)
if wordDir == "" {
return ""
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
data, err := os.ReadFile(phoneticFile)
if err != nil {
return ""
}
return string(data)
}
// CardFiles holds the paths to all files loaded for a single card.
type CardFiles struct {
WordDir string
Translation string
AudioFile string
AudioBack string
ImageFile string
PhoneticInfo string
ImagePrompt string
CardType internal.CardType
}
// LoadCardFiles loads all available files for the given word from disk.
// Returns nil if no card directory exists for the word.
func (cs *CardService) LoadCardFiles(word string) *CardFiles {
wordDir := cs.FindCardDirectory(word)
if wordDir == "" {
fmt.Printf("No card directory found for word: %s\n", word)
return nil
}
fmt.Printf("Loading files from directory: %s\n", wordDir)
cf := &CardFiles{WordDir: wordDir}
cf.CardType = internal.LoadCardType(wordDir)
cs.loadTranslation(wordDir, cf)
cs.loadImagePrompt(wordDir, cf)
cs.loadPhoneticFile(wordDir, cf)
cs.loadAudioFiles(wordDir, cf)
cs.loadImageFile(wordDir, cf)
return cf
}
// loadTranslation reads translation.txt and stores the translation in cf.
func (cs *CardService) loadTranslation(wordDir string, cf *CardFiles) {
translationFile := filepath.Join(wordDir, "translation.txt")
data, err := os.ReadFile(translationFile)
if err != nil {
return
}
content := string(data)
parts := strings.Split(content, "=")
if len(parts) >= 2 {
cf.Translation = strings.TrimSpace(parts[1])
}
}
// loadImagePrompt reads image_prompt.txt and stores the prompt in cf.
func (cs *CardService) loadImagePrompt(wordDir string, cf *CardFiles) {
promptFile := filepath.Join(wordDir, "image_prompt.txt")
data, err := os.ReadFile(promptFile)
if err != nil {
fmt.Printf("No prompt file found at: %s\n", promptFile)
return
}
fmt.Printf("Loaded prompt from file: %s\n", promptFile)
cf.ImagePrompt = strings.TrimSpace(string(data))
}
// loadPhoneticFile reads phonetic.txt and stores the info in cf.
func (cs *CardService) loadPhoneticFile(wordDir string, cf *CardFiles) {
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
data, err := os.ReadFile(phoneticFile)
if err != nil {
fmt.Printf("No phonetic file found at: %s (error: %v)\n", phoneticFile, err)
return
}
fmt.Printf("Loaded phonetic info from file: %s\n", phoneticFile)
cf.PhoneticInfo = string(data)
}
// loadAudioFiles resolves front and/or back audio paths depending on card type.
func (cs *CardService) loadAudioFiles(wordDir string, cf *CardFiles) {
if cf.CardType.IsBgBg() {
cf.AudioFile, cf.AudioBack = resolveBgBgAudioFilesInDir(wordDir)
} else {
cf.AudioFile = resolveSingleAudioFileInDir(wordDir)
}
}
// loadImageFile finds the first matching image file in the card directory.
func (cs *CardService) loadImageFile(wordDir string, cf *CardFiles) {
for _, pattern := range []string{"image.jpg", "image.png"} {
imagePath := filepath.Join(wordDir, pattern)
if _, err := os.Stat(imagePath); err == nil {
cf.ImageFile = imagePath
break
}
}
if cf.ImageFile == "" {
return
}
// Try to load the image prompt from the attribution file as a fallback
// when the image provider is AI-based (OpenAI DALL-E or Nano Banana).
if cs.config.ImageProvider == image.ImageProviderOpenAI || cs.config.ImageProvider == image.ImageProviderNanoBanana {
cs.loadPromptFromAttribution(cf)
}
}
// loadPromptFromAttribution reads the image prompt from an _attribution.txt
// file adjacent to the image file, falling back to the already-set ImagePrompt.
func (cs *CardService) loadPromptFromAttribution(cf *CardFiles) {
if cf.ImageFile == "" {
return
}
baseImagePath := cf.ImageFile
attrPath := strings.TrimSuffix(baseImagePath, filepath.Ext(baseImagePath)) + "_attribution.txt"
data, err := os.ReadFile(attrPath)
if err != nil {
return
}
// The attribution format is: "Prompt used:\n<the prompt>"
content := string(data)
lines := strings.Split(content, "\n")
for i, line := range lines {
if strings.HasPrefix(line, "Prompt used:") && i+1 < len(lines) {
cf.ImagePrompt = strings.TrimSpace(lines[i+1])
break
}
}
}
// CheckMissingFiles inspects a card directory for any files that have appeared
// since the last load and returns a partial CardFiles with only the newly found
// fields populated. Callers should merge with existing state.
func (cs *CardService) CheckMissingFiles(word string, existingAudio, existingAudioBack, existingImage, existingTranslation, existingPrompt, existingPhonetic string, cardType string) *CardFiles {
wordDir := cs.FindCardDirectory(word)
if wordDir == "" {
return nil
}
result := &CardFiles{}
// Check for missing audio.
if existingAudio == "" {
if cardType == "bg-bg" {
front, _ := resolveBgBgAudioFilesInDir(wordDir)
result.AudioFile = front
} else {
result.AudioFile = resolveSingleAudioFileInDir(wordDir)
}
}
// Check for missing back audio (bg-bg cards only).
if existingAudioBack == "" {
_, back := resolveBgBgAudioFilesInDir(wordDir)
result.AudioBack = back
}
// Check for missing image.
if existingImage == "" {
for _, pattern := range []string{"image.jpg", "image.png"} {
imagePath := filepath.Join(wordDir, pattern)
if _, err := os.Stat(imagePath); err == nil {
result.ImageFile = imagePath
break
}
}
}
// Check for missing translation.
if existingTranslation == "" {
translationFile := filepath.Join(wordDir, "translation.txt")
if data, err := os.ReadFile(translationFile); err == nil {
parts := strings.Split(string(data), "=")
if len(parts) >= 2 {
result.Translation = strings.TrimSpace(parts[1])
}
}
}
// Check for missing image prompt.
if existingPrompt == "" {
promptFile := filepath.Join(wordDir, "image_prompt.txt")
if data, err := os.ReadFile(promptFile); err == nil {
result.ImagePrompt = strings.TrimSpace(string(data))
}
}
// Check for missing phonetic info.
if existingPhonetic == "" {
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
if data, err := os.ReadFile(phoneticFile); err == nil {
result.PhoneticInfo = string(data)
}
}
return result
}
// DeleteWord moves the card directory for a word to the trash bin directory.
// Returns the updated existingWords slice and updatedSavedCards slice.
func (cs *CardService) DeleteWord(word string, existingWords []string, savedCards []anki.Card) ([]string, []anki.Card, error) {
wordDir := cs.FindCardDirectory(word)
if wordDir == "" {
return existingWords, savedCards, fmt.Errorf("no card directory found for word %q", word)
}
trashDir := filepath.Join(cs.config.OutputDir, ".trashbin")
if err := os.MkdirAll(trashDir, 0755); err != nil {
return existingWords, savedCards, fmt.Errorf("failed to create trash directory: %w", err)
}
dirName := filepath.Base(wordDir)
trashWordDir := filepath.Join(trashDir, fmt.Sprintf("%s_%s", dirName, trashTimestamp()))
if err := os.Rename(wordDir, trashWordDir); err != nil {
return existingWords, savedCards, fmt.Errorf("failed to move files to trash: %w", err)
}
// Remove the word from the existingWords list.
newWords := make([]string, 0, len(existingWords))
for _, w := range existingWords {
if w != word {
newWords = append(newWords, w)
}
}
// Remove the word from savedCards.
newCards := make([]anki.Card, 0, len(savedCards))
for _, card := range savedCards {
if card.Bulgarian != word {
newCards = append(newCards, card)
}
}
return newWords, newCards, nil
}
// LoadImagePromptForWord reads the image_prompt.txt file for the given word.
// Returns empty string if not found.
func (cs *CardService) LoadImagePromptForWord(word string) string {
wordDir := cs.FindCardDirectory(word)
if wordDir == "" {
return ""
}
promptFile := filepath.Join(wordDir, "image_prompt.txt")
data, err := os.ReadFile(promptFile)
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// hasAnyAudioFileInDir is a package-level helper so CardService can check for
// audio files without holding a reference to Application.
func hasAnyAudioFileInDir(wordDir string) bool {
if resolveSingleAudioFileInDir(wordDir) != "" {
return true
}
front, back := resolveBgBgAudioFilesInDir(wordDir)
return front != "" || back != ""
}
// resolveSingleAudioFileInDir resolves the single en-bg audio file from a card dir.
func resolveSingleAudioFileInDir(wordDir string) string {
if audioFile := resolveAudioFileFromMetadata(wordDir, "audio_file"); audioFile != "" {
return audioFile
}
return anki.ResolveAudioFile(wordDir, "audio", "")
}
// resolveBgBgAudioFilesInDir resolves front+back audio files for a bg-bg card dir.
func resolveBgBgAudioFilesInDir(wordDir string) (string, string) {
front := resolveAudioFileFromMetadata(wordDir, "audio_file")
if front == "" {
front = anki.ResolveAudioFile(wordDir, "audio_front", "")
}
back := resolveAudioFileFromMetadata(wordDir, "audio_file_back")
if back == "" {
back = anki.ResolveAudioFile(wordDir, "audio_back", "")
}
return front, back
}
// trashTimestamp returns a formatted timestamp string for use in trash
// directory names, ensuring uniqueness across multiple deletions.
func trashTimestamp() string {
return time.Now().Format("20060102_150405")
}
|