diff --git a/internal/components/gallery/image.go b/internal/components/gallery/image.go index c249d7a..c834c08 100644 --- a/internal/components/gallery/image.go +++ b/internal/components/gallery/image.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images" "github.com/valkey-io/valkey-go" ) @@ -22,6 +23,10 @@ type Image struct { Height int } +func (i Image) URL() string { + return images.URLFromUID(i.UID) +} + const ( imagesKey = "gallery:images:list" hashKey = "gallery:images:hash" diff --git a/internal/components/images/helpers.go b/internal/components/images/helpers.go index 5ee781d..1c58981 100644 --- a/internal/components/images/helpers.go +++ b/internal/components/images/helpers.go @@ -33,6 +33,11 @@ func UIDFromPath(path string) (uid string, err error) { func URLFromPath(path string) (url string, err error) { uid, err := UIDFromPath(path) + url = URLFromUID(uid) + return +} + +func URLFromUID(uid string) (url string) { url = "/images/" + uid return } diff --git a/internal/handlers/restgallery/controller.go b/internal/handlers/restgallery/controller.go index cf8c466..7ec0f27 100644 --- a/internal/handlers/restgallery/controller.go +++ b/internal/handlers/restgallery/controller.go @@ -1,12 +1,16 @@ package restgallery import ( + "fmt" "log/slog" "net/http" + "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer" "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/pages" ) +const preloadCount = 6 + func (h *Handler) gallery(w http.ResponseWriter, r *http.Request) { imgs, err := h.srv.GetGallerySet(r.Context()) if err != nil { @@ -14,5 +18,14 @@ func (h *Handler) gallery(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } - h.r.Render(r.Context(), w, r, pages.GallerySet(imgs)) + ctx := r.Context() + for i, image := range imgs.LG.Images { + if i > preloadCount { + break + } + slog.Info("preloading images", "img", image.UID) + ctx = templer.AppendHeader(ctx, fmt.Sprintf(``, image.URL())) + } + + h.r.Render(ctx, w, r, pages.GallerySet(imgs)) }