2022-02-06 02:22:23 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-upload/internal/configuration"
|
2022-02-06 03:03:01 +04:00
|
|
|
"dwelling-upload/pkg/utils"
|
2022-02-06 02:22:23 +04:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
|
2022-02-06 02:22:23 +04:00
|
|
|
|
|
|
|
func main() {
|
2022-02-06 03:03:01 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
config, err := configuration.LoadConfiguration(*configPath)
|
2022-02-06 02:22:23 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
_ = config
|
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
var deletedCount int64 = 0
|
|
|
|
var deletedSize int64 = 0
|
2022-02-06 02:22:23 +04:00
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
for _, entry := range uploadsDir {
|
2022-02-06 02:26:55 +04:00
|
|
|
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)
|
|
|
|
}
|
2022-02-06 03:03:01 +04:00
|
|
|
deletedSize += entry.Size()
|
|
|
|
deletedCount++
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:03:01 +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
|
|
|
}
|