🔖 chore: add chat image request proxy address

This commit is contained in:
MartialBE
2024-05-29 04:01:28 +08:00
parent 3d8a51e139
commit b5a4283b28
11 changed files with 76 additions and 53 deletions

26
common/image/http.go Normal file
View File

@@ -0,0 +1,26 @@
package image
import (
"net/http"
"one-api/common/config"
"one-api/common/utils"
"time"
)
var ImageHttpClients = &http.Client{
Transport: &http.Transport{
DialContext: utils.Socks5ProxyFunc,
Proxy: utils.ProxyFunc,
},
Timeout: 15 * time.Second,
}
func requestImage(url, method string) (*http.Response, error) {
res, err := utils.RequestBuilder(utils.SetProxy(config.ChatImageRequestProxy, nil), method, url, nil, nil)
if err != nil {
return nil, err
}
return ImageHttpClients.Do(res)
}

View File

@@ -16,17 +16,8 @@ import (
_ "golang.org/x/image/webp"
)
// var ImageHttpClients = &http.Client{
// Transport: &http.Transport{
// DialContext: requester.Socks5ProxyFunc,
// Proxy: requester.ProxyFunc,
// },
// //
// // // Timeout: 30 * time.Second,
// }
func IsImageUrl(url string) (bool, error) {
resp, err := http.Head(url)
resp, err := requestImage(url, http.MethodHead)
if err != nil {
return false, err
}
@@ -41,7 +32,7 @@ func GetImageSizeFromUrl(url string) (width int, height int, err error) {
if !isImage {
return
}
resp, err := http.Get(url)
resp, err := requestImage(url, http.MethodGet)
if err != nil {
return
}
@@ -126,6 +117,6 @@ func GetImageSize(image string) (width int, height int, err error) {
case strings.HasPrefix(image, "http"):
return GetImageSizeFromUrl(image)
default:
return 0, 0, errors.New("invalid file type, Please view request interface!")
return 0, 0, errors.New("invalid file type, please view request interface")
}
}