more cleanup logics

This commit is contained in:
stefanodvx 2025-04-15 21:43:41 +02:00
parent 20245c109b
commit 78d5dfbe53
2 changed files with 31 additions and 4 deletions

View file

@ -14,7 +14,8 @@ func main() {
if err != nil { if err != nil {
log.Fatal("error loading .env file") log.Fatal("error loading .env file")
} }
util.ClearDownlods() util.CleanupDownloadsDir()
util.StartDownloadsCleanup()
ok := util.CheckFFmpeg() ok := util.CheckFFmpeg()
if !ok { if !ok {
log.Fatal("ffmpeg executable not found. please install it or add it to your PATH") log.Fatal("ffmpeg executable not found. please install it or add it to your PATH")

View file

@ -7,6 +7,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -111,10 +112,35 @@ func CheckFFmpeg() bool {
return err == nil return err == nil
} }
func ClearDownlods() { func CleanupDownloadsDir() {
downloadsDir := os.Getenv("DOWNLOADS_DIR") downloadsDir := os.Getenv("DOWNLOAD_DIR")
if downloadsDir == "" { if downloadsDir == "" {
downloadsDir = "downloads" downloadsDir = "downloads"
} }
os.RemoveAll(downloadsDir) filepath.Walk(downloadsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if path == downloadsDir {
return nil
}
if time.Since(info.ModTime()) > 30*time.Minute {
if info.IsDir() {
os.RemoveAll(path)
} else {
os.Remove(path)
}
}
return nil
})
}
func StartDownloadsCleanup() {
ticker := time.NewTicker(1 * time.Hour)
go func() {
for {
CleanupDownloadsDir()
<-ticker.C
}
}()
} }