summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-18 19:45:35 +0300
committerPaul Buetow <paul@buetow.org>2025-07-18 19:45:35 +0300
commite1b3ba69499547424502cc7e4dacf1e10d5efde9 (patch)
tree17110ac06239ff24150cd87df7df1729f7a285ae /cmd
parentcd3b1e5b2fab8075303c064ba33996a0250cd6a6 (diff)
fix: ensure single directory per card with consistent ID
- Fixed issue where multiple directories were created for the same word - Added findCardDirectory to check for existing directories before creating new ones - Updated all file creation functions to reuse existing card directories - Applied fix to both GUI and CLI code - Added phonetic fetching to processing counter in generateMaterials - Now each card gets one directory with all its files 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/totalrecall/main.go119
1 files changed, 84 insertions, 35 deletions
diff --git a/cmd/totalrecall/main.go b/cmd/totalrecall/main.go
index 8258050..35b56c2 100644
--- a/cmd/totalrecall/main.go
+++ b/cmd/totalrecall/main.go
@@ -338,18 +338,22 @@ func generateAudioWithVoice(word, voice string) error {
// Generate audio file
ctx := context.Background()
- cardID := internal.GenerateCardID(word)
- // Create subdirectory for this word
- wordDir := filepath.Join(outputDir, cardID)
- if err := os.MkdirAll(wordDir, 0755); err != nil {
- return fmt.Errorf("failed to create word directory: %w", err)
- }
-
- // Save word metadata if not already present
- metadataFile := filepath.Join(wordDir, "word.txt")
- if _, err := os.Stat(metadataFile); os.IsNotExist(err) {
- os.WriteFile(metadataFile, []byte(word), 0644)
+ // Find existing card directory or create new one
+ wordDir := findCardDirectory(word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(word)
+ wordDir = filepath.Join(outputDir, cardID)
+ if err := os.MkdirAll(wordDir, 0755); err != nil {
+ return fmt.Errorf("failed to create word directory: %w", err)
+ }
+
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
+ if err := os.WriteFile(metadataFile, []byte(word), 0644); err != nil {
+ return fmt.Errorf("failed to save word metadata: %w", err)
+ }
}
// Add voice name to filename if generating multiple voices
@@ -423,17 +427,21 @@ func downloadImages(word string) error {
return fmt.Errorf("unknown image provider: %s", imageAPI)
}
- // Create subdirectory for this word
- cardID := internal.GenerateCardID(word)
- wordDir := filepath.Join(outputDir, cardID)
- if err := os.MkdirAll(wordDir, 0755); err != nil {
- return fmt.Errorf("failed to create word directory: %w", err)
- }
-
- // Save word metadata if not already present
- metadataFile := filepath.Join(wordDir, "word.txt")
- if _, err := os.Stat(metadataFile); os.IsNotExist(err) {
- os.WriteFile(metadataFile, []byte(word), 0644)
+ // Find existing card directory or create new one
+ wordDir := findCardDirectory(word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(word)
+ wordDir = filepath.Join(outputDir, cardID)
+ if err := os.MkdirAll(wordDir, 0755); err != nil {
+ return fmt.Errorf("failed to create word directory: %w", err)
+ }
+
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
+ if err := os.WriteFile(metadataFile, []byte(word), 0644); err != nil {
+ return fmt.Errorf("failed to save word metadata: %w", err)
+ }
}
// Create downloader
@@ -667,19 +675,23 @@ func translateWord(word string) (string, error) {
}
func saveTranslation(word, translation string) error {
- // Save translation to a text file
- cardID := internal.GenerateCardID(word)
- wordDir := filepath.Join(outputDir, cardID)
-
- // Ensure directory exists
- if err := os.MkdirAll(wordDir, 0755); err != nil {
- return fmt.Errorf("failed to create word directory: %w", err)
- }
-
- // Save word metadata if not already present
- metadataFile := filepath.Join(wordDir, "word.txt")
- if _, err := os.Stat(metadataFile); os.IsNotExist(err) {
- os.WriteFile(metadataFile, []byte(word), 0644)
+ // Find existing card directory or create new one
+ wordDir := findCardDirectory(word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(word)
+ wordDir = filepath.Join(outputDir, cardID)
+
+ // Ensure directory exists
+ if err := os.MkdirAll(wordDir, 0755); err != nil {
+ return fmt.Errorf("failed to create word directory: %w", err)
+ }
+
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
+ if err := os.WriteFile(metadataFile, []byte(word), 0644); err != nil {
+ return fmt.Errorf("failed to save word metadata: %w", err)
+ }
}
outputFile := filepath.Join(wordDir, "translation.txt")
@@ -696,6 +708,43 @@ func saveTranslation(word, translation string) error {
// Global map to store translations for Anki export
var wordTranslations = make(map[string]string)
+// findCardDirectory finds the directory for a given Bulgarian word
+func findCardDirectory(word string) string {
+ entries, err := os.ReadDir(outputDir)
+ if err != nil {
+ return ""
+ }
+
+ // Look through all directories to find one with matching word.txt
+ for _, entry := range entries {
+ if !entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
+ continue
+ }
+
+ dirPath := filepath.Join(outputDir, entry.Name())
+ wordFile := filepath.Join(dirPath, "word.txt")
+
+ // Read the word file to check if it matches
+ if data, err := os.ReadFile(wordFile); err == nil {
+ storedWord := strings.TrimSpace(string(data))
+ if storedWord == word {
+ return dirPath
+ }
+ } else {
+ // Try old format with underscore for backward compatibility
+ wordFile = filepath.Join(dirPath, "_word.txt")
+ if data, err := os.ReadFile(wordFile); err == nil {
+ storedWord := strings.TrimSpace(string(data))
+ if storedWord == word {
+ return dirPath
+ }
+ }
+ }
+ }
+
+ return ""
+}
+
func saveAudioAttribution(word, audioFile string, config *audio.Config) error {
// Create attribution text
attribution := fmt.Sprintf("Audio generated by OpenAI TTS\n\n")