mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-22 16:58:54 +02:00
updated ls-icon and icon fetching mechanism
This commit is contained in:
@@ -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
|
||||
|
||||
67
internal/homepage/icon_url_test.go
Normal file
67
internal/homepage/icon_url_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user