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

49 lines
1.1 KiB
Go
Raw Normal View History

2022-02-06 02:22:23 +04:00
package main
import (
"flag"
"fmt"
2022-02-06 02:22:23 +04:00
"log"
"os"
"path"
"time"
)
2023-05-26 13:01:15 +04:00
var (
uploadDir *string = flag.String("dir", "/srv/upload", "path to a directory where uploaded files are stored")
expiry *int64 = flag.Int64("expiry", 36, "keep files for this much hours")
2023-05-26 13:01:15 +04:00
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
}
uploadsDir, err := os.ReadDir(*uploadDir)
2022-02-06 02:22:23 +04:00
if err != nil {
log.Fatalf("failed to open a directory %s: %s\n", *uploadDir, err)
2022-02-06 02:22:23 +04:00
}
for _, entry := range uploadsDir {
file, err := os.Stat(entry.Name())
if err != nil {
log.Printf("failed to stat a file %s: %s", entry.Name(), err)
continue
}
if time.Now().UTC().Sub(file.ModTime().UTC()) >= time.Duration(*expiry)*time.Hour {
2023-05-24 22:09:41 +04:00
if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil {
log.Printf("failed to remove a file %s: %s", entry.Name(), err)
2022-02-06 02:22:23 +04:00
}
}
}
}