mirror of
https://codeberg.org/pluja/kycnot.me
synced 2025-01-22 12:21:11 -05:00
update img api
This commit is contained in:
parent
57929e761c
commit
4140d02f8c
@ -40,10 +40,8 @@ func (s *Server) handleVerifyPow(c iris.Context) {
|
|||||||
func (s *Server) handleApiPicture(c iris.Context) {
|
func (s *Server) handleApiPicture(c iris.Context) {
|
||||||
id := c.Params().Get("id")
|
id := c.Params().Get("id")
|
||||||
|
|
||||||
// Log and check s.Cache to make sure it is not nil
|
|
||||||
if s.Cache == nil {
|
if s.Cache == nil {
|
||||||
err := fmt.Errorf("server's Cache is nil")
|
log.Error().Msg("GetPicture: server's Cache is nil")
|
||||||
log.Error().Err(err).Msg("GetPicture: Fatal error")
|
|
||||||
respondWithPlaceholder(c, "?")
|
respondWithPlaceholder(c, "?")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -51,8 +49,10 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
|||||||
imgCacheKey := fmt.Sprintf("img-%s", id)
|
imgCacheKey := fmt.Sprintf("img-%s", id)
|
||||||
cttCacheKey := fmt.Sprintf("ctt-%s", id)
|
cttCacheKey := fmt.Sprintf("ctt-%s", id)
|
||||||
|
|
||||||
if imageData, err := s.Cache.Get(imgCacheKey); err == nil {
|
imageData, err := s.Cache.Get(imgCacheKey)
|
||||||
if ctt, err := s.Cache.Get(cttCacheKey); err == nil {
|
if err == nil {
|
||||||
|
ctt, err := s.Cache.Get(cttCacheKey)
|
||||||
|
if err == nil {
|
||||||
c.ContentType(string(ctt))
|
c.ContentType(string(ctt))
|
||||||
c.StatusCode(iris.StatusOK)
|
c.StatusCode(iris.StatusOK)
|
||||||
c.Write(imageData)
|
c.Write(imageData)
|
||||||
@ -60,16 +60,14 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log and check database.Pb to make sure it is not nil
|
|
||||||
if database.Pb == nil {
|
if database.Pb == nil {
|
||||||
err := fmt.Errorf("database.Pb is nil")
|
log.Error().Msg("GetPicture: database.Pb is nil")
|
||||||
log.Error().Err(err).Msg("GetPicture: Fatal error")
|
|
||||||
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 == "" { // Include service nil check
|
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 := "?"
|
name := "?"
|
||||||
if service != nil {
|
if service != nil {
|
||||||
@ -79,10 +77,8 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log and check s.HttpClient to make sure it is not nil
|
|
||||||
if s.HttpClient == nil {
|
if s.HttpClient == nil {
|
||||||
err := fmt.Errorf("server's HttpClient is nil")
|
log.Error().Msg("GetPicture: server's HttpClient is nil")
|
||||||
log.Error().Err(err).Msg("GetPicture: Fatal error")
|
|
||||||
respondWithPlaceholder(c, service.Name)
|
respondWithPlaceholder(c, service.Name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -96,7 +92,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 { // Include resp nil check
|
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, service.Name)
|
||||||
return
|
return
|
||||||
@ -112,10 +108,8 @@ func (s *Server) handleApiPicture(c iris.Context) {
|
|||||||
|
|
||||||
contentType := resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
|
||||||
// For other formats, use the original content type
|
|
||||||
c.ContentType(contentType)
|
c.ContentType(contentType)
|
||||||
s.Cache.Set(cttCacheKey, []byte(contentType))
|
s.Cache.Set(cttCacheKey, []byte(contentType))
|
||||||
|
|
||||||
s.Cache.Set(imgCacheKey, bodyBytes)
|
s.Cache.Set(imgCacheKey, bodyBytes)
|
||||||
c.StatusCode(iris.StatusOK)
|
c.StatusCode(iris.StatusOK)
|
||||||
c.Write(bodyBytes)
|
c.Write(bodyBytes)
|
||||||
@ -142,7 +136,7 @@ func respondWithPlaceholder(c iris.Context, serviceName string) {
|
|||||||
|
|
||||||
face := truetype.NewFace(f, &truetype.Options{Size: 180})
|
face := truetype.NewFace(f, &truetype.Options{Size: 180})
|
||||||
textWidth := font.MeasureString(face, firstLetter).Round()
|
textWidth := font.MeasureString(face, firstLetter).Round()
|
||||||
ascent, _ := face.Metrics().Ascent, face.Metrics().Descent
|
ascent := face.Metrics().Ascent
|
||||||
textHeight := ascent.Ceil()
|
textHeight := ascent.Ceil()
|
||||||
|
|
||||||
x := (width - textWidth) / 2
|
x := (width - textWidth) / 2
|
||||||
@ -152,7 +146,7 @@ func respondWithPlaceholder(c iris.Context, serviceName string) {
|
|||||||
ctx.DrawString(firstLetter, pt)
|
ctx.DrawString(firstLetter, pt)
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
png.Encode(&buf, img)
|
_ = png.Encode(&buf, img)
|
||||||
|
|
||||||
c.ContentType("image/png")
|
c.ContentType("image/png")
|
||||||
c.StatusCode(iris.StatusOK)
|
c.StatusCode(iris.StatusOK)
|
||||||
|
Loading…
Reference in New Issue
Block a user