mirror of
https://codeberg.org/pluja/kycnot.me
synced 2025-08-15 09:45:59 -04:00
use local placeholders
This commit is contained in:
parent
f14cfa0ba1
commit
0d4e23e477
5 changed files with 19 additions and 53 deletions
BIN
src/frontend/static/assets/placeholder_1.webp
Normal file
BIN
src/frontend/static/assets/placeholder_1.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
src/frontend/static/assets/placeholder_2.webp
Normal file
BIN
src/frontend/static/assets/placeholder_2.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
src/frontend/static/assets/placeholder_3.webp
Normal file
BIN
src/frontend/static/assets/placeholder_3.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
BIN
src/frontend/static/assets/placeholder_4.webp
Normal file
BIN
src/frontend/static/assets/placeholder_4.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
|
@ -1,22 +1,14 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"image/color"
|
|
||||||
"image/draw"
|
|
||||||
"image/png"
|
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/freetype"
|
|
||||||
"github.com/golang/freetype/truetype"
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"golang.org/x/image/font"
|
|
||||||
"golang.org/x/image/font/gofont/gomonobold"
|
|
||||||
|
|
||||||
"pluja.dev/kycnot.me/database"
|
"pluja.dev/kycnot.me/database"
|
||||||
)
|
)
|
||||||
|
@ -42,7 +34,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
|
|
||||||
if s.Cache == nil {
|
if s.Cache == nil {
|
||||||
log.Error().Msg("GetPicture: server's Cache is nil")
|
log.Error().Msg("GetPicture: server's Cache is nil")
|
||||||
respondWithPlaceholder(c, "?")
|
respondWithPlaceholder(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,24 +54,20 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
|
|
||||||
if database.Pb == nil {
|
if database.Pb == nil {
|
||||||
log.Error().Msg("GetPicture: database.Pb is nil")
|
log.Error().Msg("GetPicture: database.Pb is nil")
|
||||||
respondWithPlaceholder(c, "?")
|
respondWithPlaceholder(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := database.Pb.GetServiceById(id)
|
service, err := database.Pb.GetServiceById(id)
|
||||||
if err != nil || service == nil || service.LogoURL == "" {
|
if err != nil || service == nil || service.LogoURL == "" {
|
||||||
log.Error().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
|
log.Error().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
|
||||||
name := "?"
|
respondWithPlaceholder(c)
|
||||||
if service != nil {
|
|
||||||
name = service.Name
|
|
||||||
}
|
|
||||||
respondWithPlaceholder(c, name)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.HttpClient == nil {
|
if s.HttpClient == nil {
|
||||||
log.Error().Msg("GetPicture: server's HttpClient is nil")
|
log.Error().Msg("GetPicture: server's HttpClient is nil")
|
||||||
respondWithPlaceholder(c, service.Name)
|
respondWithPlaceholder(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +82,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
resp, err := s.HttpClient.Do(req)
|
resp, err := s.HttpClient.Do(req)
|
||||||
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
|
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
|
||||||
log.Debug().Err(err).Msgf("GetPicture: Could not get image for %s", service.Name)
|
log.Debug().Err(err).Msgf("GetPicture: Could not get image for %s", service.Name)
|
||||||
respondWithPlaceholder(c, service.Name)
|
respondWithPlaceholder(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
@ -102,7 +90,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
if len(bodyBytes) == 0 {
|
if len(bodyBytes) == 0 {
|
||||||
log.Debug().Msg("GetPicture: Empty response body")
|
log.Debug().Msg("GetPicture: Empty response body")
|
||||||
respondWithPlaceholder(c, service.Name)
|
respondWithPlaceholder(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,40 +103,18 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
c.Write(bodyBytes)
|
c.Write(bodyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondWithPlaceholder(c iris.Context, serviceName string) {
|
func respondWithPlaceholder(c iris.Context) {
|
||||||
if serviceName == "" {
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
serviceName = "?"
|
imageNumber := rng.Intn(4) + 1
|
||||||
}
|
imagePath := fmt.Sprintf("frontend/static/assets/placeholder_%d.webp", imageNumber)
|
||||||
firstLetter := strings.ToUpper(string(serviceName[0]))
|
|
||||||
const width, height, margin = 250, 250, 15
|
|
||||||
|
|
||||||
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
log.Debug().Msgf("Image path: %s", imagePath)
|
||||||
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src)
|
|
||||||
|
|
||||||
f, _ := truetype.Parse(gomonobold.TTF)
|
c.ContentType("image/webp")
|
||||||
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.StatusCode(iris.StatusOK)
|
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"})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue