feat(gallery): caching

This commit is contained in:
2026-06-01 12:38:09 +02:00
parent 8a72b96e66
commit efcc32b522
12 changed files with 146 additions and 66 deletions
+12 -48
View File
@@ -2,11 +2,8 @@ package gallery
import (
"context"
"log/slog"
"math"
"path"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
"github.com/InfinityTools/go-binpack2d"
)
@@ -16,32 +13,6 @@ type Gallery struct {
Images []Image
}
type Image struct {
UID string
X int
Y int
Width int
Height int
}
func (s *Service) GetImages(ctx context.Context) (imgs []images.Image, err error) {
files, err := s.fs.ReadDir(s.cfg.Path)
if err != nil {
return
}
for _, file := range files {
imagePath := path.Join(s.cfg.Path, file.Name())
img, err := s.img.GetImage(imagePath)
if err != nil || img.Image == nil {
continue
}
imgs = append(imgs, img)
}
return
}
func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, err error) {
imgs, err := s.GetImages(ctx)
if err != nil {
@@ -51,25 +22,19 @@ func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, e
packer := binpack2d.Create(50, 999999)
for len(imgs) > 0 {
unused := make([]images.Image, 0, len(imgs))
unused := make([]Image, 0, len(imgs))
for _, img := range imgs {
width, height := adjustImageSize(img, 200)
rect, ok := packer.Insert(width, height, binpack2d.RULE_BEST_AREA_FIT)
adjustImageSize(&img, 200)
rect, ok := packer.Insert(img.Width, img.Height, binpack2d.RULE_BEST_SHORT_SIDE_FIT)
if !ok {
unused = append(unused, img)
continue
}
uid, err := img.UID()
if err != nil {
return gallery, err
}
gallery.Images = append(gallery.Images, Image{
UID: uid,
X: rect.X,
Y: rect.Y,
Width: width,
Height: height,
})
img.X = rect.X
img.Y = rect.Y
gallery.Images = append(gallery.Images, img)
}
imgs = unused
}
@@ -79,21 +44,20 @@ func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, e
gallery.GridHeight = packer.GetHeight()
gallery.GridWith = packer.GetWidth()
slog.Info("serving gallery", "g", gallery)
return
}
// adjustImageSize adjusts the image size so that the aspect ratio stays the same
// but the image area is the given parameter.
func adjustImageSize(img images.Image, area int) (width, height int) {
func adjustImageSize(img *Image, area int) {
// Calculate the original aspect ratio (W / H)
aspectRatio := float64(img.Bounds().Dx()) / float64(img.Bounds().Dy())
aspectRatio := float64(img.Width) / float64(img.Height)
// Calculate new height and width using the derived formulas
newHeight := math.Sqrt(float64(area) / aspectRatio)
newWidth := newHeight * aspectRatio
// Round to the nearest integer to minimize precision loss
return int(math.Round(newWidth)), int(math.Round(newHeight))
img.Width = int(math.Round(newWidth))
img.Height = int(math.Round(newHeight))
}