55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package service
|
||
|
||
// pickModelScopeImageURL 从各类抠图 API 返回体中递归提取图片 URL(通用工具函数)
|
||
func pickModelScopeImageURL(v interface{}) string {
|
||
switch t := v.(type) {
|
||
case string:
|
||
if isHTTPURL(t) {
|
||
return t
|
||
}
|
||
case map[string]interface{}:
|
||
for _, key := range []string{"url", "image_url", "output_url", "result_url", "cutout_url"} {
|
||
if u, ok := t[key].(string); ok && isHTTPURL(u) {
|
||
return u
|
||
}
|
||
}
|
||
if images, ok := t["images"].([]interface{}); ok && len(images) > 0 {
|
||
if item, ok := images[0].(map[string]interface{}); ok {
|
||
if u := pickModelScopeImageURL(item); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
if u, ok := images[0].(string); ok && isHTTPURL(u) {
|
||
return u
|
||
}
|
||
}
|
||
if output, ok := t["output"].(map[string]interface{}); ok {
|
||
if u := pickModelScopeImageURL(output); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
if result, ok := t["result"].(map[string]interface{}); ok {
|
||
if u := pickModelScopeImageURL(result); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
if data, ok := t["data"].(map[string]interface{}); ok {
|
||
if u := pickModelScopeImageURL(data); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
for _, val := range t {
|
||
if u := pickModelScopeImageURL(val); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
case []interface{}:
|
||
for _, item := range t {
|
||
if u := pickModelScopeImageURL(item); u != "" {
|
||
return u
|
||
}
|
||
}
|
||
}
|
||
return ""
|
||
}
|