add blurhash

This commit is contained in:
grngxd 2025-08-01 08:43:25 +01:00
parent bc42a2bb75
commit 264739fd76
4 changed files with 22 additions and 1 deletions

1
go.mod
View file

@ -9,6 +9,7 @@ require (
) )
require ( require (
github.com/bbrks/go-blurhash v1.1.1 // indirect
github.com/bytedance/sonic v1.13.2 // indirect github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect github.com/cloudwego/base64x v0.1.5 // indirect

3
go.sum
View file

@ -1,3 +1,5 @@
github.com/bbrks/go-blurhash v1.1.1 h1:uoXOxRPDca9zHYabUTwvS4KnY++KKUbwFo+Yxb8ME4M=
github.com/bbrks/go-blurhash v1.1.1/go.mod h1:lkAsdyXp+EhARcUo85yS2G1o+Sh43I2ebF5togC4bAY=
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ= github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@ -77,6 +79,7 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkIcHO0h8c= github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkIcHO0h8c=
github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y= github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=

View file

@ -24,6 +24,9 @@ import (
"strings" "strings"
"time" "time"
"image"
"github.com/bbrks/go-blurhash"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
@ -84,12 +87,25 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
return 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
}
fileMeta := types.File{ fileMeta := types.File{
Owner: uid, Owner: uid,
Name: file.Filename, Name: file.Filename,
CreatedAt: time.Now(), CreatedAt: time.Now(),
Size: int64(len(buf)), Size: int64(len(buf)),
Mime: fileType.MIME.Value, Mime: fileType.MIME.Value,
Blurhash: hash,
} }
if err := cfg.Database.Create(&fileMeta).Error; err != nil { if err := cfg.Database.Create(&fileMeta).Error; err != nil {

View file

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