media: make thumbnail size configurable

This commit is contained in:
Tulir Asokan 2025-01-28 14:27:53 +02:00
parent 6d12e6e009
commit 947a853bae
2 changed files with 14 additions and 18 deletions

View file

@ -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

View file

@ -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 {