updated ls-icon and icon fetching mechanism

This commit is contained in:
yusing
2025-01-13 02:21:52 +08:00
parent d887a37f60
commit e10e6cfe4d
5 changed files with 192 additions and 74 deletions

View File

@@ -6,10 +6,26 @@ import (
E "github.com/yusing/go-proxy/internal/error"
)
type IconURL struct {
Value string `json:"value"`
IsRelative bool `json:"is_relative"`
}
type (
IconURL struct {
Value string `json:"value"`
IconSource
Extra *IconExtra `json:"extra"`
}
IconExtra struct {
FileType string `json:"file_type"`
Name string `json:"name"`
}
IconSource int
)
const (
IconSourceAbsolute IconSource = iota
IconSourceRelative
IconSourceWalkXCode
)
const DashboardIconBaseURL = "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/"
@@ -28,13 +44,19 @@ func (u *IconURL) Parse(v string) error {
switch beforeSlash {
case "http:", "https:":
u.Value = v
u.IconSource = IconSourceAbsolute
return nil
case "@target":
u.Value = v[slashIndex:]
u.IsRelative = true
u.IconSource = IconSourceRelative
return nil
case "png", "svg": // walkXCode Icons
u.Value = DashboardIconBaseURL + v
case "png", "svg", "webp": // walkXCode Icons
u.Value = v
u.IconSource = IconSourceWalkXCode
u.Extra = &IconExtra{
FileType: beforeSlash,
Name: strings.TrimSuffix(v[slashIndex+1:], "."+beforeSlash),
}
return nil
default:
return ErrInvalidIconURL

View File

@@ -0,0 +1,67 @@
package homepage
import (
"testing"
. "github.com/yusing/go-proxy/internal/utils/testing"
)
func TestIconURL(t *testing.T) {
tests := []struct {
name string
input string
wantValue *IconURL
wantErr bool
}{
{
name: "absolute",
input: "http://example.com/icon.png",
wantValue: &IconURL{
Value: "http://example.com/icon.png",
IconSource: IconSourceAbsolute,
},
},
{
name: "relative",
input: "@target/icon.png",
wantValue: &IconURL{
Value: "/icon.png",
IconSource: IconSourceRelative,
},
},
{
name: "walkxcode",
input: "png/walkxcode.png",
wantValue: &IconURL{
Value: "png/walkxcode.png",
IconSource: IconSourceWalkXCode,
Extra: &IconExtra{
FileType: "png",
Name: "walkxcode",
},
},
},
{
name: "invalid",
input: "invalid",
wantErr: true,
},
{
name: "empty",
input: "",
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
u := &IconURL{}
err := u.Parse(tc.input)
if tc.wantErr {
ExpectError(t, ErrInvalidIconURL, err)
} else {
ExpectNoError(t, err)
ExpectDeepEqual(t, u, tc.wantValue)
}
})
}
}