mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
feat: improved icon and category lookup mechanism
This commit is contained in:
parent
5fce4b445b
commit
8ef8015a7f
4 changed files with 45 additions and 29 deletions
|
@ -114,12 +114,18 @@ func fetchKnownIcon(ctx context.Context, url *IconURL) *FetchResult {
|
|||
return fetchIconAbsolute(ctx, url.URL())
|
||||
}
|
||||
|
||||
func fetchIcon(ctx context.Context, filetype, filename string) *FetchResult {
|
||||
result := fetchKnownIcon(ctx, NewSelfhStIconURL(filename, filetype))
|
||||
if result.OK() {
|
||||
return result
|
||||
func fetchIcon(ctx context.Context, filename string) *FetchResult {
|
||||
for _, fileType := range []string{"svg", "webp", "png"} {
|
||||
result := fetchKnownIcon(ctx, NewSelfhStIconURL(filename, fileType))
|
||||
if result.OK() {
|
||||
return result
|
||||
}
|
||||
result = fetchKnownIcon(ctx, NewWalkXCodeIconURL(filename, fileType))
|
||||
if result.OK() {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return fetchKnownIcon(ctx, NewWalkXCodeIconURL(filename, filetype))
|
||||
return &FetchResult{StatusCode: http.StatusNotFound, ErrMsg: "no icon found"}
|
||||
}
|
||||
|
||||
func FindIcon(ctx context.Context, r route, uri string) *FetchResult {
|
||||
|
@ -127,17 +133,18 @@ func FindIcon(ctx context.Context, r route, uri string) *FetchResult {
|
|||
return result
|
||||
}
|
||||
|
||||
result := fetchIcon(ctx, "png", sanitizeName(r.Reference()))
|
||||
if !result.OK() {
|
||||
if r, ok := r.(httpRoute); ok {
|
||||
// fallback to parse html
|
||||
result = findIconSlow(ctx, r, uri, nil)
|
||||
for _, ref := range r.References() {
|
||||
result := fetchIcon(ctx, sanitizeName(ref))
|
||||
if result.OK() {
|
||||
storeIconCache(r.Key(), result)
|
||||
return result
|
||||
}
|
||||
}
|
||||
if result.OK() {
|
||||
storeIconCache(r.Key(), result)
|
||||
if r, ok := r.(httpRoute); ok {
|
||||
// fallback to parse html
|
||||
return findIconSlow(ctx, r, uri, nil)
|
||||
}
|
||||
return result
|
||||
return &FetchResult{StatusCode: http.StatusNotFound, ErrMsg: "no icon found"}
|
||||
}
|
||||
|
||||
func findIconSlow(ctx context.Context, r httpRoute, uri string, stack []string) *FetchResult {
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
type route interface {
|
||||
pool.Object
|
||||
ProviderName() string
|
||||
Reference() string
|
||||
References() []string
|
||||
TargetURL() *gpnet.URL
|
||||
}
|
||||
|
||||
|
|
|
@ -243,11 +243,14 @@ func (r *Route) TargetURL() *net.URL {
|
|||
return r.ProxyURL
|
||||
}
|
||||
|
||||
func (r *Route) Reference() string {
|
||||
func (r *Route) References() []string {
|
||||
if r.Container != nil {
|
||||
return r.Container.Image.Name
|
||||
if r.Container.ContainerName != r.Alias {
|
||||
return []string{r.Container.Image.Name, r.Container.ContainerName, r.Alias, r.Container.Image.Author}
|
||||
}
|
||||
return []string{r.Container.Image.Name, r.Alias, r.Container.Image.Author}
|
||||
}
|
||||
return r.Alias
|
||||
return []string{r.Alias}
|
||||
}
|
||||
|
||||
// Name implements pool.Object.
|
||||
|
@ -476,21 +479,24 @@ func (r *Route) FinalizeHomepageConfig() {
|
|||
r.Homepage = r.Homepage.GetOverride(r.Alias)
|
||||
|
||||
hp := r.Homepage
|
||||
ref := r.Reference()
|
||||
meta, ok := homepage.GetHomepageMeta(ref)
|
||||
if ok {
|
||||
if hp.Name == "" {
|
||||
hp.Name = meta.DisplayName
|
||||
}
|
||||
if hp.Category == "" {
|
||||
hp.Category = meta.Tag
|
||||
refs := r.References()
|
||||
for _, ref := range refs {
|
||||
meta, ok := homepage.GetHomepageMeta(ref)
|
||||
if ok {
|
||||
if hp.Name == "" {
|
||||
hp.Name = meta.DisplayName
|
||||
}
|
||||
if hp.Category == "" {
|
||||
hp.Category = meta.Tag
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if hp.Name == "" {
|
||||
hp.Name = strutils.Title(
|
||||
strings.ReplaceAll(
|
||||
strings.ReplaceAll(ref, "-", " "),
|
||||
strings.ReplaceAll(refs[0], "-", " "),
|
||||
"_", " ",
|
||||
),
|
||||
)
|
||||
|
@ -498,8 +504,11 @@ func (r *Route) FinalizeHomepageConfig() {
|
|||
|
||||
if hp.Category == "" {
|
||||
if config.GetInstance().Value().Homepage.UseDefaultCategories {
|
||||
if category, ok := homepage.PredefinedCategories[ref]; ok {
|
||||
hp.Category = category
|
||||
for _, ref := range refs {
|
||||
if category, ok := homepage.PredefinedCategories[ref]; ok {
|
||||
hp.Category = category
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ type (
|
|||
ProviderName() string
|
||||
TargetURL() *net.URL
|
||||
HealthMonitor() health.HealthMonitor
|
||||
Reference() string
|
||||
References() []string
|
||||
|
||||
Started() bool
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue