1
0
dwelling-upload/cmd/dwelling-upload-clear/main.go

47 lines
1.1 KiB
Go
Raw Normal View History

2022-02-06 02:22:23 +04:00
package main
import (
"dwelling-upload/internal/configuration"
"dwelling-upload/pkg/utils"
2022-02-06 02:22:23 +04:00
"flag"
"io/ioutil"
"log"
"os"
"path"
"time"
)
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
2022-02-06 02:22:23 +04:00
func main() {
flag.Parse()
config, err := configuration.LoadConfiguration(*configPath)
2022-02-06 02:22:23 +04:00
if err != nil {
log.Fatalln(err)
}
_ = config
uploadsDir, err := ioutil.ReadDir(config.Uploads.Directory)
2022-02-06 02:22:23 +04:00
if err != nil {
log.Fatalf("failed to open directory %s: %s\n", config.Uploads.Directory, err)
}
var deletedCount int64 = 0
var deletedSize int64 = 0
2022-02-06 02:22:23 +04:00
for _, entry := range uploadsDir {
if time.Duration(entry.ModTime().UTC().Sub(time.Now().UTC()).Hours()) >= time.Duration(config.Uploads.Limits.KeepForHours)*time.Hour {
2022-02-06 02:22:23 +04:00
if err := os.Remove(path.Join(config.Uploads.Directory, entry.Name())); err != nil {
log.Fatalln("failed to remove file ", entry.Name(), ": ", err)
}
deletedSize += entry.Size()
deletedCount++
2022-02-06 02:22:23 +04:00
}
}
_, _, cFSz := utils.ConvertFileSize(deletedSize)
log.Println(deletedCount, "file(s) in total of", cFSz, "was removed during this run.")
2022-02-06 02:22:23 +04:00
}