Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eab7eb3879 | ||
|
|
fca1030e1a |
@@ -60,6 +60,13 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
func (s *Service) GetImage(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
|
||||||
|
|
||||||
|
}
|
||||||
|
defer s.seph.Release(1)
|
||||||
|
|
||||||
rawImage, mimeType, err := s.getImage(uid)
|
rawImage, mimeType, err := s.getImage(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -83,7 +90,7 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
|
|||||||
outBuff := bytes.NewBuffer(make([]byte, 0, 1024))
|
outBuff := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
|
|
||||||
if options.Quality == 0 {
|
if options.Quality == 0 {
|
||||||
options.Quality = 80
|
options.Quality = s.cfg.Quality
|
||||||
}
|
}
|
||||||
|
|
||||||
err = jpeg.Encode(outBuff, decImage, &jpeg.Options{Quality: options.Quality})
|
err = jpeg.Encode(outBuff, decImage, &jpeg.Options{Quality: options.Quality})
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package images
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
"github.com/valkey-io/valkey-go"
|
"github.com/valkey-io/valkey-go"
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
@@ -15,12 +17,16 @@ type Options struct {
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
fs fileSystem
|
fs fileSystem
|
||||||
cache valkey.Client
|
cache valkey.Client
|
||||||
|
seph *semaphore.Weighted
|
||||||
|
cfg *config.ImageConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(fs fileSystem, cache valkey.Client) *Service {
|
func New(fs fileSystem, cfg *config.ImageConfig, cache valkey.Client) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
|
seph: semaphore.NewWeighted(cfg.Concurency),
|
||||||
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
func Default() Cfg {
|
||||||
|
return Cfg{
|
||||||
|
Image: &ImageConfig{
|
||||||
|
Concurency: 10,
|
||||||
|
Quality: 80,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
FileSystem *WebdavConfig `env:", prefix=FILESYSTEM_"`
|
||||||
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
||||||
|
Image *ImageConfig `env:", prefix=IMAGE_"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebdavConfig struct {
|
type WebdavConfig struct {
|
||||||
@@ -30,7 +31,13 @@ type ValkeyConfig struct {
|
|||||||
InitAddress []string `env:"ADDRESS"`
|
InitAddress []string `env:"ADDRESS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImageConfig struct {
|
||||||
|
Concurency int64 `env:"CONCURENCY"`
|
||||||
|
Quality int `env:"QUALITY"`
|
||||||
|
}
|
||||||
|
|
||||||
func Read(ctx context.Context) (cfg Cfg, err error) {
|
func Read(ctx context.Context) (cfg Cfg, err error) {
|
||||||
|
cfg = Default()
|
||||||
err = envconfig.ProcessWith(ctx, &envconfig.Config{
|
err = envconfig.ProcessWith(ctx, &envconfig.Config{
|
||||||
Target: &cfg,
|
Target: &cfg,
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func (s *Server) registerOther() (h http.Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = resthome.New(mux, page.New(fs))
|
_ = resthome.New(mux, page.New(fs))
|
||||||
_ = restimage.New(mux, images.New(fs, valkeyClient))
|
_ = restimage.New(mux, images.New(fs, s.cfg.Image, valkeyClient))
|
||||||
|
|
||||||
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user