mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor(gemini-web): Centralize HTTP client creation for media
This commit is contained in:
@@ -2,7 +2,6 @@ package geminiwebapi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -11,8 +10,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -69,18 +66,9 @@ func (i Image) Save(path string, filename string, cookies map[string]string, ver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Build client with cookie jar so cookies persist across redirects.
|
// Build client using shared helper to keep proxy/TLS behavior consistent.
|
||||||
tr := &http.Transport{}
|
client := newHTTPClient(httpOptions{ProxyURL: i.Proxy, Insecure: insecure, FollowRedirects: true})
|
||||||
if i.Proxy != "" {
|
client.Timeout = 120 * time.Second
|
||||||
if pu, err := url.Parse(i.Proxy); err == nil {
|
|
||||||
tr.Proxy = http.ProxyURL(pu)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if insecure {
|
|
||||||
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
||||||
}
|
|
||||||
jar, _ := cookiejar.New(nil)
|
|
||||||
client := &http.Client{Transport: tr, Timeout: 120 * time.Second, Jar: jar}
|
|
||||||
|
|
||||||
// Helper to set raw Cookie header using provided cookies (to mirror Python client behavior).
|
// Helper to set raw Cookie header using provided cookies (to mirror Python client behavior).
|
||||||
buildCookieHeader := func(m map[string]string) string {
|
buildCookieHeader := func(m map[string]string) string {
|
||||||
@@ -352,23 +340,11 @@ func uploadFile(path string, proxy string, insecure bool) (string, error) {
|
|||||||
}
|
}
|
||||||
_ = mw.Close()
|
_ = mw.Close()
|
||||||
|
|
||||||
tr := &http.Transport{}
|
client := newHTTPClient(httpOptions{ProxyURL: proxy, Insecure: insecure, FollowRedirects: true})
|
||||||
if proxy != "" {
|
client.Timeout = 300 * time.Second
|
||||||
if pu, errParse := url.Parse(proxy); errParse == nil {
|
|
||||||
tr.Proxy = http.ProxyURL(pu)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if insecure {
|
|
||||||
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
||||||
}
|
|
||||||
client := &http.Client{Transport: tr, Timeout: 300 * time.Second}
|
|
||||||
|
|
||||||
req, _ := http.NewRequest(http.MethodPost, EndpointUpload, &buf)
|
req, _ := http.NewRequest(http.MethodPost, EndpointUpload, &buf)
|
||||||
for k, v := range HeadersUpload {
|
applyHeaders(req, HeadersUpload)
|
||||||
for _, vv := range v {
|
|
||||||
req.Header.Add(k, vv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", mw.FormDataContentType())
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
||||||
req.Header.Set("Accept", "*/*")
|
req.Header.Set("Accept", "*/*")
|
||||||
req.Header.Set("Connection", "keep-alive")
|
req.Header.Set("Connection", "keep-alive")
|
||||||
|
|||||||
Reference in New Issue
Block a user