feat: image gallery #2
@@ -17,9 +17,6 @@ RUN pnpm run build:css && pnpm run build:js
|
|||||||
FROM golang:1.26-alpine AS go-builder
|
FROM golang:1.26-alpine AS go-builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install git for potential private modules (though not strictly needed here)
|
|
||||||
RUN apk add --no-cache git
|
|
||||||
|
|
||||||
# Download Go modules
|
# Download Go modules
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
@@ -41,9 +38,6 @@ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/server ./cmd/schr
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install root certificates and timezone data
|
|
||||||
RUN apk --no-cache add ca-certificates tzdata
|
|
||||||
|
|
||||||
# Copy the compiled binary from the builder stage
|
# Copy the compiled binary from the builder stage
|
||||||
COPY --from=go-builder /app/server .
|
COPY --from=go-builder /app/server .
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
.PHONY: run
|
||||||
|
run: docker
|
||||||
|
air
|
||||||
|
|
||||||
|
.PHONY: docker
|
||||||
|
docker:
|
||||||
|
docker compose up -d
|
||||||
@@ -30,11 +30,7 @@ func main() {
|
|||||||
slog.Error("could not read configuaration", "err", err)
|
slog.Error("could not read configuaration", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.SetDefault(
|
setupLogger(&cfg)
|
||||||
slog.New(
|
|
||||||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
err = server.Start(ctx, cfg)
|
err = server.Start(ctx, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,3 +38,22 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupLogger(cfg *config.Cfg) {
|
||||||
|
opts := slog.HandlerOptions{
|
||||||
|
Level: cfg.LogLevel,
|
||||||
|
}
|
||||||
|
var handler slog.Handler
|
||||||
|
|
||||||
|
if cfg.LogJSON {
|
||||||
|
handler = slog.NewJSONHandler(os.Stdout, &opts)
|
||||||
|
} else {
|
||||||
|
handler = slog.NewTextHandler(os.Stdout, &opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.SetDefault(
|
||||||
|
slog.New(
|
||||||
|
handler,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
services:
|
services:
|
||||||
valkey:
|
valkey:
|
||||||
image: valkey/valkey:8
|
image: valkey/valkey:8
|
||||||
restart: always
|
|
||||||
ports:
|
ports:
|
||||||
- "6379:6379"
|
- "6379:6379"
|
||||||
command: valkey-server --requirepass "valkey"
|
command: valkey-server --requirepass "valkey"
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/InfinityTools/go-binpack2d v1.0.0 // indirect
|
||||||
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect
|
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect
|
||||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/InfinityTools/go-binpack2d v1.0.0 h1:l15wC3zDlR0FoffIcPze0qbszshmGCpMncDiAxpWnE8=
|
||||||
|
github.com/InfinityTools/go-binpack2d v1.0.0/go.mod h1:3hwEmVO6ffChGwW55BVr9k4rzSYGow0OH2B8bjEdO6o=
|
||||||
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e h1:HjVbSQHy+dnlS6C3XajZ69NYAb5jbGNfHanvm1+iYlo=
|
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e h1:HjVbSQHy+dnlS6C3XajZ69NYAb5jbGNfHanvm1+iYlo=
|
||||||
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ=
|
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ=
|
||||||
github.com/a-h/templ v0.3.977 h1:kiKAPXTZE2Iaf8JbtM21r54A8bCNsncrfnokZZSrSDg=
|
github.com/a-h/templ v0.3.977 h1:kiKAPXTZE2Iaf8JbtM21r54A8bCNsncrfnokZZSrSDg=
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package gallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/InfinityTools/go-binpack2d"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Gallery struct {
|
||||||
|
GridWith int
|
||||||
|
GridHeight int
|
||||||
|
Images []Image
|
||||||
|
}
|
||||||
|
type Set struct {
|
||||||
|
SM Gallery
|
||||||
|
LG Gallery
|
||||||
|
XLG Gallery
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetGallerySet(ctx context.Context) (gallerySet Set, err error) {
|
||||||
|
gallerySet.SM, err = s.GetGallery(ctx, 50)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gallerySet.LG, err = s.GetGallery(ctx, 80)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
gallerySet.XLG, err = s.GetGallery(ctx, 120)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, err error) {
|
||||||
|
imgs, err := s.GetImages(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
packer := binpack2d.Create(width, 999999)
|
||||||
|
|
||||||
|
for len(imgs) > 0 {
|
||||||
|
unused := make([]Image, 0, len(imgs))
|
||||||
|
for _, img := range imgs {
|
||||||
|
adjustImageSize(&img, 200)
|
||||||
|
rect, ok := packer.Insert(img.Width, img.Height, binpack2d.RULE_BEST_SHORT_SIDE_FIT)
|
||||||
|
if !ok {
|
||||||
|
unused = append(unused, img)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
img.X = rect.X
|
||||||
|
img.Y = rect.Y
|
||||||
|
|
||||||
|
gallery.Images = append(gallery.Images, img)
|
||||||
|
}
|
||||||
|
imgs = unused
|
||||||
|
}
|
||||||
|
|
||||||
|
packer.ShrinkBin(false)
|
||||||
|
|
||||||
|
gallery.GridHeight = packer.GetHeight()
|
||||||
|
gallery.GridWith = packer.GetWidth()
|
||||||
|
|
||||||
|
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 *Image, area int) {
|
||||||
|
// Calculate the original aspect ratio (W / H)
|
||||||
|
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
|
||||||
|
img.Width = int(math.Round(newWidth))
|
||||||
|
img.Height = int(math.Round(newHeight))
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package gallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fileWithPath struct {
|
||||||
|
os.FileInfo
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileWithPath) Name() string {
|
||||||
|
return path.Join(f.path, f.FileInfo.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) readDirRecursive(path string) (files []os.FileInfo, err error) {
|
||||||
|
return s.readDirRecursiveTo([]os.FileInfo{}, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) readDirRecursiveTo(files []os.FileInfo, p ...string) ([]os.FileInfo, error) {
|
||||||
|
localFiles, err := s.fs.ReadDir(path.Join(p...))
|
||||||
|
if err != nil {
|
||||||
|
return files, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range localFiles {
|
||||||
|
if file.IsDir() {
|
||||||
|
newP := append(p, file.Name())
|
||||||
|
files, err = s.readDirRecursiveTo(files, newP...)
|
||||||
|
if err != nil {
|
||||||
|
return files, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
files = append(files, fileWithPath{file, path.Join(p[1:]...)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package gallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/valkey-io/valkey-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Image struct {
|
||||||
|
UID string
|
||||||
|
X int
|
||||||
|
Y int
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
imagesKey = "gallery:images:list"
|
||||||
|
hashKey = "gallery:images:hash"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) autoRefresh() {
|
||||||
|
t := time.NewTicker(time.Minute)
|
||||||
|
for {
|
||||||
|
ctx, cancle := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
|
||||||
|
if err := s.assembleImageList(ctx); err != nil {
|
||||||
|
slog.Error("error while refreshing gallery", "err", err)
|
||||||
|
}
|
||||||
|
cancle()
|
||||||
|
<-t.C
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) assembleImageList(ctx context.Context) error {
|
||||||
|
files, err := s.readDirRecursive(s.cfg.Path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not read contents of %s: %w", s.cfg.Path, err)
|
||||||
|
}
|
||||||
|
slog.Debug("loaded files", "files", files)
|
||||||
|
|
||||||
|
savedHash, _ := s.cache.Do(ctx, s.cache.B().Get().Key(hashKey).Build()).AsBytes()
|
||||||
|
|
||||||
|
newHash := hashImageList(files)
|
||||||
|
|
||||||
|
if bytes.Equal(savedHash, newHash) {
|
||||||
|
slog.Debug("images up to date no refreshing of gallery", "savedHash", savedHash, "newHash", newHash)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("starting gallery refresh")
|
||||||
|
defer slog.Info("gallery refresh completed")
|
||||||
|
|
||||||
|
imgs := make([]Image, 0, len(files))
|
||||||
|
for _, file := range files {
|
||||||
|
if strings.HasSuffix(file.Name(), ".md") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
imagePath := path.Join(s.cfg.Path, file.Name())
|
||||||
|
img, err := s.img.GetImage(imagePath)
|
||||||
|
if err != nil || img.Image == nil {
|
||||||
|
slog.Warn("could not get image for gallery", "err", err, "img", img)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, err := img.UID()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
imgs = append(imgs, Image{
|
||||||
|
UID: uid,
|
||||||
|
Width: img.Bounds().Dx(),
|
||||||
|
Height: img.Bounds().Dy(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.cache.Do(ctx, s.cache.B().Set().Key(hashKey).Value(valkey.BinaryString(newHash)).Build()).Error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.cache.Do(ctx, s.cache.B().Set().Key(imagesKey).Value(valkey.JSON(imgs)).Build()).Error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashImageList(files []os.FileInfo) []byte {
|
||||||
|
h := fnv.New128()
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
fmt.Fprint(h, file.ModTime())
|
||||||
|
fmt.Fprint(h, file.Size())
|
||||||
|
fmt.Fprint(h, file.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum([]byte{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetImages(ctx context.Context) (images []Image, err error) {
|
||||||
|
err = s.cache.Do(ctx, s.cache.B().Get().Key(imagesKey).Build()).DecodeJSON(&images)
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package gallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||||
|
"github.com/valkey-io/valkey-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type getImager interface {
|
||||||
|
GetImage(path string) (img images.Image, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
fs filesystem.FS
|
||||||
|
cfg *config.Gallery
|
||||||
|
img getImager
|
||||||
|
cache valkey.Client
|
||||||
|
|
||||||
|
refreshIntervall time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(fs filesystem.FS, cfg *config.Gallery, img getImager, cache valkey.Client) *Service {
|
||||||
|
s := &Service{
|
||||||
|
fs: fs,
|
||||||
|
cfg: cfg,
|
||||||
|
img: img,
|
||||||
|
cache: cache,
|
||||||
|
}
|
||||||
|
go s.autoRefresh()
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ import (
|
|||||||
|
|
||||||
var ErrNotAnImage = errors.New("not an image")
|
var ErrNotAnImage = errors.New("not an image")
|
||||||
|
|
||||||
func (s *Service) getMime(path string) (mimeType string, err error) {
|
func (s *Service) GetMime(path string) (mimeType string, err error) {
|
||||||
info, err := s.fs.Stat(path)
|
info, err := s.fs.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("image file info could not be fetched: %w", err)
|
err = fmt.Errorf("image file info could not be fetched: %w", err)
|
||||||
@@ -35,13 +35,12 @@ func (s *Service) getMime(path string) (mimeType string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) getImage(uid string) (file []byte, mimeType string, err error) {
|
func (s *Service) getImage(path string) (file []byte, mimeType string, err error) {
|
||||||
path, err := PathFromUID(uid)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("could not get path: %w", err)
|
err = fmt.Errorf("could not get path: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mimeType, err = s.getMime(path)
|
mimeType, err = s.GetMime(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -59,7 +58,19 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
func (s *Service) GetImage(path string) (img Image, err error) {
|
||||||
|
rawImage, _, err := s.getImage(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
img.Path = path
|
||||||
|
img.Image, _, err = image.Decode(bytes.NewBuffer(rawImage))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetScaledImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
||||||
if err = s.seph.Acquire(ctx, 1); err != nil {
|
if err = s.seph.Acquire(ctx, 1); err != nil {
|
||||||
err = fmt.Errorf("could not Acquire semaphore: %w", err)
|
err = fmt.Errorf("could not Acquire semaphore: %w", err)
|
||||||
return
|
return
|
||||||
@@ -67,7 +78,12 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
|
|||||||
}
|
}
|
||||||
defer s.seph.Release(1)
|
defer s.seph.Release(1)
|
||||||
|
|
||||||
rawImage, mimeType, err := s.getImage(uid)
|
path, err := PathFromUID(uid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rawImage, mimeType, err := s.getImage(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
|
func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
|
||||||
valkey := mock.NewClient(ctrl)
|
valkey := mock.NewClient(ctrl)
|
||||||
return images.New(testdata.FS(), &config.ImageConfig{Concurency: 1, Quality: 80}, valkey), valkey
|
return images.New(testdata.FS(), &config.Image{Concurency: 1, Quality: 80}, valkey), valkey
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkService_GetImage(b *testing.B) {
|
func BenchmarkService_GetImage(b *testing.B) {
|
||||||
@@ -29,7 +29,7 @@ func BenchmarkService_GetImage(b *testing.B) {
|
|||||||
for b.Loop() {
|
for b.Loop() {
|
||||||
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.ErrorResult(errors.New("adf")))
|
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.ErrorResult(errors.New("adf")))
|
||||||
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.Result(valkey.ValkeyMessage{}))
|
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.Result(valkey.ValkeyMessage{}))
|
||||||
_, _, err = srv.GetImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
|
_, _, err = srv.GetScaledImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(err)
|
b.Error(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package images
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
|
||||||
|
type Image struct {
|
||||||
|
image.Image
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Image) UID() (string, error) {
|
||||||
|
return UIDFromPath(i.Path)
|
||||||
|
}
|
||||||
@@ -18,10 +18,10 @@ type Service struct {
|
|||||||
fs fileSystem
|
fs fileSystem
|
||||||
cache valkey.Client
|
cache valkey.Client
|
||||||
seph *semaphore.Weighted
|
seph *semaphore.Weighted
|
||||||
cfg *config.ImageConfig
|
cfg *config.Image
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(fs fileSystem, cfg *config.ImageConfig, cache valkey.Client) *Service {
|
func New(fs fileSystem, cfg *config.Image, cache valkey.Client) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
|
|||||||
@@ -56,9 +56,14 @@ func (s *Service) getPageHeaders(ctx context.Context, uid string) (page PageHead
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
page.md, err = s.fs.Read(contentMD.Path())
|
if contentMD != nil {
|
||||||
if err != nil {
|
|
||||||
return
|
page.md, err = s.fs.Read(contentMD.Path())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
page.md = []byte("# ...")
|
||||||
}
|
}
|
||||||
|
|
||||||
title := titleRGX.FindStringSubmatch(string(page.md))
|
title := titleRGX.FindStringSubmatch(string(page.md))
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ func SupportedLanguages(ctx context.Context) []string {
|
|||||||
s, ok := ctx.Value(serviceKey).(*Service)
|
s, ok := ctx.Value(serviceKey).(*Service)
|
||||||
if !ok {
|
if !ok {
|
||||||
slog.Error("could not extract translation service from context for getting supported languages")
|
slog.Error("could not extract translation service from context for getting supported languages")
|
||||||
|
return []string{}
|
||||||
}
|
}
|
||||||
return s.SupportedLanguages()
|
return s.SupportedLanguages()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package restgallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/pages"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) gallery(w http.ResponseWriter, r *http.Request) {
|
||||||
|
imgs, err := h.srv.GetGallerySet(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("could not get images", "err", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.r.Render(r.Context(), w, r, pages.GallerySet(imgs))
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package restgallery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/gallery"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
|
"github.com/a-h/templ"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
http.Handler
|
||||||
|
srv galleryService
|
||||||
|
r renderer
|
||||||
|
cfg *config.Gallery
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(renderer renderer, srv galleryService, cfg *config.Gallery) *Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
h := &Handler{
|
||||||
|
Handler: mux,
|
||||||
|
srv: srv,
|
||||||
|
r: renderer,
|
||||||
|
cfg: cfg,
|
||||||
|
}
|
||||||
|
|
||||||
|
mux.HandleFunc(fmt.Sprintf("GET /%s", url.PathEscape(cfg.Path)), h.gallery)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
type galleryService interface {
|
||||||
|
GetGallerySet(ctx context.Context) (gallerySet gallery.Set, err error)
|
||||||
|
}
|
||||||
|
type renderer interface {
|
||||||
|
Render(ctx context.Context, w io.Writer, r *http.Request, component templ.Component)
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package resthome
|
package resthome
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -24,6 +26,10 @@ func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
page, err := h.src.GetPage(r.Context(), uid)
|
page, err := h.src.GetPage(r.Context(), uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
w.WriteHeader(http.StatusRequestTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
img, mime, err := h.img.GetImage(r.Context(), uid, options)
|
img, mime, err := h.img.GetScaledImage(r.Context(), uid, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error wile serving image", "err", err, "uid", uid)
|
slog.Error("error wile serving image", "err", err, "uid", uid)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
@@ -25,5 +25,5 @@ func New(srv imageService) *Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type imageService interface {
|
type imageService interface {
|
||||||
GetImage(ctx context.Context, uid string, options images.Options) (img io.Reader, mimeType string, err error)
|
GetScaledImage(ctx context.Context, uid string, options images.Options) (img io.Reader, mimeType string, err error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,22 @@ import "time"
|
|||||||
|
|
||||||
func Default() Cfg {
|
func Default() Cfg {
|
||||||
return Cfg{
|
return Cfg{
|
||||||
Image: &ImageConfig{
|
Image: &Image{
|
||||||
Concurency: 10,
|
Concurency: 10,
|
||||||
Quality: 80,
|
Quality: 80,
|
||||||
CacheLifetime: time.Hour * 24 * 31,
|
CacheLifetime: time.Hour * 24 * 31,
|
||||||
},
|
},
|
||||||
Tracking: &UmamiConfig{
|
Tracking: &Umami{
|
||||||
ScriptSrc: "https://umami.schreifuchs.ch/script.js",
|
ScriptSrc: "https://umami.schreifuchs.ch/script.js",
|
||||||
WebsiteID: "54d8a379-77d5-4c20-b46d-5c9a6c9e39bd",
|
WebsiteID: "54d8a379-77d5-4c20-b46d-5c9a6c9e39bd",
|
||||||
},
|
},
|
||||||
|
Gallery: &Gallery{
|
||||||
|
Path: "photos",
|
||||||
|
},
|
||||||
|
Cache: &Valkey{
|
||||||
|
RefreshTime: time.Minute * 10,
|
||||||
|
LifeTime: time.Hour * 48,
|
||||||
|
},
|
||||||
|
LogJSON: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ import (
|
|||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
||||||
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
Cache *Valkey `env:", prefix=CACHE_"`
|
||||||
Image *ImageConfig `env:", prefix=IMAGE_"`
|
Image *Image `env:", prefix=IMAGE_"`
|
||||||
Tracking *UmamiConfig `env:", prefix=TRACKING_"`
|
Tracking *Umami `env:", prefix=TRACKING_"`
|
||||||
|
Gallery *Gallery `env:", prefix=GALLERY_"`
|
||||||
|
|
||||||
LogLevel slog.Level `env:"LOG_LEVEL, default=DEBUG"`
|
LogLevel slog.Level `env:"LOG_LEVEL, default=DEBUG"`
|
||||||
|
LogJSON bool `env:"LOG_JSON"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebdavConfig struct {
|
type WebdavConfig struct {
|
||||||
@@ -23,11 +25,14 @@ type WebdavConfig struct {
|
|||||||
Password string `env:"PASSWORD"`
|
Password string `env:"PASSWORD"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValkeyConfig struct {
|
type Valkey struct {
|
||||||
ClientName string `env:"CLIENTNAME"`
|
ClientName string `env:"CLIENTNAME"`
|
||||||
Username string `env:"USERNAME"`
|
Username string `env:"USERNAME"`
|
||||||
Password string `env:"PASSWORD"`
|
Password string `env:"PASSWORD"`
|
||||||
|
|
||||||
|
RefreshTime time.Duration `env:"REFRESH_TIME"`
|
||||||
|
LifeTime time.Duration `env:"LIFE_TIME"`
|
||||||
|
|
||||||
// InitAddress point to valkey nodes.
|
// InitAddress point to valkey nodes.
|
||||||
// Valkey will connect to them one by one and issue a CLUSTER SLOT command to initialize the cluster client until success.
|
// Valkey will connect to them one by one and issue a CLUSTER SLOT command to initialize the cluster client until success.
|
||||||
// If len(InitAddress) == 1 and the address is not running in cluster mode, valkey will fall back to the single client mode.
|
// If len(InitAddress) == 1 and the address is not running in cluster mode, valkey will fall back to the single client mode.
|
||||||
@@ -36,13 +41,17 @@ type ValkeyConfig struct {
|
|||||||
InitAddress []string `env:"ADDRESS"`
|
InitAddress []string `env:"ADDRESS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ImageConfig struct {
|
type Image struct {
|
||||||
Concurency int64 `env:"CONCURENCY"`
|
Concurency int64 `env:"CONCURENCY"`
|
||||||
Quality int `env:"QUALITY"`
|
Quality int `env:"QUALITY"`
|
||||||
CacheLifetime time.Duration `env:"CACHE_LIFETIME"`
|
CacheLifetime time.Duration `env:"CACHE_LIFETIME"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UmamiConfig struct {
|
type Gallery struct {
|
||||||
|
Path string `env:"PATH"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Umami struct {
|
||||||
ScriptSrc string `env:"SCRIPT_SRC"`
|
ScriptSrc string `env:"SCRIPT_SRC"`
|
||||||
WebsiteID string `env:"WEBSITE_ID"`
|
WebsiteID string `env:"WEBSITE_ID"`
|
||||||
}
|
}
|
||||||
@@ -64,5 +73,7 @@ func Read(ctx context.Context) (cfg Cfg, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("rft", "rft", cfg.Cache.RefreshTime)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ type CachedClient struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCachedClient creates a new CachedClient.
|
// NewCachedClient creates a new CachedClient.
|
||||||
func NewCachedClient(impl FS, client valkey.Client, refreshtime time.Duration) *CachedClient {
|
func NewCachedClient(impl FS, client valkey.Client, refreshtime time.Duration, lifetime time.Duration) *CachedClient {
|
||||||
c := &CachedClient{
|
c := &CachedClient{
|
||||||
impl: impl,
|
impl: impl,
|
||||||
cache: client,
|
cache: client,
|
||||||
ttl: refreshtime * 5,
|
ttl: lifetime,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -42,6 +42,7 @@ func NewCachedClient(impl FS, client valkey.Client, refreshtime time.Duration) *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CachedClient) revalidate(ctx context.Context) {
|
func (c *CachedClient) revalidate(ctx context.Context) {
|
||||||
|
slog.Info("starting revalidation")
|
||||||
go c.revalidateReadDir(ctx)
|
go c.revalidateReadDir(ctx)
|
||||||
go c.revalidateRead(ctx)
|
go c.revalidateRead(ctx)
|
||||||
go c.revalidateStat(ctx)
|
go c.revalidateStat(ctx)
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ func (c *CachedClient) Read(path string) ([]byte, error) {
|
|||||||
if err == nil && len(cached) > 0 {
|
if err == nil && len(cached) > 0 {
|
||||||
c.readHits.Add(1)
|
c.readHits.Add(1)
|
||||||
c.hits.Add(1)
|
c.hits.Add(1)
|
||||||
slog.Debug("cache hit", "key", key)
|
|
||||||
|
|
||||||
var file readFile
|
var file readFile
|
||||||
err := json.Unmarshal(cached, &file)
|
err := json.Unmarshal(cached, &file)
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ func (c *CachedClient) ReadDir(path string) ([]os.FileInfo, error) {
|
|||||||
if err := json.Unmarshal([]byte(val), &cached); err == nil {
|
if err := json.Unmarshal([]byte(val), &cached); err == nil {
|
||||||
c.readDirHits.Add(1)
|
c.readDirHits.Add(1)
|
||||||
c.hits.Add(1)
|
c.hits.Add(1)
|
||||||
slog.Debug("cache hit", "key", key)
|
|
||||||
infos := make([]os.FileInfo, len(cached))
|
infos := make([]os.FileInfo, len(cached))
|
||||||
for i, f := range cached {
|
for i, f := range cached {
|
||||||
infos[i] = f
|
infos[i] = f
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ func (c *CachedClient) Stat(path string) (info os.FileInfo, err error) {
|
|||||||
var f File
|
var f File
|
||||||
if err := json.Unmarshal([]byte(val), &f); err == nil {
|
if err := json.Unmarshal([]byte(val), &f); err == nil {
|
||||||
c.hits.Add(1)
|
c.hits.Add(1)
|
||||||
slog.Debug("cache hit", "key", key)
|
|
||||||
|
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ package server
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/gallery"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restgallery"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||||
@@ -59,11 +62,12 @@ func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
} else {
|
} else {
|
||||||
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, 5*time.Hour)
|
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, s.cfg.Cache.RefreshTime, s.cfg.Cache.LifeTime)
|
||||||
}
|
}
|
||||||
|
imageSrv := images.New(fs, s.cfg.Image, valkeyClient)
|
||||||
|
|
||||||
mux.Handle("/images/", rest.Use(
|
mux.Handle("/images/", rest.Use(
|
||||||
restimage.New(images.New(fs, s.cfg.Image, valkeyClient)),
|
restimage.New(imageSrv),
|
||||||
middleware.Cache(time.Hour*24*5),
|
middleware.Cache(time.Hour*24*5),
|
||||||
))
|
))
|
||||||
|
|
||||||
@@ -71,7 +75,14 @@ func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
|||||||
renderer := layouts.New(s.cfg)
|
renderer := layouts.New(s.cfg)
|
||||||
|
|
||||||
mux.Handle("/", rest.Use(
|
mux.Handle("/", rest.Use(
|
||||||
resthome.New(renderer, page.New(fs)),
|
func() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.Handle(path.Join("/"+s.cfg.Gallery.Path), restgallery.New(renderer, gallery.New(fs, s.cfg.Gallery, imageSrv, valkeyClient), s.cfg.Gallery))
|
||||||
|
mux.Handle("/", resthome.New(renderer, page.New(fs)))
|
||||||
|
|
||||||
|
return mux
|
||||||
|
}(),
|
||||||
translator.Middleware,
|
translator.Middleware,
|
||||||
middleware.Cache(time.Minute),
|
middleware.Cache(time.Minute),
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package pages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/gallery"
|
||||||
|
)
|
||||||
|
|
||||||
|
templ GallerySet(g gallery.Set) {
|
||||||
|
<div
|
||||||
|
class="grid gap-5 grid-cols-1 lg:hidden m-5"
|
||||||
|
>
|
||||||
|
for _,img := range g.SM.Images {
|
||||||
|
<img
|
||||||
|
src={ "/images/" + img.UID + "?w=600" }
|
||||||
|
loading="lazy"
|
||||||
|
class="w-full"
|
||||||
|
alt="Gallery item"
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="hidden lg:block 3xl:hidden m-5">
|
||||||
|
@Gallery(g.SM)
|
||||||
|
</div>
|
||||||
|
<div class="hidden 3xl:block 6xl:hidden m-5">
|
||||||
|
@Gallery(g.LG)
|
||||||
|
</div>
|
||||||
|
<div class="hidden 6xl:block m-5">
|
||||||
|
@Gallery(g.XLG)
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Gallery(g gallery.Gallery) {
|
||||||
|
<div
|
||||||
|
class="grid gap-5"
|
||||||
|
style={ fmt.Sprintf("grid-template-columns: repeat(%d, 1fr); grid-template-rows: repeat(%d, auto);", g.GridWith, g.GridHeight) }
|
||||||
|
>
|
||||||
|
for _, img := range g.Images {
|
||||||
|
<div
|
||||||
|
style={ fmt.Sprintf("grid-column: %d / span %d; grid-row: %d / span %d;aspect-ratio: %d / %d;", img.X+1, img.Width, img.Y+1, img.Height, img.Width, img.Height) }
|
||||||
|
class="overflow-hidden shadow-sm"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={ "/images/" + img.UID + "?w=800" }
|
||||||
|
loading="lazy"
|
||||||
|
class="w-full h-full object-cover"
|
||||||
|
alt="Gallery item"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user