fix: decoding images for blurHash generation

This commit is contained in:
kolaente 2021-12-20 19:37:39 +01:00 committed by Gitea
parent e19ad11846
commit d3bdafb717
2 changed files with 36 additions and 39 deletions

View file

@ -18,27 +18,30 @@ package handler
import ( import (
"image" "image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"github.com/bbrks/go-blurhash"
"golang.org/x/image/draw"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"xorm.io/xorm"
"code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/models"
auth2 "code.vikunja.io/api/pkg/modules/auth" auth2 "code.vikunja.io/api/pkg/modules/auth"
"code.vikunja.io/api/pkg/modules/background" "code.vikunja.io/api/pkg/modules/background"
"code.vikunja.io/api/pkg/modules/background/unsplash" "code.vikunja.io/api/pkg/modules/background/unsplash"
"code.vikunja.io/api/pkg/modules/background/upload"
"code.vikunja.io/web" "code.vikunja.io/web"
"code.vikunja.io/web/handler" "code.vikunja.io/web/handler"
"github.com/bbrks/go-blurhash"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"golang.org/x/image/draw"
"xorm.io/xorm"
) )
// BackgroundProvider represents a thing which holds a background provider // BackgroundProvider represents a thing which holds a background provider
@ -161,8 +164,6 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
return handler.HandleHTTPError(err, c) return handler.HandleHTTPError(err, c)
} }
p := bp.Provider()
// Get + upload the image // Get + upload the image
file, err := c.FormFile("background") file, err := c.FormFile("background")
if err != nil { if err != nil {
@ -186,10 +187,8 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
_ = s.Rollback() _ = s.Rollback()
return c.JSON(http.StatusBadRequest, models.Message{Message: "Uploaded file is no image."}) return c.JSON(http.StatusBadRequest, models.Message{Message: "Uploaded file is no image."})
} }
_, _ = srcf.Seek(0, io.SeekStart)
// Save the file err = SaveBackgroundFile(s, auth, list, srcf, file.Filename, uint64(file.Size))
f, err := files.CreateWithMime(srcf, file.Filename, uint64(file.Size), auth, mime.String())
if err != nil { if err != nil {
_ = s.Rollback() _ = s.Rollback()
if files.IsErrFileIsTooLarge(err) { if files.IsErrFileIsTooLarge(err) {
@ -199,21 +198,6 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
return handler.HandleHTTPError(err, c) return handler.HandleHTTPError(err, c)
} }
// Generate a blurHash
_, _ = srcf.Seek(0, io.SeekStart)
list.BackgroundBlurHash, err = CreateBlurHash(srcf)
if err != nil {
return handler.HandleHTTPError(err, c)
}
// Save it
img := &background.Image{ID: strconv.FormatInt(f.ID, 10)}
err = p.Set(s, img, list, auth)
if err != nil {
_ = s.Rollback()
return handler.HandleHTTPError(err, c)
}
if err := s.Commit(); err != nil { if err := s.Commit(); err != nil {
_ = s.Rollback() _ = s.Rollback()
return handler.HandleHTTPError(err, c) return handler.HandleHTTPError(err, c)
@ -222,6 +206,27 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
return c.JSON(http.StatusOK, list) return c.JSON(http.StatusOK, list)
} }
func SaveBackgroundFile(s *xorm.Session, auth web.Auth, list *models.List, srcf io.ReadSeeker, filename string, filesize uint64) (err error) {
_, _ = srcf.Seek(0, io.SeekStart)
f, err := files.Create(srcf, filename, filesize, auth)
if err != nil {
return err
}
// Generate a blurHash
_, _ = srcf.Seek(0, io.SeekStart)
list.BackgroundBlurHash, err = CreateBlurHash(srcf)
if err != nil {
return err
}
// Save it
p := upload.Provider{}
img := &background.Image{ID: strconv.FormatInt(f.ID, 10)}
err = p.Set(s, img, list, auth)
return err
}
func checkListBackgroundRights(s *xorm.Session, c echo.Context) (list *models.List, auth web.Auth, err error) { func checkListBackgroundRights(s *xorm.Session, c echo.Context) (list *models.List, auth web.Auth, err error) {
auth, err = auth2.GetAuthFromClaims(c) auth, err = auth2.GetAuthFromClaims(c)
if err != nil { if err != nil {

View file

@ -25,7 +25,6 @@ import (
"xorm.io/xorm" "xorm.io/xorm"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/user"
@ -108,26 +107,19 @@ func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTas
log.Debugf("[creating structure] Created list %d", l.ID) log.Debugf("[creating structure] Created list %d", l.ID)
backgroundFile, is := originalBackgroundInformation.(*bytes.Buffer) bf, is := originalBackgroundInformation.(*bytes.Buffer)
if is { if is {
backgroundFile := bytes.NewReader(bf.Bytes())
log.Debugf("[creating structure] Creating a background file for list %d", l.ID) log.Debugf("[creating structure] Creating a background file for list %d", l.ID)
file, err := files.Create(backgroundFile, "", uint64(backgroundFile.Len()), user) err = handler.SaveBackgroundFile(s, user, &l.List, backgroundFile, "", uint64(backgroundFile.Len()))
if err != nil { if err != nil {
return err return err
} }
hash, err := handler.CreateBlurHash(backgroundFile) log.Debugf("[creating structure] Created a background file for list %d", l.ID)
if err != nil {
return err
}
err = models.SetListBackground(s, l.ID, file, hash)
if err != nil {
return err
}
log.Debugf("[creating structure] Created a background file as new file %d for list %d", file.ID, l.ID)
} }
// Create all buckets // Create all buckets