From e72382b8c3093ac8cbbb3b79147c3387d37e8ffb Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期五, 17 五月 2024 22:20:20 +0800 Subject: [PATCH] 添加图片下载功能 --- util/http.go | 84 ++++++++++++++++++++++++++++ repository/captureRepo.go | 41 +++++++++---- 2 files changed, 111 insertions(+), 14 deletions(-) diff --git a/repository/captureRepo.go b/repository/captureRepo.go index cddc2a6..505d6dc 100644 --- a/repository/captureRepo.go +++ b/repository/captureRepo.go @@ -3,9 +3,9 @@ import ( "encoding/base64" "encoding/json" - "gat1400Exchange/client" "time" + "gat1400Exchange/client" "gat1400Exchange/config" "gat1400Exchange/models" "gat1400Exchange/pkg/logger" @@ -43,16 +43,31 @@ // 鑾峰彇澶у浘, 鐩墠娴峰悍鐨勫皬鍥惧垎杈ㄧ巼澶綆 for _, image := range face.SubImageList.SubImageInfoObject { - if len(image.Data) > len(bgImageStr) { - bgImageStr = image.Data - imageType = image.Type + if imageType != "14" { + continue + } + + if len(image.Data) > 0 { + if len(image.Data) > len(bgImageStr) { + bgImageStr = image.Data + } + } else if image.StoragePath != "" { + imgData, err := util.ImageDownload(image.StoragePath, nil) + if err != nil { + logger.Warn("Image download failure, %s", err.Error()) + } else { + bgImageBytes = imgData + bgImageStr = "///" + } } } - bgImageBytes, err = base64.StdEncoding.DecodeString(bgImageStr) - if err != nil { - logger.Warn("Decode Image Base64 String failure, %s", err.Error()) - continue + if bgImageBytes == nil { + bgImageBytes, err = base64.StdEncoding.DecodeString(bgImageStr) + if err != nil { + logger.Warn("Decode Image Base64 String failure, %s", err.Error()) + continue + } } // 鍒ゆ柇鍥剧墖绫诲瀷鏄惁涓哄満鏅浘, 鏍规嵁浜鸿劯鍧愭爣鍒囧皬鍥�. @@ -71,18 +86,16 @@ } // 杞彂鍥惧儚 - logger.Debug("Prepare forward image, deviceId:%s, image len:%d, server:%s", deviceId, len(bgImageStr), config.ForwardConf.SyncServer) + logger.Debug("Prepare forward image, deviceId:%s, image len:%d, server:%s", deviceId, len(bgImageBytes), config.ForwardConf.SyncServer) if deviceId != "" && bgImageStr != "" && config.ForwardConf.SyncServer != "" { pd := c.PackPushDataV2(deviceId, faceId, face.FaceAppearTime, bgImageBytes, faceImageBytes) if pd == nil { return } - // 閮ㄧ讲鍦ㄦ湇鍔$鐨�1400浠g悊, 浠呮帴鏀�1400淇℃伅, 妤煎眰淇℃伅鏆傚瓨鍦╫herFeature瀛楁 - if config.ServeConf.Role == "proxy" { - if face.OtherFeature != "" { - pd.CameraFloor = face.OtherFeature - } + // 澶勭悊姊帶濉啓鐨勬ゼ灞備俊鎭� 鏆傛椂浣跨敤oherFeature瀛楁 + if face.OtherFeature != "" { + pd.CameraFloor = face.OtherFeature } payload, err := json.Marshal(pd) diff --git a/util/http.go b/util/http.go index 38ad875..d518287 100644 --- a/util/http.go +++ b/util/http.go @@ -3,8 +3,12 @@ import ( "bytes" "encoding/json" + "errors" + "fmt" "io" "net/http" + "strconv" + "time" "gat1400Exchange/pkg/logger" "gat1400Exchange/vo" @@ -109,3 +113,83 @@ return body, nil } + +func ImageDownload(downloadUrl string, fb func(length, downLen int64)) ([]byte, error) { + var ( + fSize int64 + buf = make([]byte, 10*1024) + buffer bytes.Buffer + written int64 + ) + + request, err := http.NewRequest("GET", downloadUrl, nil) + if err != nil { + return nil, err + } + + //鍒涘缓涓�涓猦ttp client + client := new(http.Client) + client.Timeout = time.Second * 600 //璁剧疆瓒呮椂鏃堕棿 + + // get鏂规硶鑾峰彇璧勬簮 + resp, err := client.Do(request) + if err != nil { + return nil, err + } + + if resp.StatusCode != 200 { + return nil, errors.New(fmt.Sprintf("璇锋眰澶辫触, 鐘舵�佺爜:%d", resp.StatusCode)) + } + + //璇诲彇鏈嶅姟鍣ㄨ繑鍥炵殑鏂囦欢澶у皬 + fSize, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32) + if err != nil { + return nil, err + } + + if resp.Body == nil { + return nil, errors.New("body is null") + } + + defer resp.Body.Close() + + //涓嬮潰鏄� io.copyBuffer() 鐨勭畝鍖栫増鏈� + for { + //璇诲彇bytes + nr, er := resp.Body.Read(buf) + if nr > 0 { + //鍐欏叆bytes + nw, ew := buffer.Write(buf[0:nr]) + //鏁版嵁闀垮害澶т簬0 + if nw > 0 { + written += int64(nw) + } + + //鍐欏叆鍑洪敊 + if ew != nil { + err = ew + break + } + + //璇诲彇鏄暟鎹暱搴︿笉绛変簬鍐欏叆鐨勬暟鎹暱搴� + if nr != nw { + err = io.ErrShortWrite + break + } + } + + if er != nil { + if er != io.EOF { + err = er + } + break + } + + //娌℃湁閿欒浜嗗揩浣跨敤 callback + if fb != nil { + fb(fSize, written) + } + } + + return buffer.Bytes(), err +} -- Gitblit v1.8.0