Files
web_hghl_server/pub/common/webpath/paths.go
T
2026-10-26 16:28:14 +08:00

106 lines
2.2 KiB
Go

package webpath
import (
"net/url"
"strings"
)
// LegacyPathRemap 旧版 app 导航路径 → 当前 h5 SSR 实际路由。
var LegacyPathRemap = map[string]string{
"/avtopic/": "/video/all",
"/avtopic": "/video/all",
"/dh/": "/nav",
"/dh": "/nav",
}
// SkipPublicPaths 当前 web 无对应页面、或不应对外暴露的路径。
var SkipPublicPaths = map[string]struct{}{
"/topic/": {},
"/topic": {},
"/history/": {},
"/history": {},
"/tag/all/": {},
"/tag/all": {},
"/useragreement/": {},
"/useragreement": {},
"/privacypolicy/": {},
"/privacypolicy": {},
"/dmca/": {},
"/dmca": {},
}
// NormalizePublicURL 将 URL 规范为 web 可访问地址;第二个返回值 false 表示应跳过。
func NormalizePublicURL(raw string) (string, bool) {
raw = strings.TrimSpace(raw)
if raw == "" {
return "", false
}
parsed, err := url.Parse(raw)
if err != nil {
return "", false
}
path := parsed.Path
if path == "" {
path = "/"
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
normalizedPath := normalizePathKey(path)
if _, skip := SkipPublicPaths[normalizedPath]; skip {
return "", false
}
if mapped, ok := LegacyPathRemap[normalizedPath]; ok {
path = mapped
parsed.Path = path
}
if parsed.Path != "/" {
parsed.Path = strings.TrimRight(parsed.Path, "/")
if parsed.Path == "" {
parsed.Path = "/"
}
}
return parsed.String(), true
}
// NormalizePublicPath 规范单条站内 path(不含域名)。
func NormalizePublicPath(path string) (string, bool) {
path = strings.TrimSpace(path)
if path == "" {
return "", false
}
if strings.Contains(path, "://") {
return NormalizePublicURL(path)
}
base := "https://example.com"
normalized, ok := NormalizePublicURL(base + path)
if !ok {
return "", false
}
parsed, err := url.Parse(normalized)
if err != nil {
return "", false
}
return parsed.Path, true
}
func normalizePathKey(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return "/"
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
lower := strings.ToLower(path)
if lower != "/" && !strings.HasSuffix(lower, "/") {
lower += "/"
}
return lower
}