
Fix lint Add docs for keyvalue config options Use keyvalue store to cache unsplash photo results Cleanup Use keyvalue store for upload avatar Use keyvalue store for initials avatar Fix initializing metrics Use keyvalue for metrics Add IncryBy and DecrBy methods to increase or decrease a value Fix lint Return custom error if a key does not exist Init keyvalue storage Follow the keyvalue storage setting for things like cache and other Add docs Add configuration of the storage backend Add redis keyvalue storage implementation Add doc comments Add methods to use storage through the package itself Add memory implementation for keyvalue store Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/674 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
189 lines
4.8 KiB
Go
189 lines
4.8 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package initials
|
|
|
|
import (
|
|
"bytes"
|
|
"code.vikunja.io/api/pkg/log"
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
|
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
|
"code.vikunja.io/api/pkg/user"
|
|
"github.com/disintegration/imaging"
|
|
"github.com/golang/freetype/truetype"
|
|
"golang.org/x/image/font"
|
|
"golang.org/x/image/font/gofont/goregular"
|
|
"golang.org/x/image/math/fixed"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/png"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Provider represents the provider implementation of the initials provider
|
|
type Provider struct {
|
|
}
|
|
|
|
var (
|
|
avatarBgColors = []*color.RGBA{
|
|
{69, 189, 243, 255},
|
|
{224, 143, 112, 255},
|
|
{77, 182, 172, 255},
|
|
{149, 117, 205, 255},
|
|
{176, 133, 94, 255},
|
|
{240, 98, 146, 255},
|
|
{163, 211, 108, 255},
|
|
{121, 134, 203, 255},
|
|
{241, 185, 29, 255},
|
|
}
|
|
)
|
|
|
|
const (
|
|
dpi = 72
|
|
defaultSize = 1024
|
|
)
|
|
|
|
func drawImage(text rune, bg *color.RGBA) (img *image.RGBA64, err error) {
|
|
|
|
size := defaultSize
|
|
fontSize := float64(size) * 0.8
|
|
|
|
// Inspired by https://github.com/holys/initials-avatar
|
|
|
|
// Get the font
|
|
f, err := truetype.Parse(goregular.TTF)
|
|
if err != nil {
|
|
return img, err
|
|
}
|
|
|
|
// Build the image background
|
|
img = image.NewRGBA64(image.Rect(0, 0, size, size))
|
|
draw.Draw(img, img.Bounds(), &image.Uniform{C: bg}, image.Point{}, draw.Src)
|
|
|
|
// Add the text
|
|
drawer := &font.Drawer{
|
|
Dst: img,
|
|
Src: image.White,
|
|
Face: truetype.NewFace(f, &truetype.Options{
|
|
Size: fontSize,
|
|
DPI: dpi,
|
|
Hinting: font.HintingNone,
|
|
}),
|
|
}
|
|
|
|
// Font Index
|
|
fi := f.Index(text)
|
|
|
|
// Glyph example: http://www.freetype.org/freetype2/docs/tutorial/metrics.png
|
|
var gbuf truetype.GlyphBuf
|
|
fsize := fixed.Int26_6(fontSize * dpi * (64.0 / 72.0))
|
|
err = gbuf.Load(f, fsize, fi, font.HintingFull)
|
|
if err != nil {
|
|
drawer.DrawString("")
|
|
return img, err
|
|
}
|
|
|
|
// Center
|
|
dY := (size - int(gbuf.Bounds.Max.Y-gbuf.Bounds.Min.Y)>>6) / 2
|
|
dX := (size - int(gbuf.Bounds.Max.X-gbuf.Bounds.Min.X)>>6) / 2
|
|
y := int(gbuf.Bounds.Max.Y>>6) + dY
|
|
x := 0 - int(gbuf.Bounds.Min.X>>6) + dX
|
|
|
|
drawer.Dot = fixed.Point26_6{
|
|
X: fixed.I(x),
|
|
Y: fixed.I(y),
|
|
}
|
|
drawer.DrawString(string(text))
|
|
|
|
return img, err
|
|
}
|
|
|
|
func getCacheKey(prefix string, keys ...int64) string {
|
|
result := "avatar_initials_" + prefix
|
|
for i, key := range keys {
|
|
result += strconv.Itoa(int(key))
|
|
if i < len(keys) {
|
|
result += "_"
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func getAvatarForUser(u *user.User) (fullSizeAvatar *image.RGBA64, err error) {
|
|
cacheKey := getCacheKey("full", u.ID)
|
|
|
|
a, err := keyvalue.Get(cacheKey)
|
|
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
|
return nil, err
|
|
}
|
|
|
|
if err != nil && e.IsErrValueNotFoundForKey(err) {
|
|
log.Debugf("Initials avatar for user %d not cached, creating...", u.ID)
|
|
firstRune := []rune(strings.ToUpper(u.Username))[0]
|
|
bg := avatarBgColors[int(u.ID)%len(avatarBgColors)] // Random color based on the user id
|
|
|
|
fullSizeAvatar, err = drawImage(firstRune, bg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = keyvalue.Put(cacheKey, fullSizeAvatar)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
fullSizeAvatar = a.(*image.RGBA64)
|
|
}
|
|
|
|
return fullSizeAvatar, nil
|
|
}
|
|
|
|
// GetAvatar returns an initials avatar for a user
|
|
func (p *Provider) GetAvatar(u *user.User, size int64) (avatar []byte, mimeType string, err error) {
|
|
cacheKey := getCacheKey("resized", u.ID, size)
|
|
|
|
a, err := keyvalue.Get(cacheKey)
|
|
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
|
return nil, "", err
|
|
}
|
|
|
|
if err != nil && e.IsErrValueNotFoundForKey(err) {
|
|
log.Debugf("Initials avatar for user %d and size %d not cached, creating...", u.ID, size)
|
|
fullAvatar, err := getAvatarForUser(u)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
img := imaging.Resize(fullAvatar, int(size), int(size), imaging.Lanczos)
|
|
buf := &bytes.Buffer{}
|
|
err = png.Encode(buf, img)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
avatar = buf.Bytes()
|
|
err = keyvalue.Put(cacheKey, avatar)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
} else {
|
|
avatar = a.([]byte)
|
|
log.Debugf("Serving initials avatar for user %d and size %d from cache", u.ID, size)
|
|
}
|
|
|
|
return avatar, "image/png", nil
|
|
}
|