clean.go 540 B

123456789101112131415161718192021222324252627
  1. package session
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "time"
  7. )
  8. // Clean to remove all local session files which not was modified more than 24 hours
  9. func Clean(tmpdir string) error {
  10. files, err := ioutil.ReadDir(tmpdir)
  11. if err != nil {
  12. return err
  13. }
  14. for _, file := range files {
  15. if len(file.Name()) == 40 {
  16. if diff := time.Since(file.ModTime()); diff > 24*time.Hour {
  17. err = os.Remove(strings.Join([]string{tmpdir, file.Name()}, string(os.PathSeparator)))
  18. if err != nil {
  19. return err
  20. }
  21. }
  22. }
  23. }
  24. return nil
  25. }