Skip to content

Commit

Permalink
Use os.ReadDir ratherthan WalkDir
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 2, 2024
1 parent 2d77843 commit 900e7e3
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"flag"
"fmt"
"instafix/handlers"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 900e7e3

Please sign in to comment.