clean.go 619 B

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