2022-02-06 02:22:23 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2022-03-31 16:10:56 +04:00
|
|
|
"fmt"
|
2022-02-06 02:22:23 +04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-05-26 13:01:15 +04:00
|
|
|
var (
|
2023-08-05 19:28:05 +04:00
|
|
|
uploadDir *string = flag.String("dir", "/srv/upload", "path to a directory where uploaded files are stored")
|
|
|
|
expiry *time.Duration = flag.Duration("expiry", 36*time.Hour, "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
|
|
|
|
2023-04-24 01:05:07 +04:00
|
|
|
var version string
|
|
|
|
|
2022-02-07 22:04:40 +04:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2023-05-24 22:09:04 +04:00
|
|
|
log.SetFlags(log.Llongfile)
|
2022-02-07 22:04:40 +04:00
|
|
|
|
2022-03-31 16:10:56 +04:00
|
|
|
if *showVersion {
|
2023-04-24 01:05:07 +04:00
|
|
|
fmt.Println("dwelling-upload-clean Ver. ", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev <me@arav.su>")
|
2022-03-31 16:10:56 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-01 16:42:39 +04:00
|
|
|
uploadsDir, err := os.ReadDir(*uploadDir)
|
2022-02-06 02:22:23 +04:00
|
|
|
if err != nil {
|
2023-08-01 16:42:39 +04:00
|
|
|
log.Fatalf("failed to open a directory %s: %s\n", *uploadDir, err)
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|
|
|
|
|
2022-02-06 03:03:01 +04:00
|
|
|
for _, entry := range uploadsDir {
|
2023-08-05 05:07:59 +04:00
|
|
|
file, err := os.Stat(path.Join(*uploadDir, entry.Name()))
|
2023-08-01 16:42:39 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to stat a file %s: %s", entry.Name(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-05 19:28:05 +04:00
|
|
|
if time.Now().UTC().Sub(file.ModTime().UTC()) >= *expiry {
|
2023-05-24 22:09:41 +04:00
|
|
|
if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil {
|
2023-08-01 16:42:39 +04:00
|
|
|
log.Printf("failed to remove a file %s: %s", entry.Name(), err)
|
2022-02-06 02:22:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|