From be7723ff07c4325d73805cb13c961eac926a23c5 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 4 Jul 2023 15:36:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 8f0f03f780f6b4ad4021b04c7c73afeac7910077 --- .../internal/handler/image/chunkhandler.go | 26 ++++++++++--------- .../internal/handler/image/uploadhandler.go | 3 ++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go b/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go index 1b05f1d1..3a62dceb 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go @@ -49,6 +49,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { os.Remove(saveFile) } fs, _ := os.OpenFile(saveFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm) + defer fs.Close() var wg sync.WaitGroup filesCount := len(files) if filesCount != len(filesSort) { @@ -66,7 +67,6 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } wg.Wait() os.RemoveAll(chunksPath) - defer fs.Close() // 保存到数据库表里 svcCtx.DbEngin.Create(&model.File{ @@ -77,17 +77,20 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { Bucket: "pcm"}) // 根据数据类型按需上传(镜像推送到nexus 数据集和算法推送到云际存储) - //switch dataType { - //case "image": - // pushImage(svcCtx, name) - //case "dataSet", "algorithm": - // uploadStorage(svcCtx, name) - //} + switch dataType { + case "image": + pushImage(svcCtx, name) + case "dataSet", "algorithm": + uploadStorage(svcCtx, name) + } err := pushImage(svcCtx, name) if err != nil { + result2.HttpResult(r, w, nil, err) return } + // 删除本地文件 避免占用本地存储资源 + err = os.Remove(filepath.Join(uploadPath, name)) result2.HttpResult(r, w, nil, err) } } @@ -103,6 +106,7 @@ func uploadStorage(svcCtx *svc.ServiceContext, name string) error { func pushImage(svcCtx *svc.ServiceContext, name string) error { // 加载镜像文件到docker fileInfo, err := os.Open(filepath.Join(uploadPath, name)) + defer fileInfo.Close() reader := bufio.NewReader(fileInfo) if err != nil { return err @@ -111,12 +115,15 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error { if err != nil { return err } + bytes, err := ioutil.ReadAll(body.Body) + loadBody := LoadBody{} err = json.Unmarshal(bytes, &loadBody) if err != nil { return err } + imageName := strings.TrimSpace(loadBody.Stream[13:]) privateImageName := "hub.jcce.dev:18445/" + imageName // 给镜像打上私有仓库的tag @@ -141,11 +148,6 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error { if err != nil { return err } - // 删除本地文件 避免占用本地存储资源 - err = os.Remove(filepath.Join(uploadPath, name)) - if err != nil { - return err - } return nil } diff --git a/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go b/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go index dc35dd68..002f3711 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go @@ -26,6 +26,7 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { index := r.PostFormValue("index") hash := r.PostFormValue("hash") + defer file.Close() // 合并路径 chunksPath := filepath.Join(uploadTempPath, hash) // 文件路径 @@ -46,7 +47,7 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } start := strconv.Itoa(int(fileInfo.Size())) oldFile, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm) - defer file.Close() + defer oldFile.Close() count, _ := strconv.ParseInt(start, 10, 64) fmt.Println("已上传:", count) // 设置读,写的偏移量