Skip to content

Commit 61f77dc

Browse files
support download file
1 parent 06bdaff commit 61f77dc

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

utils/utils.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,40 @@ func MapKeysToString(input interface{}) string {
265265
return strings.Join(keyStrs, ",")
266266
}
267267

268-
func DownloadFile(url string) ([]byte, error) {
269-
if url == "" {
268+
func DownloadFile(urlStr string) ([]byte, error) {
269+
if urlStr == "" {
270270
return nil, errors.New("url is empty")
271271
}
272272

273-
client := GetRobotProxyClient()
273+
// 解析 URL 以判断协议
274+
parsedURL, err := url.Parse(urlStr)
275+
if err != nil {
276+
return nil, errors.New("invalid URL format: " + err.Error())
277+
}
278+
279+
// 处理 file:// 协议
280+
if parsedURL.Scheme == "file" {
281+
// 去除 "file://" 前缀,得到本地文件路径
282+
filePath := strings.TrimPrefix(urlStr, "file://")
283+
// 对于 Windows 路径可能需要额外处理,但你的路径是 macOS/Linux 格式
284+
data, err := os.ReadFile(filePath)
285+
if err != nil {
286+
return nil, errors.New("failed to read local file: " + err.Error())
287+
}
288+
return data, nil
289+
}
274290

275-
resp, err := client.Get(url)
291+
client := GetRobotProxyClient()
292+
resp, err := client.Get(urlStr)
276293
if err != nil {
277294
return nil, err
278295
}
279296
defer resp.Body.Close()
280297

281-
// 检查 HTTP 状态码
282298
if resp.StatusCode != http.StatusOK {
283299
return nil, errors.New("failed to download file: " + resp.Status)
284300
}
285301

286-
// 读取响应体内容
287302
data, err := io.ReadAll(resp.Body)
288303
if err != nil {
289304
return nil, err

0 commit comments

Comments
 (0)