mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
feat(favicon): add variant support for favicons
- Introduced a new Variant field in GetFavIconRequest to specify icon variants (light/dark). - Updated GetFavIconFromAlias function to handle the variant when fetching favicons. - Added WithVariant method in IconURL to manage icon variants effectively.
This commit is contained in:
@@ -13,8 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GetFavIconRequest struct {
|
type GetFavIconRequest struct {
|
||||||
URL string `form:"url" binding:"required_without=Alias"`
|
URL string `form:"url" binding:"required_without=Alias"`
|
||||||
Alias string `form:"alias" binding:"required_without=URL"`
|
Alias string `form:"alias" binding:"required_without=URL"`
|
||||||
|
Variant homepage.IconVariant `form:"variant" binding:"omitempty,oneof=light dark"`
|
||||||
} // @name GetFavIconRequest
|
} // @name GetFavIconRequest
|
||||||
|
|
||||||
// @x-id "favicon"
|
// @x-id "favicon"
|
||||||
@@ -56,7 +57,7 @@ func FavIcon(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// try with alias
|
// try with alias
|
||||||
result, err := GetFavIconFromAlias(c.Request.Context(), request.Alias)
|
result, err := GetFavIconFromAlias(c.Request.Context(), request.Alias, request.Variant)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
homepage.GinFetchError(c, result.StatusCode, err)
|
homepage.GinFetchError(c, result.StatusCode, err)
|
||||||
return
|
return
|
||||||
@@ -65,7 +66,7 @@ func FavIcon(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//go:linkname GetFavIconFromAlias v1.GetFavIconFromAlias
|
//go:linkname GetFavIconFromAlias v1.GetFavIconFromAlias
|
||||||
func GetFavIconFromAlias(ctx context.Context, alias string) (homepage.FetchResult, error) {
|
func GetFavIconFromAlias(ctx context.Context, alias string, variant homepage.IconVariant) (homepage.FetchResult, error) {
|
||||||
// try with route.Icon
|
// try with route.Icon
|
||||||
r, ok := routes.HTTP.Get(alias)
|
r, ok := routes.HTTP.Get(alias)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -81,7 +82,8 @@ func GetFavIconFromAlias(ctx context.Context, alias string) (homepage.FetchResul
|
|||||||
if hp.Icon.IconSource == homepage.IconSourceRelative {
|
if hp.Icon.IconSource == homepage.IconSourceRelative {
|
||||||
result, err = homepage.FindIcon(ctx, r, *hp.Icon.FullURL)
|
result, err = homepage.FindIcon(ctx, r, *hp.Icon.FullURL)
|
||||||
} else {
|
} else {
|
||||||
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon)
|
icon := hp.Icon.WithVariant(variant)
|
||||||
|
result, err = homepage.FetchFavIconFromURL(ctx, icon)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// try extract from "link[rel=icon]"
|
// try extract from "link[rel=icon]"
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ type (
|
|||||||
IsDark bool `json:"is_dark"`
|
IsDark bool `json:"is_dark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
IconSource string
|
IconSource string
|
||||||
|
IconVariant string
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -33,6 +34,12 @@ const (
|
|||||||
IconSourceSelfhSt IconSource = "@selfhst"
|
IconSourceSelfhSt IconSource = "@selfhst"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IconVariantNone IconVariant = ""
|
||||||
|
IconVariantLight IconVariant = "light"
|
||||||
|
IconVariantDark IconVariant = "dark"
|
||||||
|
)
|
||||||
|
|
||||||
var ErrInvalidIconURL = gperr.New("invalid icon url")
|
var ErrInvalidIconURL = gperr.New("invalid icon url")
|
||||||
|
|
||||||
func NewIconURL(source IconSource, refOrName, format string) *IconURL {
|
func NewIconURL(source IconSource, refOrName, format string) *IconURL {
|
||||||
@@ -76,6 +83,28 @@ func (u *IconURL) HasIcon() bool {
|
|||||||
return HasIcon(u)
|
return HasIcon(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *IconURL) WithVariant(variant IconVariant) *IconURL {
|
||||||
|
var extra *IconExtra
|
||||||
|
if u.Extra != nil {
|
||||||
|
extra = &IconExtra{
|
||||||
|
Key: u.Extra.Key,
|
||||||
|
Ref: u.Extra.Ref,
|
||||||
|
FileType: u.Extra.FileType,
|
||||||
|
}
|
||||||
|
switch variant {
|
||||||
|
case IconVariantLight:
|
||||||
|
extra.IsLight = true
|
||||||
|
case IconVariantDark:
|
||||||
|
extra.IsDark = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &IconURL{
|
||||||
|
IconSource: u.IconSource,
|
||||||
|
FullURL: u.FullURL,
|
||||||
|
Extra: extra,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse implements strutils.Parser.
|
// Parse implements strutils.Parser.
|
||||||
func (u *IconURL) Parse(v string) error {
|
func (u *IconURL) Parse(v string) error {
|
||||||
return u.parse(v, true)
|
return u.parse(v, true)
|
||||||
|
|||||||
Reference in New Issue
Block a user