package main import ( "flag" "fmt" "io/ioutil" "log" "os" "path" "time" ) 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") 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 ") return } uploadsDir, err := ioutil.ReadDir(*uploadDir) if err != nil { log.Fatalf("failed to open directory %s: %s\n", *uploadDir, err) } for _, entry := range uploadsDir { 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) } } } }