use local placeholders

This commit is contained in:
pluja 2024-10-21 23:16:16 +02:00
parent f14cfa0ba1
commit 0d4e23e477
No known key found for this signature in database
5 changed files with 19 additions and 53 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -1,22 +1,14 @@
package server
import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"math/rand"
"net/http"
"strings"
"time"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/kataras/iris/v12"
"github.com/rs/zerolog/log"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/gomonobold"
"pluja.dev/kycnot.me/database"
)
@ -42,7 +34,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
if s.Cache == nil {
log.Error().Msg("GetPicture: server's Cache is nil")
respondWithPlaceholder(c, "?")
respondWithPlaceholder(c)
return
}
@ -62,24 +54,20 @@ func (s *Server) handleApiPicture(c iris.Context) {
if database.Pb == nil {
log.Error().Msg("GetPicture: database.Pb is nil")
respondWithPlaceholder(c, "?")
respondWithPlaceholder(c)
return
}
service, err := database.Pb.GetServiceById(id)
if err != nil || service == nil || service.LogoURL == "" {
log.Error().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
name := "?"
if service != nil {
name = service.Name
}
respondWithPlaceholder(c, name)
respondWithPlaceholder(c)
return
}
if s.HttpClient == nil {
log.Error().Msg("GetPicture: server's HttpClient is nil")
respondWithPlaceholder(c, service.Name)
respondWithPlaceholder(c)
return
}
@ -94,7 +82,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
resp, err := s.HttpClient.Do(req)
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
log.Debug().Err(err).Msgf("GetPicture: Could not get image for %s", service.Name)
respondWithPlaceholder(c, service.Name)
respondWithPlaceholder(c)
return
}
defer resp.Body.Close()
@ -102,7 +90,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
bodyBytes, _ := io.ReadAll(resp.Body)
if len(bodyBytes) == 0 {
log.Debug().Msg("GetPicture: Empty response body")
respondWithPlaceholder(c, service.Name)
respondWithPlaceholder(c)
return
}
@ -115,40 +103,18 @@ func (s *Server) handleApiPicture(c iris.Context) {
c.Write(bodyBytes)
}
func respondWithPlaceholder(c iris.Context, serviceName string) {
if serviceName == "" {
serviceName = "?"
}
firstLetter := strings.ToUpper(string(serviceName[0]))
const width, height, margin = 250, 250, 15
func respondWithPlaceholder(c iris.Context) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
imageNumber := rng.Intn(4) + 1
imagePath := fmt.Sprintf("frontend/static/assets/placeholder_%d.webp", imageNumber)
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src)
log.Debug().Msgf("Image path: %s", imagePath)
f, _ := truetype.Parse(gomonobold.TTF)
ctx := freetype.NewContext()
ctx.SetDPI(72)
ctx.SetFont(f)
ctx.SetFontSize(180)
ctx.SetSrc(image.NewUniform(color.RGBA{0x84, 0xcc, 0x16, 0xff}))
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
face := truetype.NewFace(f, &truetype.Options{Size: 180})
textWidth := font.MeasureString(face, firstLetter).Round()
ascent := face.Metrics().Ascent
textHeight := ascent.Ceil()
x := (width - textWidth) / 2
y := (height-textHeight)/2 + textHeight
pt := freetype.Pt(x, y)
ctx.DrawString(firstLetter, pt)
var buf bytes.Buffer
_ = png.Encode(&buf, img)
c.ContentType("image/png")
c.ContentType("image/webp")
c.StatusCode(iris.StatusOK)
c.Write(buf.Bytes())
err := c.SendFile(imagePath, "placeholder.webp")
if err != nil {
c.StatusCode(iris.StatusInternalServerError)
c.JSON(iris.Map{"error": "Failed to send image"})
}
}