Files
Atlas/internal/service/asset/url.go
T
renjue 2b020bb782
CI / docker (push) Successful in 2m58s
实现 Atlas AI 平台一期能力并完成品牌化改造。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 23:35:19 +08:00

75 lines
1.6 KiB
Go

package asset
import (
"net/http"
"strings"
)
const userFilesPathPrefix = "/api/v1/user/files/"
func PublicBaseURL(r *http.Request) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if proto := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); proto != "" {
scheme = strings.ToLower(proto)
}
host := r.Host
if fwd := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); fwd != "" {
host = strings.Split(fwd, ",")[0]
host = strings.TrimSpace(host)
}
if host == "" {
return ""
}
return scheme + "://" + host
}
func APIBaseURL(r *http.Request) string {
base := PublicBaseURL(r)
if base == "" {
return ""
}
return strings.TrimRight(base, "/") + "/api/v1"
}
func ResolvePublicFileURLs(params map[string]any, publicBase string) {
if publicBase == "" || len(params) == 0 {
return
}
publicBase = strings.TrimRight(publicBase, "/")
for key, value := range params {
params[key] = resolvePublicFileValue(value, publicBase)
}
}
func resolvePublicFileValue(value any, publicBase string) any {
switch v := value.(type) {
case string:
return resolvePublicFileString(v, publicBase)
case []any:
out := make([]any, len(v))
for i, item := range v {
out[i] = resolvePublicFileValue(item, publicBase)
}
return out
case []string:
out := make([]string, len(v))
for i, item := range v {
out[i] = resolvePublicFileString(item, publicBase)
}
return out
default:
return value
}
}
func resolvePublicFileString(raw, publicBase string) string {
value := strings.TrimSpace(raw)
if value == "" || !strings.HasPrefix(value, userFilesPathPrefix) {
return raw
}
return publicBase + value
}