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

42 lines
1019 B
Go
Raw Normal View History

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