diff --git a/internal/services/assets/handler.go b/internal/services/assets/handler.go index c0af74a0a..0ee2021db 100644 --- a/internal/services/assets/handler.go +++ b/internal/services/assets/handler.go @@ -32,6 +32,14 @@ func (s *Server) handleGetData(t AssetType) http.HandlerFunc { return } + // check if it's a recognized item; if not then we can skip the rest + assetName, ok := s.assetNameMapping[t][key] + if !ok { + s.logger.Info("unrecognized key, serving default", "t", t.String(), "key", key) + s.handleNotFound(w) + return + } + // try cache first data, err := s.loadFromCache(t, key) switch { @@ -47,25 +55,23 @@ func (s *Server) handleGetData(t AssetType) http.HandlerFunc { s.logger.Info("cache data not found", "t", t.String(), "key", key) } - if assetName, ok := s.assetNameMapping[t][key]; ok { - hosts := s.hosts[t] - // else try external 1 at a time - for i, v := range hosts { - joinedURL := v.JoinPath(fmt.Sprintf("/%v.png", assetName)) - s.logger.Info("trying external image source", "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String()) - data, err := s.proxyImageRequest(joinedURL) - if err != nil { - s.logger.Info("error getting", "err", err, "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String()) - continue - } - s.logger.Info("received image from source ok", "host", v.String()) - s.saveToCache(t, key, data) - // found ok, save to cache and end request - w.WriteHeader(http.StatusOK) - w.Write(data) - return + // else try external 1 at a time + for i, v := range s.hosts[t] { + joinedURL := v.JoinPath(fmt.Sprintf("/%v.png", assetName)) + s.logger.Info("trying external image source", "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String()) + data, err := s.proxyImageRequest(joinedURL) + if err != nil { + s.logger.Info("error getting", "err", err, "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String()) + continue } + s.logger.Info("received image from source ok", "host", v.String()) + s.saveToCache(t, key, data) + // found ok, save to cache and end request + w.WriteHeader(http.StatusOK) + w.Write(data) + return } + s.logger.Info("no external source found, serving default", "t", t.String(), "key", key) // if we reached here then all external failed so we should serve default question mark image s.handleNotFound(w) }