summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-18 19:31:44 +0300
committerPaul Buetow <paul@buetow.org>2025-07-18 19:31:44 +0300
commitcd3b1e5b2fab8075303c064ba33996a0250cd6a6 (patch)
treebf280f05decd692dd3f477815acdb10f7b781144 /cmd
parentaa84a890ba80ba70a6ac311786cb9d80ae3d9e42 (diff)
fix: use timestamp+hash naming for directories in CLI to match GUI
- Updated CLI code to use internal.GenerateCardID() for directory names - Changed file naming to match GUI: word.txt, translation.txt, audio.mp3, image.jpg/png - Created internal/utils.go with shared GenerateCardID and SanitizeFilename functions - Fixed all references to use the new naming system consistently - Ensures both CLI and GUI use the same directory and file naming convention 🤖 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.go58
1 files changed, 29 insertions, 29 deletions
diff --git a/cmd/totalrecall/main.go b/cmd/totalrecall/main.go
index 04174bc..8258050 100644
--- a/cmd/totalrecall/main.go
+++ b/cmd/totalrecall/main.go
@@ -338,20 +338,26 @@ func generateAudioWithVoice(word, voice string) error {
// Generate audio file
ctx := context.Background()
- filename := sanitizeFilename(word)
+ cardID := internal.GenerateCardID(word)
// Create subdirectory for this word
- wordDir := filepath.Join(outputDir, filename)
+ 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)
+ }
+
// Add voice name to filename if generating multiple voices
var outputFile string
if allVoices {
- outputFile = filepath.Join(wordDir, fmt.Sprintf("%s_%s.%s", filename, voice, audioFormat))
+ outputFile = filepath.Join(wordDir, fmt.Sprintf("audio_%s.%s", voice, audioFormat))
} else {
- outputFile = filepath.Join(wordDir, fmt.Sprintf("%s.%s", filename, audioFormat))
+ outputFile = filepath.Join(wordDir, fmt.Sprintf("audio.%s", audioFormat))
}
// Generate the audio
@@ -418,18 +424,24 @@ func downloadImages(word string) error {
}
// Create subdirectory for this word
- filename := sanitizeFilename(word)
- wordDir := filepath.Join(outputDir, filename)
+ 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)
+ }
+
// Create downloader
downloadOpts := &image.DownloadOptions{
OutputDir: wordDir,
OverwriteExisting: true, // Allow overwriting existing files
CreateDir: true,
- FileNamePattern: "{word}_{index}",
+ FileNamePattern: "image",
MaxSizeBytes: 5 * 1024 * 1024, // 5MB
}
@@ -446,24 +458,6 @@ func downloadImages(word string) error {
return nil
}
-func sanitizeFilename(s string) string {
- // Simple filename sanitization
- result := ""
- for _, r := range s {
- if isAlphaNumeric(r) || r == '-' || r == '_' {
- result += string(r)
- } else {
- result += "_"
- }
- }
- return result
-}
-
-func isAlphaNumeric(r rune) bool {
- return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
- (r >= '0' && r <= '9') || (r >= 'а' && r <= 'я') ||
- (r >= 'А' && r <= 'Я')
-}
func splitLines(s string) []string {
// Simple line splitter
@@ -533,7 +527,7 @@ func generateAnkiFile() error {
}
} else {
// Generate APKG
- outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.apkg", sanitizeFilename(deckName)))
+ outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.apkg", internal.SanitizeFilename(deckName)))
if err := gen.GenerateAPKG(outputPath, deckName); err != nil {
return fmt.Errorf("failed to generate APKG: %w", err)
}
@@ -674,15 +668,21 @@ func translateWord(word string) (string, error) {
func saveTranslation(word, translation string) error {
// Save translation to a text file
- filename := sanitizeFilename(word)
- wordDir := filepath.Join(outputDir, filename)
+ 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)
}
- outputFile := filepath.Join(wordDir, fmt.Sprintf("%s_translation.txt", filename))
+ // 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)
+ }
+
+ outputFile := filepath.Join(wordDir, "translation.txt")
content := fmt.Sprintf("%s = %s\n", word, translation)