mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package homepage
|
|
|
|
import (
|
|
"strings"
|
|
|
|
E "github.com/yusing/go-proxy/internal/error"
|
|
)
|
|
|
|
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/"
|
|
|
|
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
|
|
u.IconSource = IconSourceAbsolute
|
|
return nil
|
|
case "@target":
|
|
u.Value = v[slashIndex:]
|
|
u.IconSource = IconSourceRelative
|
|
return nil
|
|
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
|
|
}
|
|
}
|
|
|
|
func (u *IconURL) String() string {
|
|
return u.Value
|
|
}
|