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

62 lines
1.5 KiB
Go
Raw Normal View History

2022-02-06 02:22:23 +04:00
package main
import (
"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")
2023-05-24 22:09:41 +04:00
var uploadDir *string = flag.String("upload-dir", "/srv/uploads", "path to a directory where uploaded files are stored")
var keepFileForHours *int64 = flag.Int64("keep-for", 36, "keep files for this much hours")
var showVersion *bool = flag.Bool("v", false, "show version")
2022-02-06 02:22:23 +04:00
var version string
func main() {
flag.Parse()
log.SetFlags(log.Llongfile)
if *showVersion {
fmt.Println("dwelling-upload-clean Ver. ", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev <me@arav.su>")
return
}
logClean, err := logging.New(path.Join(os.Getenv("LOGS_DIRECTORY"), "clean.log"))
if err != nil {
log.Fatalln("failed to open clean logger:", err)
}
defer logClean.Close()
2022-02-06 02:22:23 +04:00
2023-05-24 22:09:41 +04:00
uploadsDir, err := ioutil.ReadDir(*uploadDir)
2022-02-06 02:22:23 +04:00
if err != nil {
log.Fatalf("failed to open directory %s: %s\n", *uploadDir, 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 {
2023-05-24 22:09:41 +04:00
if time.Now().UTC().Sub(entry.ModTime().UTC()) >= time.Duration(*keepFileForHours)*time.Hour {
if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil {
log.Printf("failed to remove file %s: %s", entry.Name(), err)
} else {
deletedSize += entry.Size()
deletedCount++
2022-02-06 02:22:23 +04:00
}
}
}
_, _, cFSz := utils.ConvertFileSize(deletedSize)
if deletedCount > 0 {
logClean.Println(deletedCount, cFSz)
}
2022-02-06 02:22:23 +04:00
}