diff --git a/cmd/backend/images.go b/cmd/backend/images.go index 398e201..a8dce5f 100644 --- a/cmd/backend/images.go +++ b/cmd/backend/images.go @@ -6,11 +6,9 @@ import ( "image" "io" "io/fs" - "log" "os" "path/filepath" "strings" - "time" "github.com/chai2010/webp" "github.com/disintegration/imaging" @@ -49,58 +47,67 @@ func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, err return filename, nil } -func CleanUpImages(c *Config) { - for { - if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err +func CleanUpImages(c *Config, db *DB) error { + if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error { + if err != nil { + return fmt.Errorf("error walking images filepath: %v", err) + } + + if !info.IsDir() { + imageName := info.Name() + imagePath := path + imageWasFound := false + + if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error { + if err != nil { + return fmt.Errorf("error walking articles filepath: %v", err) + } + + if !info.IsDir() { + mdFile, err := os.Open(path) + if err != nil { + return fmt.Errorf("error opening article %v: %v", info.Name(), err) + } + defer mdFile.Close() + + scanner := bufio.NewScanner(mdFile) + + for scanner.Scan() { + if strings.Contains(scanner.Text(), imageName) { + imageWasFound = true + } + } + + return scanner.Err() + } + + return nil + }); err != nil { + return fmt.Errorf("error walking articles filepath: %v", err) } - if !info.IsDir() { - imageName := info.Name() - absImageName := path + users, err := db.GetAllUsers(c) + if err != nil { + return fmt.Errorf("error getting all users: %v", err) + } - if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } - - if !info.IsDir() { - mdFile, err := os.Open(path) - if err != nil { - return err - } - defer mdFile.Close() - - scanner := bufio.NewScanner(mdFile) - imageWasFound := false - - for scanner.Scan() { - if strings.Contains(scanner.Text(), imageName) { - imageWasFound = true - } - } - - if !imageWasFound { - if err = os.Remove(absImageName); err != nil { - return err - } - } - - return scanner.Err() - } - - return nil - }); err != nil { - return err + for _, user := range users { + if imageName == user.ProfilePicLink { + imageWasFound = true } } - return nil - }); err != nil { - log.Println(err) + if !imageWasFound { + if err = os.Remove(imagePath); err != nil { + return fmt.Errorf("error removing unused image: %v", err) + } + } } - time.Sleep(time.Hour) + return nil + }); err != nil { + return fmt.Errorf("error cleaning up: %v", err) } + + return nil } diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 0f1b6a5..c8947f5 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -846,6 +846,13 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun return } + go func(c *b.Config, db *b.DB) { + if err = b.CleanUpImages(c, db); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + }(c, db) + feed, err := b.GenerateAtomFeed(c, db) if err != nil { log.Println(err) diff --git a/cmd/main.go b/cmd/main.go index 7c047bd..bb07bca 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,6 +4,7 @@ import ( "log" "net/http" "os" + "time" b "streifling.com/jason/cpolis/cmd/backend" c "streifling.com/jason/cpolis/cmd/calls" @@ -32,7 +33,14 @@ func main() { sessions, sessionExpiryChan := f.StartSessions() defer close(sessionExpiryChan) - // go b.CleanUpImages(config) + go func(c *b.Config, db *b.DB) { + for { + if err = b.CleanUpImages(c, db); err != nil { + log.Println(err) + } + time.Sleep(time.Hour * 24) + } + }(config, db) mux := http.NewServeMux() mux.Handle("/web/static/", http.StripPrefix("/web/static/",