last accessed at!!!
This commit is contained in:
@@ -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
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -82,7 +82,7 @@
|
||||
</details>
|
||||
|
||||
<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
|
||||
we will make this element visible with javascript
|
||||
|
||||
Reference in New Issue
Block a user