imageEditDownloadHTTPURL 下载一个 https/http 图片并包装为 editImageInput。
(client httpclient.AuroraHttpClient, url string)
| 1025 | |
| 1026 | // imageEditDownloadHTTPURL 下载一个 https/http 图片并包装为 editImageInput。 |
| 1027 | func imageEditDownloadHTTPURL(client httpclient.AuroraHttpClient, url string) (editImageInput, error) { |
| 1028 | if client == nil { |
| 1029 | client = bogdanfinn.NewStdClient() |
| 1030 | } |
| 1031 | headers := httpclient.AuroraHeaders{ |
| 1032 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36", |
| 1033 | "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8", |
| 1034 | } |
| 1035 | resp, err := client.Request(httpclient.GET, url, headers, nil, nil) |
| 1036 | if err != nil { |
| 1037 | return editImageInput{}, err |
| 1038 | } |
| 1039 | defer resp.Body.Close() |
| 1040 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 1041 | return editImageInput{}, fmt.Errorf("download image failed: status %d", resp.StatusCode) |
| 1042 | } |
| 1043 | data, err := io.ReadAll(resp.Body) |
| 1044 | if err != nil { |
| 1045 | return editImageInput{}, err |
| 1046 | } |
| 1047 | contentType := resp.Header.Get("Content-Type") |
| 1048 | if contentType == "" { |
| 1049 | contentType = http.DetectContentType(data) |
| 1050 | } |
| 1051 | filename := "image_url" |
| 1052 | if idx := strings.LastIndex(url, "/"); idx >= 0 && idx < len(url)-1 { |
| 1053 | filename = url[idx+1:] |
| 1054 | } |
| 1055 | if dot := strings.Index(filename, "?"); dot >= 0 { |
| 1056 | filename = filename[:dot] |
| 1057 | } |
| 1058 | if filename == "" { |
| 1059 | filename = "image_url.png" |
| 1060 | } |
| 1061 | return imageEditReadJSONImage(data, filename, contentType) |
| 1062 | } |
| 1063 | |
| 1064 | // imageEditConvertURL 把字符串(image_url)解析为 editImageInput。 |
| 1065 | func imageEditConvertURL(client httpclient.AuroraHttpClient, raw string) (editImageInput, bool, error) { |
no test coverage detected