From 33baccb3f5c2f9bf7a24747800ffa89d6cdec473 Mon Sep 17 00:00:00 2001 From: YangSen-qn Date: Tue, 10 Oct 2023 18:08:07 +0800 Subject: [PATCH] optimize get network file info --- iqshell/common/utils/file.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/iqshell/common/utils/file.go b/iqshell/common/utils/file.go index dc45feb6..c989db09 100644 --- a/iqshell/common/utils/file.go +++ b/iqshell/common/utils/file.go @@ -4,13 +4,15 @@ import ( "bufio" "encoding/json" "fmt" - "github.com/qiniu/qshell/v2/iqshell/common/data" + "io" "io/ioutil" - "net/http" "os" "path/filepath" "strconv" "strings" + + "github.com/qiniu/qshell/v2/iqshell/common/client" + "github.com/qiniu/qshell/v2/iqshell/common/data" ) const ( @@ -128,11 +130,16 @@ func NetworkFileLength(srcResUrl string) (fileSize int64, err *data.CodeError) { func GetNetworkFileInfo(srcResUrl string) (*NetworkFileInfo, *data.CodeError) { - resp, respErr := http.Head(srcResUrl) + resp, respErr := client.DefaultStorageClient().Head(srcResUrl) if respErr != nil { return nil, data.NewEmptyError().AppendDescF("New head request failed, %s", respErr.Error()) } - defer resp.Body.Close() + defer func() { + if resp.Body != nil { + io.Copy(ioutil.Discard, resp.Body) + resp.Body.Close() + } + }() file := &NetworkFileInfo{ Size: -1,