Include proper image cleanup

This commit is contained in:
Jason Streifling 2025-01-19 10:04:16 +01:00
parent cc8693ffaf
commit 0af4a70aed
3 changed files with 70 additions and 48 deletions

View File

@ -6,11 +6,9 @@ import (
"image" "image"
"io" "io"
"io/fs" "io/fs"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/chai2010/webp" "github.com/chai2010/webp"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
@ -49,31 +47,30 @@ func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, err
return filename, nil return filename, nil
} }
func CleanUpImages(c *Config) { func CleanUpImages(c *Config, db *DB) error {
for {
if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error { if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return err return fmt.Errorf("error walking images filepath: %v", err)
} }
if !info.IsDir() { if !info.IsDir() {
imageName := info.Name() imageName := info.Name()
absImageName := path imagePath := path
imageWasFound := false
if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error { if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return err return fmt.Errorf("error walking articles filepath: %v", err)
} }
if !info.IsDir() { if !info.IsDir() {
mdFile, err := os.Open(path) mdFile, err := os.Open(path)
if err != nil { if err != nil {
return err return fmt.Errorf("error opening article %v: %v", info.Name(), err)
} }
defer mdFile.Close() defer mdFile.Close()
scanner := bufio.NewScanner(mdFile) scanner := bufio.NewScanner(mdFile)
imageWasFound := false
for scanner.Scan() { for scanner.Scan() {
if strings.Contains(scanner.Text(), imageName) { if strings.Contains(scanner.Text(), imageName) {
@ -81,26 +78,36 @@ func CleanUpImages(c *Config) {
} }
} }
if !imageWasFound {
if err = os.Remove(absImageName); err != nil {
return err
}
}
return scanner.Err() return scanner.Err()
} }
return nil return nil
}); err != nil { }); err != nil {
return err return fmt.Errorf("error walking articles filepath: %v", err)
}
users, err := db.GetAllUsers(c)
if err != nil {
return fmt.Errorf("error getting all users: %v", err)
}
for _, user := range users {
if imageName == user.ProfilePicLink {
imageWasFound = true
}
}
if !imageWasFound {
if err = os.Remove(imagePath); err != nil {
return fmt.Errorf("error removing unused image: %v", err)
}
} }
} }
return nil return nil
}); err != nil { }); err != nil {
log.Println(err) return fmt.Errorf("error cleaning up: %v", err)
} }
time.Sleep(time.Hour) return nil
}
} }

View File

@ -846,6 +846,13 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
return 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) feed, err := b.GenerateAtomFeed(c, db)
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
b "streifling.com/jason/cpolis/cmd/backend" b "streifling.com/jason/cpolis/cmd/backend"
c "streifling.com/jason/cpolis/cmd/calls" c "streifling.com/jason/cpolis/cmd/calls"
@ -32,7 +33,14 @@ func main() {
sessions, sessionExpiryChan := f.StartSessions() sessions, sessionExpiryChan := f.StartSessions()
defer close(sessionExpiryChan) 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 := http.NewServeMux()
mux.Handle("/web/static/", http.StripPrefix("/web/static/", mux.Handle("/web/static/", http.StripPrefix("/web/static/",