Move to flat files
This commit is contained in:
parent
9e35088168
commit
125c3947c3
10
go.mod
10
go.mod
@ -1,13 +1,3 @@
|
||||
module github.com/ramatevish/kindle-sender
|
||||
|
||||
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
14
go.sum
@ -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
60
main.go
@ -5,6 +5,7 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@ -14,10 +15,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -28,14 +27,14 @@ var (
|
||||
senderEmail = os.Getenv("SENDER_EMAIL")
|
||||
recipient = os.Getenv("RECIPIENT_EMAIL")
|
||||
watchDir = os.Getenv("WATCH_DIR")
|
||||
db *gorm.DB
|
||||
sentDir string
|
||||
sentFileLock sync.Mutex
|
||||
)
|
||||
|
||||
type SentFile struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Path string
|
||||
Hash string `gorm:"uniqueIndex"`
|
||||
SentAt time.Time
|
||||
type SentFileEntry struct {
|
||||
Path string `json:"path"`
|
||||
SentAt time.Time `json:"sent_at"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func isEbookFile(name string) bool {
|
||||
@ -104,33 +103,38 @@ func fileHash(path string) (string, error) {
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func sentFilePath(hash string) string {
|
||||
return filepath.Join(sentDir, hash+".json")
|
||||
}
|
||||
|
||||
func hasBeenSent(hash string) bool {
|
||||
var file SentFile
|
||||
err := db.Where("hash = ?", hash).First(&file).Error
|
||||
sentFileLock.Lock()
|
||||
defer sentFileLock.Unlock()
|
||||
_, err := os.Stat(sentFilePath(hash))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func markAsSent(filePath, hash string) {
|
||||
err := db.Create(&SentFile{
|
||||
sentFileLock.Lock()
|
||||
defer sentFileLock.Unlock()
|
||||
|
||||
entry := SentFileEntry{
|
||||
Path: filePath,
|
||||
Hash: hash,
|
||||
SentAt: time.Now().UTC(),
|
||||
}).Error
|
||||
if err != nil {
|
||||
log.Printf("DB error marking file as sent: %v", err)
|
||||
Hash: hash,
|
||||
}
|
||||
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 {
|
||||
dbPath := filepath.Join(watchDir, "sent_files.db")
|
||||
database, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
||||
SkipDefaultTransaction: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db = database
|
||||
return db.AutoMigrate(&SentFile{})
|
||||
func setupFileTracking() error {
|
||||
sentDir = filepath.Join(watchDir, ".sent")
|
||||
return os.MkdirAll(sentDir, 0755)
|
||||
}
|
||||
|
||||
func scanAndSendEbooks() {
|
||||
@ -170,8 +174,8 @@ func main() {
|
||||
log.Fatal("WATCH_DIR environment variable is required")
|
||||
}
|
||||
|
||||
if err := setupDatabase(); err != nil {
|
||||
log.Fatalf("Failed to setup database: %v", err)
|
||||
if err := setupFileTracking(); err != nil {
|
||||
log.Fatalf("Failed to setup tracking directory: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Scanning folder: %s\n", watchDir)
|
||||
|
Loading…
Reference in New Issue
Block a user