diff options
| author | Paul Buetow <paul@buetow.org> | 2025-07-18 19:31:44 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-07-18 19:31:44 +0300 |
| commit | cd3b1e5b2fab8075303c064ba33996a0250cd6a6 (patch) | |
| tree | bf280f05decd692dd3f477815acdb10f7b781144 /internal/utils.go | |
| parent | aa84a890ba80ba70a6ac311786cb9d80ae3d9e42 (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 'internal/utils.go')
| -rw-r--r-- | internal/utils.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/utils.go b/internal/utils.go new file mode 100644 index 0000000..779b95a --- /dev/null +++ b/internal/utils.go @@ -0,0 +1,43 @@ +package internal + +import ( + "crypto/md5" + "encoding/hex" + "fmt" + "time" +) + +// GenerateCardID creates a unique ID for a card based on timestamp and Bulgarian word +// Format: epochMillis_md5(word)[:8] +func GenerateCardID(bulgarianWord string) string { + // Get current timestamp in milliseconds + now := time.Now() + epochMillis := now.UnixNano() / 1000000 + + // Calculate MD5 hash of the word + hash := md5.Sum([]byte(bulgarianWord)) + hashStr := hex.EncodeToString(hash[:])[:8] // Use first 8 chars of MD5 + + // Combine timestamp and hash + return fmt.Sprintf("%d_%s", epochMillis, hashStr) +} + +// SanitizeFilename creates a safe filename from a string +func SanitizeFilename(s string) string { + result := "" + for _, r := range s { + if isAlphaNumeric(r) || r == '-' || r == '_' { + result += string(r) + } else { + result += "_" + } + } + return result +} + +// isAlphaNumeric checks if a rune is alphanumeric +func isAlphaNumeric(r rune) bool { + return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || + (r >= '0' && r <= '9') || (r >= 'а' && r <= 'я') || + (r >= 'А' && r <= 'Я') +}
\ No newline at end of file |
