From e1b3ba69499547424502cc7e4dacf1e10d5efde9 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 18 Jul 2025 19:45:35 +0300 Subject: fix: ensure single directory per card with consistent ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- internal/gui/app.go | 88 +++++++++++++++++++++++++++++++++++++---------- internal/gui/generator.go | 48 +++++++++++++++----------- 2 files changed, 97 insertions(+), 39 deletions(-) (limited to 'internal/gui') diff --git a/internal/gui/app.go b/internal/gui/app.go index c4b609e..28abb7b 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -507,9 +507,17 @@ func (a *Application) generateMaterials(word string) { // Save translation to disk regardless if translation != "" { - cardID := internal.GenerateCardID(word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Find existing card directory first + wordDir := a.findCardDirectory(word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Save word metadata + metadataFile := filepath.Join(wordDir, "word.txt") + os.WriteFile(metadataFile, []byte(word), 0644) + } translationFile := filepath.Join(wordDir, "translation.txt") content := fmt.Sprintf("%s = %s\n", word, translation) os.WriteFile(translationFile, []byte(content), 0644) @@ -580,6 +588,36 @@ func (a *Application) generateMaterials(word string) { a.mu.Unlock() } + // Fetch phonetic information in a separate goroutine + go func() { + fyne.Do(func() { + a.incrementProcessing() // Phonetic processing starts + }) + + phoneticInfo, err := a.getPhoneticInfo(word) + if err != nil { + // Log error but don't fail - phonetic info is optional + fmt.Printf("Warning: Failed to get phonetic info: %v\n", err) + phoneticInfo = "Failed to fetch phonetic information" + } + + // Update UI with phonetic info if this is still the current word + a.mu.Lock() + if a.currentWord == word { + fyne.Do(func() { + a.phoneticDisplay.SetText(phoneticInfo) + }) + } + a.mu.Unlock() + + // Save phonetic info to disk + if phoneticInfo != "" && phoneticInfo != "Failed to fetch phonetic information" { + a.savePhoneticInfoForWord(word, phoneticInfo) + } + + a.decrementProcessing() // Phonetic processing ends + }() + // Enable action buttons fyne.Do(func() { a.hideProgress() @@ -1114,12 +1152,15 @@ func (a *Application) processWordJob(job *WordJob) { // Save translation to disk immediately for this specific word if translation != "" { - cardID := internal.GenerateCardID(job.Word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - os.MkdirAll(wordDir, 0755) // Ensure directory exists - // Save word metadata if not already present - metadataFile := filepath.Join(wordDir, "word.txt") - if _, err := os.Stat(metadataFile); os.IsNotExist(err) { + // Find existing card directory first + wordDir := a.findCardDirectory(job.Word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(job.Word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Save word metadata + metadataFile := filepath.Join(wordDir, "word.txt") os.WriteFile(metadataFile, []byte(job.Word), 0644) } translationFile := filepath.Join(wordDir, "translation.txt") @@ -1155,12 +1196,15 @@ func (a *Application) processWordJob(job *WordJob) { // Save phonetic info to disk immediately for this specific word if phoneticInfo != "" && phoneticInfo != "Failed to fetch phonetic information" { - cardID := internal.GenerateCardID(job.Word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - os.MkdirAll(wordDir, 0755) // Ensure directory exists - // Save word metadata if not already present - metadataFile := filepath.Join(wordDir, "word.txt") - if _, err := os.Stat(metadataFile); os.IsNotExist(err) { + // Find existing card directory first + wordDir := a.findCardDirectory(job.Word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(job.Word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Save word metadata + metadataFile := filepath.Join(wordDir, "word.txt") os.WriteFile(metadataFile, []byte(job.Word), 0644) } phoneticFile := filepath.Join(wordDir, "phonetic.txt") @@ -1585,9 +1629,17 @@ func (a *Application) savePhoneticInfoForWord(word, phoneticText string) { if word != "" && phoneticText != "" && phoneticText != "Failed to fetch phonetic information" && phoneticText != "Phonetic information will appear here..." { - cardID := internal.GenerateCardID(word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Find existing card directory first + wordDir := a.findCardDirectory(word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + os.MkdirAll(wordDir, 0755) // Ensure directory exists + // Save word metadata + metadataFile := filepath.Join(wordDir, "word.txt") + os.WriteFile(metadataFile, []byte(word), 0644) + } phoneticFile := filepath.Join(wordDir, "phonetic.txt") os.WriteFile(phoneticFile, []byte(phoneticText), 0644) } diff --git a/internal/gui/generator.go b/internal/gui/generator.go index f3b9563..ca9d854 100644 --- a/internal/gui/generator.go +++ b/internal/gui/generator.go @@ -100,17 +100,21 @@ func (a *Application) generateAudio(word string) (string, error) { return "", err } - // Create subdirectory for this word using card ID - cardID := internal.GenerateCardID(word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - if err := os.MkdirAll(wordDir, 0755); err != nil { - return "", fmt.Errorf("failed to create word directory: %w", err) - } - - // Save the original Bulgarian word in a metadata file - 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) + // Find existing card directory or create new one + wordDir := a.findCardDirectory(word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + if err := os.MkdirAll(wordDir, 0755); err != nil { + return "", fmt.Errorf("failed to create word directory: %w", err) + } + + // Save the original Bulgarian word in a metadata file + 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) + } } // Generate filename in subdirectory @@ -163,16 +167,18 @@ func (a *Application) generateImagesWithPrompt(word string, customPrompt string, return "", fmt.Errorf("unknown image provider: %s", a.config.ImageProvider) } - // Create subdirectory for this word using card ID - cardID := internal.GenerateCardID(word) - wordDir := filepath.Join(a.config.OutputDir, cardID) - if err := os.MkdirAll(wordDir, 0755); err != nil { - return "", fmt.Errorf("failed to create word directory: %w", err) - } - - // Save the original Bulgarian word in a metadata file if not already present - metadataFile := filepath.Join(wordDir, "word.txt") - if _, err := os.Stat(metadataFile); os.IsNotExist(err) { + // Find existing card directory or create new one + wordDir := a.findCardDirectory(word) + if wordDir == "" { + // No existing directory, create new one with card ID + cardID := internal.GenerateCardID(word) + wordDir = filepath.Join(a.config.OutputDir, cardID) + if err := os.MkdirAll(wordDir, 0755); err != nil { + return "", fmt.Errorf("failed to create word directory: %w", err) + } + + // Save the original Bulgarian word in a metadata file + 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) } -- cgit v1.2.3