feat: image gallery (#2)
/ publish (push) Successful in 4m4s

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
2026-06-01 14:07:50 +02:00
parent ee3eba3cd9
commit 763163c24e
30 changed files with 513 additions and 44 deletions
+22 -6
View File
@@ -21,7 +21,7 @@ import (
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)
if err != nil {
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
}
func (s *Service) getImage(uid string) (file []byte, mimeType string, err error) {
path, err := PathFromUID(uid)
func (s *Service) getImage(path string) (file []byte, mimeType string, err error) {
if err != nil {
err = fmt.Errorf("could not get path: %w", err)
return
}
mimeType, err = s.getMime(path)
mimeType, err = s.GetMime(path)
if err != nil {
return
}
@@ -59,7 +58,19 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
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 {
err = fmt.Errorf("could not Acquire semaphore: %w", err)
return
@@ -67,7 +78,12 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
}
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 {
return
}
@@ -15,7 +15,7 @@ import (
func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
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) {
@@ -29,7 +29,7 @@ func BenchmarkService_GetImage(b *testing.B) {
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.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 {
b.Error(err)
}
+12
View File
@@ -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)
}
+2 -2
View File
@@ -18,10 +18,10 @@ type Service struct {
fs fileSystem
cache valkey.Client
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{
fs: fs,
cache: cache,