From 947a853bae42f31a699e59ac41e31a4068ce521e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 28 Jan 2025 14:27:53 +0200 Subject: [PATCH] media: make thumbnail size configurable --- pkg/gomuks/config.go | 12 ++++++++++++ pkg/gomuks/media.go | 20 ++------------------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/pkg/gomuks/config.go b/pkg/gomuks/config.go index 3d51e00..76ab63a 100644 --- a/pkg/gomuks/config.go +++ b/pkg/gomuks/config.go @@ -35,6 +35,7 @@ type Config struct { Web WebConfig `yaml:"web"` Matrix MatrixConfig `yaml:"matrix"` Push PushConfig `yaml:"push"` + Media MediaConfig `yaml:"media"` Logging zeroconfig.Config `yaml:"logging"` } @@ -46,6 +47,10 @@ type PushConfig struct { FCMGateway string `yaml:"fcm_gateway"` } +type MediaConfig struct { + ThumbnailSize int `yaml:"thumbnail_size"` +} + type WebConfig struct { ListenAddress string `yaml:"listen_address"` Username string `yaml:"username"` @@ -74,6 +79,9 @@ func makeDefaultConfig() Config { Matrix: MatrixConfig{ DisableHTTP2: false, }, + Media: MediaConfig{ + ThumbnailSize: 120, + }, Logging: zeroconfig.Config{ MinLevel: ptr.Ptr(zerolog.DebugLevel), Writers: []zeroconfig.WriterConfig{{ @@ -130,6 +138,10 @@ func (gmx *Gomuks) LoadConfig() error { gmx.Config.Push.FCMGateway = "https://push.gomuks.app" changed = true } + if gmx.Config.Media.ThumbnailSize == 0 { + gmx.Config.Media.ThumbnailSize = 120 + changed = true + } if len(gmx.Config.Web.OriginPatterns) == 0 { gmx.Config.Web.OriginPatterns = []string{"localhost:*", "*.localhost:*"} changed = true diff --git a/pkg/gomuks/media.go b/pkg/gomuks/media.go index 79d922c..f2b040f 100644 --- a/pkg/gomuks/media.go +++ b/pkg/gomuks/media.go @@ -89,7 +89,7 @@ func (gmx *Gomuks) downloadMediaFromCache(ctx context.Context, w http.ResponseWr return true } if entry.ThumbnailHash == nil { - err := gmx.generateAvatarThumbnail(entry, thumbnailMaxSize) + err := gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize) if err != nil { log.Err(err).Msg("Failed to generate avatar thumbnail") w.WriteHeader(http.StatusInternalServerError) @@ -100,7 +100,7 @@ func (gmx *Gomuks) downloadMediaFromCache(ctx context.Context, w http.ResponseWr } cacheFile, err := os.Open(gmx.cacheEntryToPath(hash[:])) if useThumbnail && errors.Is(err, os.ErrNotExist) { - err = gmx.generateAvatarThumbnail(entry, thumbnailMaxSize) + err = gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize) if errors.Is(err, os.ErrNotExist) { // Fall through to next error handler } else if err != nil { @@ -151,8 +151,6 @@ func cacheEntryToHeaders(w http.ResponseWriter, entry *database.Media, thumbnail w.Header().Set("ETag", entry.ETag(thumbnail)) } -const thumbnailMaxSize = 80 - func (gmx *Gomuks) generateAvatarThumbnail(entry *database.Media, size int) error { cacheFile, err := os.Open(gmx.cacheEntryToPath(entry.Hash[:])) if err != nil { @@ -162,20 +160,6 @@ func (gmx *Gomuks) generateAvatarThumbnail(entry *database.Media, size int) erro if err != nil { return fmt.Errorf("failed to decode image: %w", err) } - //bounds := img.Bounds() - //origWidth := bounds.Dx() - //origHeight := bounds.Dy() - //var width, height int - //if origWidth == origHeight { - // width = size - // height = size - //} else if origWidth > origHeight { - // width = size - // height = origHeight * size / origWidth - //} else { - // width = origWidth * size / origHeight - // height = size - //} tempFile, err := os.CreateTemp(gmx.TempDir, "thumbnail-*") if err != nil {