From fc81eae5aaa4dc4d9d0eb3446f8d9b70cdb8a6fd Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 4 Jul 2023 16:30:31 +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: 4d246847cd2379c6df4d4ab9277ede9ceaeebe65 --- .../internal/handler/image/chunkhandler.go | 23 ++++---------- .../internal/handler/image/uploadhandler.go | 17 ++--------- common/tool/file.go | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 common/tool/file.go diff --git a/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go b/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go index 3a62dceb..3c272e8a 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go @@ -3,6 +3,7 @@ package image import ( "PCM/adaptor/PCM-CORE/model" result2 "PCM/common/result" + "PCM/common/tool" "bufio" "context" "encoding/base64" @@ -32,7 +33,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { name := r.PostFormValue("name") dataType := r.PostFormValue("dataType") // 对比合并请求的文件大小和已上传文件夹大小 - toSize, _ := getDirSize(filepath.Join(uploadTempPath, hash)) + toSize, _ := tool.GetDirSize(filepath.Join(uploadTempPath, hash)) if size != toSize { fmt.Fprintf(w, "文件上传错误") } @@ -45,7 +46,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { filesSort[nameArr[1]] = f.Name() } saveFile := filepath.Join(uploadPath, name) - if exists, _ := PathExists(saveFile); exists { + if exists, _ := tool.PathExists(saveFile); exists { os.Remove(saveFile) } fs, _ := os.OpenFile(saveFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm) @@ -62,7 +63,6 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { data, err := ioutil.ReadFile(fileName) fmt.Println(err) fs.Write(data) - wg.Done() } wg.Wait() @@ -90,7 +90,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return } // 删除本地文件 避免占用本地存储资源 - err = os.Remove(filepath.Join(uploadPath, name)) + go os.Remove(filepath.Join(uploadPath, name)) result2.HttpResult(r, w, nil, err) } } @@ -108,14 +108,15 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error { fileInfo, err := os.Open(filepath.Join(uploadPath, name)) defer fileInfo.Close() reader := bufio.NewReader(fileInfo) + if err != nil { return err } body, err := svcCtx.DockerClient.ImageLoad(context.Background(), reader, false) + if err != nil { return err } - bytes, err := ioutil.ReadAll(body.Body) loadBody := LoadBody{} @@ -150,15 +151,3 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error { } return nil } - -// DirSize 获取整体文件夹大小 -func getDirSize(path string) (int64, error) { - var size int64 - err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { - if !info.IsDir() { - size += info.Size() - } - return err - }) - return size, err -} diff --git a/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go b/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go index 002f3711..a9001b5e 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go @@ -2,6 +2,7 @@ package image import ( result2 "PCM/common/result" + "PCM/common/tool" "bufio" "fmt" "io" @@ -32,12 +33,12 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { // 文件路径 filePath := filepath.Join(chunksPath, hash+"-"+index) // 检查临时文件夹是否存在 不存在则创建文件夹 - isPathExists, err := PathExists(chunksPath) + isPathExists, err := tool.PathExists(chunksPath) if !isPathExists { err = os.MkdirAll(chunksPath, os.ModePerm) } // 检查文件是否存在 - exists, err := PathExists(filePath) + exists, err := tool.PathExists(filePath) // 文件存在 进行断点续传 if exists { fileInfo, _ := os.Stat(filePath) @@ -86,15 +87,3 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { result2.HttpResult(r, w, nil, err) } } - -// PathExists 判断文件夹是否存在 -func PathExists(path string) (bool, error) { - _, err := os.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} diff --git a/common/tool/file.go b/common/tool/file.go new file mode 100644 index 00000000..58728acc --- /dev/null +++ b/common/tool/file.go @@ -0,0 +1,30 @@ +package tool + +import ( + "os" + "path/filepath" +) + +// DirSize 获取整体文件夹大小 +func GetDirSize(path string) (int64, error) { + var size int64 + err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + if !info.IsDir() { + size += info.Size() + } + return err + }) + return size, err +} + +// PathExists 判断文件夹是否存在 +func PathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +}