use thumbhash instead of blurhash

This commit is contained in:
grngxd 2025-08-01 10:45:56 +01:00
parent 264739fd76
commit 997774c5de
4 changed files with 38 additions and 36 deletions

View file

@ -19,14 +19,18 @@ package routes
import (
"bytes"
"encoding/base64"
"io"
"strconv"
"strings"
"time"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/bbrks/go-blurhash"
"github.com/galdor/go-thumbhash"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/golang-jwt/jwt/v5"
@ -81,31 +85,35 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
}
fileType, err := filetype.Match(buf)
if err != nil {
types.ErrorReaderOpen.Throw(c, err)
return
}
img, _, err := image.Decode(bytes.NewReader(buf))
if err != nil {
types.ErrorInvalidFile.Throw(c, err)
return
}
hash, err := blurhash.Encode(4, 3, img)
if err != nil {
types.ErrorInvalidFile.Throw(c, err)
return
}
mime := fileType.MIME.Value
fileMeta := types.File{
Owner: uid,
Name: file.Filename,
CreatedAt: time.Now(),
Size: int64(len(buf)),
Mime: fileType.MIME.Value,
Blurhash: hash,
Mime: mime,
}
if filetype.IsImage(buf) {
img, _, err := image.Decode(bytes.NewReader(buf))
if err != nil {
types.ErrorInvalidFile.Throw(c, err)
return
}
hashBytes := thumbhash.EncodeImage(img)
// encode the byte[] into a string
hash := base64.StdEncoding.EncodeToString(hashBytes)
fileMeta.Hash = hash
fileMeta.Width = img.Bounds().Dx()
fileMeta.Height = img.Bounds().Dy()
}
if err := cfg.Database.Create(&fileMeta).Error; err != nil {

View file

@ -53,8 +53,10 @@ type File struct {
Owner string `gorm:"not null;index"`
Size int64 `gorm:"not null;type:bigint"`
CreatedAt time.Time `gorm:"autoCreateTime"`
Mime string ` gorm:"type:text"`
Blurhash string `gorm:"type:text"`
Mime string `gorm:"type:text"`
Hash string `gorm:"type:text"`
Width int
Height int
}
func (f *File) BeforeCreate(tx *gorm.DB) (err error) {