Include proper image cleanup
This commit is contained in:
parent
cc8693ffaf
commit
0af4a70aed
@ -6,11 +6,9 @@ import (
|
||||
"image"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/disintegration/imaging"
|
||||
@ -49,31 +47,30 @@ func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, err
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
func CleanUpImages(c *Config) {
|
||||
for {
|
||||
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 err
|
||||
return fmt.Errorf("error walking images filepath: %v", err)
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
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 != nil {
|
||||
return err
|
||||
return fmt.Errorf("error walking articles filepath: %v", err)
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
mdFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("error opening article %v: %v", info.Name(), err)
|
||||
}
|
||||
defer mdFile.Close()
|
||||
|
||||
scanner := bufio.NewScanner(mdFile)
|
||||
imageWasFound := false
|
||||
|
||||
for scanner.Scan() {
|
||||
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 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
|
||||
}); err != nil {
|
||||
log.Println(err)
|
||||
return fmt.Errorf("error cleaning up: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -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)
|
||||
|
10
cmd/main.go
10
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/",
|
||||
|
Loading…
x
Reference in New Issue
Block a user