From 900e7e3a7efbaf8ac6db2b81efe50bd6bf401e89 Mon Sep 17 00:00:00 2001 From: Akmal Date: Tue, 2 Jul 2024 19:23:10 +0700 Subject: [PATCH] Use os.ReadDir ratherthan WalkDir --- main.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 5486b45..4108f71 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,7 @@ import ( "flag" "fmt" "instafix/handlers" - "io/fs" "os" - "path/filepath" "strconv" "strings" "time" @@ -157,24 +155,27 @@ func main() { // Remove file in static folder until below threshold func evictStatic(threshold int64) { var dirSize int64 = 0 - readSize := func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } + dir, err := os.ReadDir("static") + if err != nil { + log.Error().Err(err).Msg("Failed to read static folder") + return + } + for _, d := range dir { if !d.IsDir() { - if dirSize > threshold { - err := os.Remove(path) - return err - } - info, err := d.Info() - if err != nil { - return err + continue + } + if dirSize > threshold { + if err := os.Remove(d.Name()); err != nil { + log.Error().Err(err).Msg("Failed to remove file") } - dirSize += info.Size() + continue + } + info, err := d.Info() + if err != nil { + continue } - return nil + dirSize += info.Size() } - filepath.WalkDir("static", readSize) } // Remove cache from Pebble if already expired