Move to flat files

This commit is contained in:
R. Alex Matevish 2025-05-27 20:42:15 -07:00
parent 9e35088168
commit 125c3947c3
No known key found for this signature in database
GPG Key ID: 8717A977B24993D8
3 changed files with 32 additions and 52 deletions

10
go.mod
View File

@ -1,13 +1,3 @@
module github.com/ramatevish/kindle-sender module github.com/ramatevish/kindle-sender
go 1.24.3 go 1.24.3
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.28 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.20.0 // indirect
gorm.io/driver/sqlite v1.5.7 // indirect
gorm.io/gorm v1.30.0 // indirect
)

14
go.sum
View File

@ -1,14 +0,0 @@
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=

60
main.go
View File

@ -5,6 +5,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -14,10 +15,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
) )
var ( var (
@ -28,14 +27,14 @@ var (
senderEmail = os.Getenv("SENDER_EMAIL") senderEmail = os.Getenv("SENDER_EMAIL")
recipient = os.Getenv("RECIPIENT_EMAIL") recipient = os.Getenv("RECIPIENT_EMAIL")
watchDir = os.Getenv("WATCH_DIR") watchDir = os.Getenv("WATCH_DIR")
db *gorm.DB sentDir string
sentFileLock sync.Mutex
) )
type SentFile struct { type SentFileEntry struct {
ID uint `gorm:"primaryKey"` Path string `json:"path"`
Path string SentAt time.Time `json:"sent_at"`
Hash string `gorm:"uniqueIndex"` Hash string `json:"hash"`
SentAt time.Time
} }
func isEbookFile(name string) bool { func isEbookFile(name string) bool {
@ -104,33 +103,38 @@ func fileHash(path string) (string, error) {
return hex.EncodeToString(h.Sum(nil)), nil return hex.EncodeToString(h.Sum(nil)), nil
} }
func sentFilePath(hash string) string {
return filepath.Join(sentDir, hash+".json")
}
func hasBeenSent(hash string) bool { func hasBeenSent(hash string) bool {
var file SentFile sentFileLock.Lock()
err := db.Where("hash = ?", hash).First(&file).Error defer sentFileLock.Unlock()
_, err := os.Stat(sentFilePath(hash))
return err == nil return err == nil
} }
func markAsSent(filePath, hash string) { func markAsSent(filePath, hash string) {
err := db.Create(&SentFile{ sentFileLock.Lock()
defer sentFileLock.Unlock()
entry := SentFileEntry{
Path: filePath, Path: filePath,
Hash: hash,
SentAt: time.Now().UTC(), SentAt: time.Now().UTC(),
}).Error Hash: hash,
if err != nil {
log.Printf("DB error marking file as sent: %v", err)
} }
f, err := os.Create(sentFilePath(hash))
if err != nil {
log.Printf("Failed to record sent file %s: %v", filePath, err)
return
}
defer f.Close()
json.NewEncoder(f).Encode(entry)
} }
func setupDatabase() error { func setupFileTracking() error {
dbPath := filepath.Join(watchDir, "sent_files.db") sentDir = filepath.Join(watchDir, ".sent")
database, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{ return os.MkdirAll(sentDir, 0755)
SkipDefaultTransaction: true,
})
if err != nil {
return err
}
db = database
return db.AutoMigrate(&SentFile{})
} }
func scanAndSendEbooks() { func scanAndSendEbooks() {
@ -170,8 +174,8 @@ func main() {
log.Fatal("WATCH_DIR environment variable is required") log.Fatal("WATCH_DIR environment variable is required")
} }
if err := setupDatabase(); err != nil { if err := setupFileTracking(); err != nil {
log.Fatalf("Failed to setup database: %v", err) log.Fatalf("Failed to setup tracking directory: %v", err)
} }
log.Printf("Scanning folder: %s\n", watchDir) log.Printf("Scanning folder: %s\n", watchDir)