mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-17 13:43:42 +08:00
🔖 chore: add chat image request proxy address
This commit is contained in:
26
common/image/http.go
Normal file
26
common/image/http.go
Normal 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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user