tiny 88x31 lexicon for atproto

test new cleanup stuff

+59 -1
+6 -1
blobs/blobs.go
··· 12 12 "net/http" 13 13 "os" 14 14 "strings" 15 + "time" 15 16 ) 16 17 17 18 func AddImageToCache(did string, cid string, ctx context.Context) (string, error) { ··· 41 42 log.Println("invalid button!") 42 43 os.Remove(imgPath) 43 44 } 45 + now := time.Now() 46 + err = os.Chtimes(imgPath, now, now) 47 + if err != nil { 48 + log.Println(err) 49 + } 44 50 45 51 return imgPath, nil 46 - 47 52 } 48 53 49 54 func SyncGetBlob(did string, cid string, ctx context.Context) ([]byte, error) {
+48
blobs/cleanup.go
··· 1 + package blobs 2 + 3 + import ( 4 + "context" 5 + "log" 6 + "os" 7 + "path/filepath" 8 + "time" 9 + ) 10 + 11 + func Cleanup(ctx context.Context) { 12 + ticker := time.NewTicker(12 * time.Second) 13 + 14 + for { 15 + select { 16 + case <-ctx.Done(): 17 + return 18 + case <-ticker.C: 19 + err := clean() 20 + if err != nil { 21 + log.Println(err) 22 + } 23 + } 24 + } 25 + } 26 + 27 + func clean() error { 28 + path := "./uploads" 29 + ee, err := os.ReadDir(path) 30 + if err != nil { 31 + return err 32 + } 33 + now := time.Now() 34 + for _, e := range ee { 35 + info, err := e.Info() 36 + if err != nil { 37 + return err 38 + } 39 + t := info.ModTime() 40 + 41 + if now.Before(t.Add(12 * time.Second)) { 42 + continue 43 + } 44 + os.Remove(filepath.Join(path, info.Name())) 45 + log.Println("cleaned!") 46 + } 47 + return nil 48 + }
+5
cmd/main.go
··· 1 1 package main 2 2 3 3 import ( 4 + "context" 4 5 "fmt" 5 6 "log" 6 7 "net/http" 7 8 8 9 "github.com/joho/godotenv" 10 + "tangled.org/moth11.net/88x31/blobs" 9 11 "tangled.org/moth11.net/88x31/db" 10 12 "tangled.org/moth11.net/88x31/handler" 11 13 "tangled.org/moth11.net/88x31/oauth" ··· 23 25 log.Fatal(err) 24 26 } 25 27 h := handler.MakeHandler(store, svc) 28 + ctx, done := context.WithCancel(context.Background()) 29 + blobs.Cleanup(ctx) 26 30 log.Fatal(http.ListenAndServe(":8081", h.Serve())) 31 + done() 27 32 }