47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"dwelling-upload/internal/configuration"
|
|
"dwelling-upload/pkg/utils"
|
|
"flag"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
config, err := configuration.LoadConfiguration(*configPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
_ = config
|
|
|
|
uploadsDir, err := ioutil.ReadDir(config.Uploads.Directory)
|
|
if err != nil {
|
|
log.Fatalf("failed to open directory %s: %s\n", config.Uploads.Directory, err)
|
|
}
|
|
|
|
var deletedCount int64 = 0
|
|
var deletedSize int64 = 0
|
|
|
|
for _, entry := range uploadsDir {
|
|
if time.Duration(entry.ModTime().UTC().Sub(time.Now().UTC()).Hours()) >= time.Duration(config.Uploads.Limits.KeepForHours)*time.Hour {
|
|
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++
|
|
}
|
|
}
|
|
|
|
_, _, cFSz := utils.ConvertFileSize(deletedSize)
|
|
|
|
log.Println(deletedCount, "file(s) in total of", cFSz, "was removed during this run.")
|
|
}
|