1
0

ioutil package is deprecated, so its ReadDir() was replaced by os. ReadDir() and os.Stat(). Also fixed existing error messages a little.

This commit is contained in:
Alexander Andreev 2023-08-01 16:42:39 +04:00
parent 1c7f03053f
commit e430f0bfc9
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path" "path"
@ -28,15 +27,21 @@ func main() {
return return
} }
uploadsDir, err := ioutil.ReadDir(*uploadDir) uploadsDir, err := os.ReadDir(*uploadDir)
if err != nil { if err != nil {
log.Fatalf("failed to open directory %s: %s\n", *uploadDir, err) log.Fatalf("failed to open a directory %s: %s\n", *uploadDir, err)
} }
for _, entry := range uploadsDir { for _, entry := range uploadsDir {
if time.Now().UTC().Sub(entry.ModTime().UTC()) >= time.Duration(*keepForHours)*time.Hour { 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 {
if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil { if err := os.Remove(path.Join(*uploadDir, entry.Name())); err != nil {
log.Printf("failed to remove file %s: %s", entry.Name(), err) log.Printf("failed to remove a file %s: %s", entry.Name(), err)
} }
} }
} }