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