mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 09:48:32 +02:00
server side favicon retrieving and caching
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package homepage
|
||||
|
||||
import "strings"
|
||||
|
||||
type (
|
||||
//nolint:recvcheck
|
||||
Config map[string]Category
|
||||
@@ -8,7 +10,7 @@ type (
|
||||
Item struct {
|
||||
Show bool `json:"show"`
|
||||
Name string `json:"name"` // display name
|
||||
Icon string `json:"icon"`
|
||||
Icon *IconURL `json:"icon"`
|
||||
URL string `json:"url"` // alias + domain
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description" aliases:"desc"`
|
||||
@@ -22,7 +24,7 @@ type (
|
||||
|
||||
func (item *Item) IsEmpty() bool {
|
||||
return item == nil || (item.Name == "" &&
|
||||
item.Icon == "" &&
|
||||
item.Icon == nil &&
|
||||
item.URL == "" &&
|
||||
item.Category == "" &&
|
||||
item.Description == "" &&
|
||||
@@ -37,6 +39,13 @@ func (c *Config) Clear() {
|
||||
*c = make(Config)
|
||||
}
|
||||
|
||||
var cleanName = strings.NewReplacer(
|
||||
" ", "-",
|
||||
"_", "-",
|
||||
"(", "",
|
||||
")", "",
|
||||
)
|
||||
|
||||
func (c Config) Add(item *Item) {
|
||||
if c[item.Category] == nil {
|
||||
c[item.Category] = make(Category, 0)
|
||||
|
||||
46
internal/homepage/icon_url.go
Normal file
46
internal/homepage/icon_url.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package homepage
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
)
|
||||
|
||||
type IconURL struct {
|
||||
Value string `json:"value"`
|
||||
IsRelative bool `json:"is_relative"`
|
||||
}
|
||||
|
||||
const DashboardIconBaseURL = "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/"
|
||||
|
||||
var ErrInvalidIconURL = E.New("invalid icon url")
|
||||
|
||||
// Parse implements strutils.Parser.
|
||||
func (u *IconURL) Parse(v string) error {
|
||||
if v == "" {
|
||||
return ErrInvalidIconURL
|
||||
}
|
||||
slashIndex := strings.Index(v, "/")
|
||||
if slashIndex == -1 {
|
||||
return ErrInvalidIconURL
|
||||
}
|
||||
beforeSlash := v[:slashIndex]
|
||||
switch beforeSlash {
|
||||
case "http:", "https:":
|
||||
u.Value = v
|
||||
return nil
|
||||
case "@target":
|
||||
u.Value = v[slashIndex:]
|
||||
u.IsRelative = true
|
||||
return nil
|
||||
case "png", "svg": // walkXCode Icons
|
||||
u.Value = DashboardIconBaseURL + v
|
||||
return nil
|
||||
default:
|
||||
return ErrInvalidIconURL
|
||||
}
|
||||
}
|
||||
|
||||
func (u *IconURL) String() string {
|
||||
return u.Value
|
||||
}
|
||||
Reference in New Issue
Block a user