Make background image check way more efficient

This commit is contained in:
Jason Streifling 2025-01-20 20:25:50 +01:00
parent a7b6fb9705
commit 3d08cc7612

View File

@ -17,6 +17,53 @@ import (
var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging
func checkImageUsage(c *Config, db *DB, name string) (bool, error) {
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(), name) {
imageWasFound = true
return nil
}
}
return scanner.Err()
}
return nil
}); err != nil {
return false, fmt.Errorf("error walking articles filepath: %v", err)
}
if !imageWasFound {
users, err := db.GetAllUsers(c)
if err != nil {
return false, fmt.Errorf("error getting all users: %v", err)
}
for _, user := range users {
if name == user.ProfilePicLink {
return true, nil
}
}
}
return imageWasFound, nil
}
func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, error) { func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, error) {
img, err := imaging.Decode(src, imaging.AutoOrientation(true)) img, err := imaging.Decode(src, imaging.AutoOrientation(true))
if err != nil { if err != nil {
@ -56,45 +103,10 @@ func CleanUpImages(c *Config, db *DB) error {
if !info.IsDir() { if !info.IsDir() {
imageName := info.Name() imageName := info.Name()
imagePath := path imagePath := path
imageWasFound := false
if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error { imageWasFound, err := checkImageUsage(c, db, imageName)
if err != nil { if err != nil {
return fmt.Errorf("error walking articles filepath: %v", err) return fmt.Errorf("error checking image usage: %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)
}
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 !imageWasFound {