2022-02-06 02:22:23 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-upload/internal/configuration"
|
2022-02-07 21:23:13 +04:00
|
|
|
"dwelling-upload/pkg/logging"
|
2022-02-06 03:03:01 +04:00
|
|
|
"dwelling-upload/pkg/utils"
|
2022-02-06 02:22:23 +04:00
|
|
|
"flag"
|
2022-03-31 16:10:56 +04:00
|
|
|
"fmt"
|
2022-02-06 02:22:23 +04:00
|
|
|
"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-07 21:23:13 +04:00
|
|
|
var logToStdout *bool = flag.Bool("log-stdout", false, "write logs to stdout")
|
2022-03-31 16:10:56 +04:00
|
|
|
var showVersion *bool = flag.Bool("v", false, "show version")
|
2022-02-06 02:22:23 +04:00
|
|
|
|
2022-02-07 22:04:40 +04:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-03-31 16:10:56 +04:00
|
|
|
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>")
|
2022-03-31 16:10:56 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-07 22:04:40 +04:00
|
|
|
config, err := configuration.LoadConfiguration(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:23:13 +04:00
|
|
|
if *logToStdout {
|
|
|
|
config.Log.ToStdout = true
|
|
|
|
}
|
|
|
|
|
2022-03-20 18:19:59 +04:00
|
|
|
logErr, err := logging.NewLogger(config.Log.CleanError, config.Log.ToStdout)
|
2022-02-07 21:23:13 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("failed to open error logger:", err)
|
|
|
|
}
|
|
|
|
defer logErr.Close()
|
|
|
|
|
2022-03-20 18:19:59 +04:00
|
|
|
logClean, err := logging.NewLogger(config.Log.Clean, config.Log.ToStdout)
|
2022-02-07 21:23:13 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("failed to open error logger:", err)
|
|
|
|
}
|
2022-03-20 18:19:59 +04:00
|
|
|
defer logClean.Close()
|
2022-02-06 02:22:23 +04:00
|
|
|
|
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 {
|
2022-02-07 21:23:13 +04:00
|
|
|
logErr.Fatalf("failed to open directory %s: %s\n", config.Uploads.Directory, err)
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|
|
|
|
|
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-10 23:04:27 +04:00
|
|
|
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 {
|
2022-02-10 23:06:24 +04:00
|
|
|
logErr.Println("failed to remove file ", entry.Name(), ": ", err)
|
2022-03-06 22:00:07 +04:00
|
|
|
} else {
|
|
|
|
deletedSize += entry.Size()
|
|
|
|
deletedCount++
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
_, _, cFSz := utils.ConvertFileSize(deletedSize)
|
|
|
|
|
2022-02-08 02:35:48 +04:00
|
|
|
if deletedCount > 0 {
|
2022-03-20 18:19:59 +04:00
|
|
|
logClean.Printf("%d %s", deletedCount, cFSz)
|
2022-02-08 02:35:48 +04:00
|
|
|
}
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|