From 4140d02f8cf94723a8ae990752340dacbfe28653 Mon Sep 17 00:00:00 2001 From: pluja Date: Fri, 16 Aug 2024 22:08:17 +0200 Subject: [PATCH] update img api --- src/server/api_generic.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/server/api_generic.go b/src/server/api_generic.go index 1190f88..f5cb517 100644 --- a/src/server/api_generic.go +++ b/src/server/api_generic.go @@ -40,10 +40,8 @@ func (s *Server) handleVerifyPow(c iris.Context) { func (s *Server) handleApiPicture(c iris.Context) { id := c.Params().Get("id") - // Log and check s.Cache to make sure it is not nil if s.Cache == nil { - err := fmt.Errorf("server's Cache is nil") - log.Error().Err(err).Msg("GetPicture: Fatal error") + log.Error().Msg("GetPicture: server's Cache is nil") respondWithPlaceholder(c, "?") return } @@ -51,8 +49,10 @@ func (s *Server) handleApiPicture(c iris.Context) { imgCacheKey := fmt.Sprintf("img-%s", id) cttCacheKey := fmt.Sprintf("ctt-%s", id) - if imageData, err := s.Cache.Get(imgCacheKey); err == nil { - if ctt, err := s.Cache.Get(cttCacheKey); err == nil { + imageData, err := s.Cache.Get(imgCacheKey) + if err == nil { + ctt, err := s.Cache.Get(cttCacheKey) + if err == nil { c.ContentType(string(ctt)) c.StatusCode(iris.StatusOK) 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 { - err := fmt.Errorf("database.Pb is nil") - log.Error().Err(err).Msg("GetPicture: Fatal error") + log.Error().Msg("GetPicture: database.Pb is nil") respondWithPlaceholder(c, "?") return } 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") name := "?" if service != nil { @@ -79,10 +77,8 @@ func (s *Server) handleApiPicture(c iris.Context) { return } - // Log and check s.HttpClient to make sure it is not nil if s.HttpClient == nil { - err := fmt.Errorf("server's HttpClient is nil") - log.Error().Err(err).Msg("GetPicture: Fatal error") + log.Error().Msg("GetPicture: server's HttpClient is nil") respondWithPlaceholder(c, service.Name) return } @@ -96,7 +92,7 @@ func (s *Server) handleApiPicture(c iris.Context) { } 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) respondWithPlaceholder(c, service.Name) return @@ -112,10 +108,8 @@ func (s *Server) handleApiPicture(c iris.Context) { contentType := resp.Header.Get("Content-Type") - // For other formats, use the original content type c.ContentType(contentType) s.Cache.Set(cttCacheKey, []byte(contentType)) - s.Cache.Set(imgCacheKey, bodyBytes) c.StatusCode(iris.StatusOK) c.Write(bodyBytes) @@ -142,7 +136,7 @@ func respondWithPlaceholder(c iris.Context, serviceName string) { face := truetype.NewFace(f, &truetype.Options{Size: 180}) textWidth := font.MeasureString(face, firstLetter).Round() - ascent, _ := face.Metrics().Ascent, face.Metrics().Descent + ascent := face.Metrics().Ascent textHeight := ascent.Ceil() x := (width - textWidth) / 2 @@ -152,7 +146,7 @@ func respondWithPlaceholder(c iris.Context, serviceName string) { ctx.DrawString(firstLetter, pt) var buf bytes.Buffer - png.Encode(&buf, img) + _ = png.Encode(&buf, img) c.ContentType("image/png") c.StatusCode(iris.StatusOK)