github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/bitwarden/icons.go (about)

     1  package bitwarden
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/cozy/cozy-stack/model/bitwarden"
     7  	"github.com/cozy/cozy-stack/pkg/assets"
     8  	"github.com/cozy/cozy-stack/web/middlewares"
     9  	"github.com/cozy/cozy-stack/web/statik"
    10  	"github.com/labstack/echo/v4"
    11  )
    12  
    13  func serveDefaultIcon(c echo.Context) error {
    14  	inst := middlewares.GetInstance(c)
    15  	f, ok := assets.Get("/images/default-bitwarden-icon.png", inst.ContextName)
    16  	if !ok {
    17  		return echo.NewHTTPError(http.StatusNotFound, "Page not found")
    18  	}
    19  	handler := statik.NewHandler()
    20  	handler.ServeFile(c.Response(), c.Request(), f, true)
    21  	return nil
    22  }
    23  
    24  // GetIcon returns an icon for the given domain, to be used by the bitwarden
    25  // clients.
    26  func GetIcon(c echo.Context) error {
    27  	domain := c.Param("domain")
    28  	ico, err := bitwarden.GetIcon(domain)
    29  	if err == nil {
    30  		return c.Blob(http.StatusOK, ico.Mime, ico.Body)
    31  	}
    32  
    33  	inst := middlewares.GetInstance(c)
    34  	inst.Logger().WithNamespace("bitwarden").Debugf("Error for icon %s: %s", domain, err)
    35  	if c.QueryParam("fallback") == "404" {
    36  		return echo.NewHTTPError(http.StatusNotFound, "Page not found")
    37  	}
    38  	return serveDefaultIcon(c)
    39  }