mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:08:31 +02:00
refactor(favicon): enhance FindIcon function to support icon variants
- Updated FindIcon to accept an additional variant parameter for improved icon fetching. - Adjusted FavIcon and GetFavIconFromAlias functions to utilize the new variant handling logic.
This commit is contained in:
@@ -47,7 +47,11 @@ func FavIcon(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, apitypes.Error("invalid url", err))
|
c.JSON(http.StatusBadRequest, apitypes.Error("invalid url", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fetchResult, err := homepage.FetchFavIconFromURL(c.Request.Context(), &iconURL)
|
icon := &iconURL
|
||||||
|
if request.Variant != homepage.IconVariantNone {
|
||||||
|
icon = icon.WithVariant(request.Variant)
|
||||||
|
}
|
||||||
|
fetchResult, err := homepage.FetchFavIconFromURL(c.Request.Context(), icon)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
homepage.GinFetchError(c, fetchResult.StatusCode, err)
|
homepage.GinFetchError(c, fetchResult.StatusCode, err)
|
||||||
return
|
return
|
||||||
@@ -80,7 +84,7 @@ func GetFavIconFromAlias(ctx context.Context, alias string, variant homepage.Ico
|
|||||||
hp := r.HomepageItem()
|
hp := r.HomepageItem()
|
||||||
if hp.Icon != nil {
|
if hp.Icon != nil {
|
||||||
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, variant)
|
||||||
} else if variant != homepage.IconVariantNone {
|
} else if variant != homepage.IconVariantNone {
|
||||||
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon.WithVariant(variant))
|
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon.WithVariant(variant))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -92,7 +96,7 @@ func GetFavIconFromAlias(ctx context.Context, alias string, variant homepage.Ico
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// try extract from "link[rel=icon]"
|
// try extract from "link[rel=icon]"
|
||||||
result, err = homepage.FindIcon(ctx, r, "/")
|
result, err = homepage.FindIcon(ctx, r, "/", variant)
|
||||||
}
|
}
|
||||||
if result.StatusCode == 0 {
|
if result.StatusCode == 0 {
|
||||||
result.StatusCode = http.StatusOK
|
result.StatusCode = http.StatusOK
|
||||||
|
|||||||
@@ -150,9 +150,13 @@ type contextValue struct {
|
|||||||
uri string
|
uri string
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindIcon(ctx context.Context, r route, uri string) (FetchResult, error) {
|
func FindIcon(ctx context.Context, r route, uri string, variant IconVariant) (FetchResult, error) {
|
||||||
for _, ref := range r.References() {
|
for _, ref := range r.References() {
|
||||||
result, err := fetchIcon(ctx, sanitizeName(ref))
|
ref = sanitizeName(ref)
|
||||||
|
if variant != IconVariantNone {
|
||||||
|
ref += "-" + string(variant)
|
||||||
|
}
|
||||||
|
result, err := fetchIcon(ctx, ref)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,13 +104,13 @@ func (w *Watcher) getFavIcon(ctx context.Context) (result homepage.FetchResult,
|
|||||||
hp := r.HomepageItem()
|
hp := r.HomepageItem()
|
||||||
if hp.Icon != nil {
|
if hp.Icon != nil {
|
||||||
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, homepage.IconVariantNone)
|
||||||
} else {
|
} else {
|
||||||
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon)
|
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// try extract from "link[rel=icon]"
|
// try extract from "link[rel=icon]"
|
||||||
result, err = homepage.FindIcon(ctx, r, "/")
|
result, err = homepage.FindIcon(ctx, r, "/", homepage.IconVariantNone)
|
||||||
}
|
}
|
||||||
if result.StatusCode == 0 {
|
if result.StatusCode == 0 {
|
||||||
result.StatusCode = http.StatusOK
|
result.StatusCode = http.StatusOK
|
||||||
|
|||||||
Reference in New Issue
Block a user