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

69 lines
1.6 KiB
Go
Raw Normal View History

2022-02-06 02:22:23 +04:00
package main
import (
"dwelling-upload/internal/configuration"
"dwelling-upload/pkg/logging"
"dwelling-upload/pkg/utils"
2022-02-06 02:22:23 +04:00
"flag"
"fmt"
2022-02-06 02:22:23 +04:00
"io/ioutil"
"log"
"os"
"path"
"time"
)
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
var showVersion *bool = flag.Bool("v", false, "show version")
2022-02-06 02:22:23 +04:00
func main() {
flag.Parse()
if *showVersion {
2023-03-12 00:15:25 +04:00
fmt.Println("dwelling-upload-clean Ver. 23.10.0\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev <me@arav.su>")
return
}
config, err := configuration.LoadConfiguration(*configPath)
if err != nil {
log.Fatalln(err)
}
logErr, err := logging.New(config.Log.CleanError)
if err != nil {
log.Fatalln("failed to open error logger:", err)
}
defer logErr.Close()
logClean, err := logging.New(config.Log.Clean)
if err != nil {
log.Fatalln("failed to open error logger:", err)
}
defer logClean.Close()
2022-02-06 02:22:23 +04:00
uploadsDir, err := ioutil.ReadDir(config.Uploads.Directory)
2022-02-06 02:22:23 +04:00
if err != nil {
logErr.Fatalf("failed to open directory %s: %s\n", config.Uploads.Directory, err)
2022-02-06 02:22:23 +04:00
}
var deletedCount int64 = 0
var deletedSize int64 = 0
2022-02-06 02:22:23 +04:00
for _, entry := range uploadsDir {
if time.Now().UTC().Sub(entry.ModTime().UTC()) >= 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 {
logErr.Println("failed to remove file ", entry.Name(), ": ", err)
} else {
deletedSize += entry.Size()
deletedCount++
2022-02-06 02:22:23 +04:00
}
}
}
_, _, cFSz := utils.ConvertFileSize(deletedSize)
if deletedCount > 0 {
logClean.Printf("%d %s", deletedCount, cFSz)
}
2022-02-06 02:22:23 +04:00
}