last accessed at!!!

This commit is contained in:
2026-11-14 14:25:13 -05:00
parent 2e63dcbf31
commit 507ff7359c
5 changed files with 90 additions and 6 deletions
+76
View File
@@ -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
}
}
}
+2 -1
View File
@@ -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) { return func(w http.ResponseWriter, r *http.Request) {
id, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut 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() defer file.Close()
tracker.Record(id)
ttl := 600 // 10 minutes ttl := 600 // 10 minutes
if upload.ExpiresAt != nil { if upload.ExpiresAt != nil {
+9 -1
View File
@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"errors" "errors"
"html/template" "html/template"
"log" "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() mux := http.NewServeMux()
fs := http.FileServer(http.Dir("static")) fs := http.FileServer(http.Dir("static"))
mux.Handle("GET /css/", fs) mux.Handle("GET /css/", fs)
@@ -110,7 +118,7 @@ func main() {
"Host": config.Host, "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("POST /upload", uploadHandler(db, config, pages))
mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages)) mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages))
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
+2 -3
View File
@@ -18,14 +18,13 @@ goals:
- [x] upload endpoint - [x] upload endpoint
- [x] upload content 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 - use a memory cache and batch commit to database to keep performance
- [x] add cache control header - [x] add cache control header
- [ ] size based expiry - [ ] size based expiry
- exclusions if below a certain size (like 5mb) - exclusions if below a certain size (like 5mb)
- [x] delete content endpoints - [x] delete content endpoints
- [ ] paste page - [x] return a page for nojs clients, such as on delete or upload
- [ ] return a page for nojs clients, such as on delete or upload
- [ ] max total storage usage - [ ] max total storage usage
- [ ] normalize mime types (like html) - [ ] normalize mime types (like html)
- [ ] filter mime types config - [ ] filter mime types config
+1 -1
View File
@@ -82,7 +82,7 @@
</details> </details>
<div class="upload-links"> <div class="upload-links">
<a href="/{{.ID}}">view raw upload</a> <a href="/{{.ID}}{{.FileExtension}}">view raw upload</a>
<!-- <!--
copying is only available with javascript copying is only available with javascript
we will make this element visible with javascript we will make this element visible with javascript