From d206429bab1df0fea065fdb0d13b1b56f9d2920a Mon Sep 17 00:00:00 2001 From: Logan Fick Date: Thu, 4 Jul 2024 19:41:45 -0400 Subject: [PATCH] Refactored directory cleanup function to remove contents instead of recreating directory. --- utils/utils.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 2a51c367a..6a8772537 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "math/rand" "net/url" "os" @@ -302,14 +303,18 @@ func VerifyFFMpegPath(path string) error { return nil } -// CleanupDirectory removes the directory and makes it fresh again. Throws fatal error on failure. +// CleanupDirectory removes all contents within the directory. Throws fatal error on failure. func CleanupDirectory(path string) { log.Traceln("Cleaning", path) - if err := os.RemoveAll(path); err != nil { - log.Fatalln("Unable to remove directory. Please check the ownership and permissions", err) + entries, err := ioutil.ReadDir(path) + if err != nil { + log.Fatalln("Unable to read contents of directory. Please check the ownership and permissions", err) } - if err := os.MkdirAll(path, 0o750); err != nil { - log.Fatalln("Unable to create directory. Please check the ownership and permissions", err) + for _, entry := range entries { + entryPath := filepath.Join(path, entry.Name()) + if err := os.RemoveAll(entryPath); err != nil { + log.Fatalln("Unable to remove directory. Please check the ownership and permissions", err) + } } }