From 507ff7359c6c850259e3f68225160cec477be72d Mon Sep 17 00:00:00 2001 From: whoswhip Date: Sat, 14 Nov 2026 14:25:13 -0500 Subject: [PATCH] last accessed at!!! --- access.go | 76 +++++++++++++++++++++++++++++++++++++++++++ handlers.go | 3 +- main.go | 10 +++++- readme.md | 5 ++- templates/upload.html | 2 +- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 access.go diff --git a/access.go b/access.go new file mode 100644 index 0000000..6fa6e75 --- /dev/null +++ b/access.go @@ -0,0 +1,76 @@ +package main + +import ( + "context" + "log" + "sync" + "time" + + "gorm.io/gorm" +) + +type AccessTracker struct { + mu sync.Mutex + pending map[string]int64 +} + +func (tracker *AccessTracker) Record(id string) { + tracker.mu.Lock() + defer tracker.mu.Unlock() + tracker.pending[id] = time.Now().UTC().Unix() +} + +func (tracker *AccessTracker) Flush(ctx context.Context, db *gorm.DB) error { + tracker.mu.Lock() + batch := tracker.pending + tracker.pending = make(map[string]int64) + tracker.mu.Unlock() + + if len(batch) == 0 { + return nil + } + + err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + for id, timestamp := range batch { + err := tx.Model(&Upload{}). + Where("id = ? AND last_accessed_at < ?", id, timestamp). + UpdateColumn("last_accessed_at", timestamp).Error + if err != nil { + return err + } + } + return nil + }) + + if err != nil { + tracker.mu.Lock() + for id, timestamp := range batch { + if timestamp > tracker.pending[id] { + tracker.pending[id] = timestamp + } + } + tracker.mu.Unlock() + return err + } + + log.Printf("updated access timestamps for %d uploads", len(batch)) + + return nil +} + +func (tracker *AccessTracker) Run(ctx context.Context, db *gorm.DB) { + ticker := time.NewTicker(time.Minute) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + err := tracker.Flush(ctx, db) + if err != nil { + log.Println("flush pending access:", err) + } + case <-ctx.Done(): + return + } + } +} diff --git a/handlers.go b/handlers.go index bc8eb33..4992098 100644 --- a/handlers.go +++ b/handlers.go @@ -284,7 +284,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl } } -func contentHandler(db *gorm.DB, cfg Config) http.HandlerFunc { +func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut @@ -323,6 +323,7 @@ func contentHandler(db *gorm.DB, cfg Config) http.HandlerFunc { } defer file.Close() + tracker.Record(id) ttl := 600 // 10 minutes if upload.ExpiresAt != nil { diff --git a/main.go b/main.go index be6d6db..d70a6bb 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "html/template" "log" @@ -78,6 +79,13 @@ func main() { } } + tracker := &AccessTracker{ + pending: make(map[string]int64), + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go tracker.Run(ctx, db) + mux := http.NewServeMux() fs := http.FileServer(http.Dir("static")) mux.Handle("GET /css/", fs) @@ -110,7 +118,7 @@ func main() { "Host": config.Host, }) }) - mux.HandleFunc("GET /{id}", contentHandler(db, config)) + mux.HandleFunc("GET /{id}", contentHandler(db, config, tracker)) mux.HandleFunc("POST /upload", uploadHandler(db, config, pages)) mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages)) mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) diff --git a/readme.md b/readme.md index c7e34e2..8687ae6 100644 --- a/readme.md +++ b/readme.md @@ -18,14 +18,13 @@ goals: - [x] upload endpoint - [x] upload content endpoint - - [ ] update last accessed at + - [x] update last accessed at - use a memory cache and batch commit to database to keep performance - [x] add cache control header - [ ] size based expiry - exclusions if below a certain size (like 5mb) - [x] delete content endpoints -- [ ] paste page -- [ ] return a page for nojs clients, such as on delete or upload +- [x] return a page for nojs clients, such as on delete or upload - [ ] max total storage usage - [ ] normalize mime types (like html) - [ ] filter mime types config diff --git a/templates/upload.html b/templates/upload.html index 40460e4..3154973 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -82,7 +82,7 @@