49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
uploadDir = flag.String("dir", "/srv/upload", "path to a directory where uploaded files are stored")
|
|
expiry = flag.Duration("expiry", 36*time.Hour, "keep files for this much hours")
|
|
|
|
showVersion = 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 <me@arav.su>")
|
|
return
|
|
}
|
|
|
|
uploadsDir, err := os.ReadDir(*uploadDir)
|
|
if err != nil {
|
|
log.Fatalf("failed to open a directory %s: %s\n", *uploadDir, err)
|
|
}
|
|
|
|
for _, entry := range uploadsDir {
|
|
file, err := os.Stat(path.Join(*uploadDir, 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()) >= *expiry {
|
|
if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil {
|
|
log.Printf("failed to remove a file %s: %s", entry.Name(), err)
|
|
}
|
|
}
|
|
}
|
|
}
|