simplefileshare/walker.go

45 lines
814 B
Go
Raw Normal View History

2021-08-31 19:07:19 +05:00
package main
import (
"os"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
)
2024-07-10 16:16:15 +05:00
func removeOldFilesThread(olderThan time.Duration) {
2021-09-22 19:05:28 +05:00
ticker := time.NewTicker(time.Hour)
2021-08-31 19:07:19 +05:00
2024-07-10 16:16:02 +05:00
for range ticker.C {
2021-08-31 19:07:19 +05:00
log.Debugln("Removing old files...")
2024-07-10 16:16:15 +05:00
err := removeOldFiles(olderThan)
2021-08-31 19:07:19 +05:00
if err != nil {
log.Println(err)
}
log.Debugln("Removing old files completed.")
}
}
2024-07-10 16:16:15 +05:00
func removeOldFiles(olderThan time.Duration) error {
2021-08-31 19:07:19 +05:00
return filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if info.ModTime().Add(olderThan).Before(time.Now()) {
log.WithField("filepath", path).Debugln("Removing file...")
err := os.Remove(path)
if err != nil {
log.Println(err)
}
}
return nil
})
}