From db8acc2e128104f782eb216d64ef0283a860aa35 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Wed, 31 May 2023 08:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=96=AD=E7=82=B9=E7=BB=AD?= =?UTF-8?q?=E4=BC=A0=E5=B7=B2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 8abc72dc7225fc37a456bc08f133a4ff8d41c90f --- .../internal/handler/image/chunkimagehandler.go | 14 ++++++-------- .../handler/image/uploadimagehandler.go | 17 +++++++++-------- adaptor/PCM-CORE/api/internal/handler/routes.go | 1 + .../api/internal/logic/image/checklogic.go | 13 +++++-------- .../PCM-TH/rpc/internal/logic/cronlogic.go | 5 ++--- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go b/adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go index 3cf6e57f..17d1a8f3 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "net/http" "os" - "path" "path/filepath" "strconv" "strings" @@ -17,8 +16,7 @@ import ( ) var dir, _ = os.Getwd() -var windowsUploadPath = strings.ReplaceAll(path.Join(dir, "uploads"), "/", "\\") -var linuxUploadPath = path.Join(dir, "uploads") +var uploadPath = filepath.Join(dir, "uploads") func ChunkImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -26,11 +24,11 @@ func ChunkImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { hash := r.PostFormValue("hash") name := r.PostFormValue("name") // 对比合并请求的文件大小和已上传文件夹大小 - toSize, _ := getDirSize(path.Join(linuxUploadTempPath, hash)) + toSize, _ := getDirSize(filepath.Join(uploadTempPath, hash)) if size != toSize { fmt.Fprintf(w, "文件上传错误") } - chunksPath := path.Join(linuxUploadTempPath, hash) + chunksPath := filepath.Join(uploadTempPath, hash) files, _ := ioutil.ReadDir(chunksPath) // 将文件根据索引序号排序 filesSort := make(map[string]string) @@ -38,7 +36,7 @@ func ChunkImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { nameArr := strings.Split(f.Name(), "-") filesSort[nameArr[1]] = f.Name() } - saveFile := path.Join(linuxUploadPath, name) + saveFile := filepath.Join(uploadPath, name) if exists, _ := PathExists(saveFile); exists { os.Remove(saveFile) } @@ -51,7 +49,7 @@ func ChunkImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { wg.Add(filesCount) for i := 0; i < filesCount; i++ { // 这里一定要注意按顺序读取不然文件就会损坏 - fileName := path.Join(chunksPath, "/"+filesSort[strconv.Itoa(i)]) + fileName := filepath.Join(chunksPath, filesSort[strconv.Itoa(i)]) data, err := ioutil.ReadFile(fileName) fmt.Println(err) fs.Write(data) @@ -59,7 +57,7 @@ func ChunkImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { wg.Done() } wg.Wait() - os.RemoveAll(path.Join(chunksPath, "/")) + os.RemoveAll(chunksPath) defer fs.Close() //// 加载镜像文件到docker diff --git a/adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go b/adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go index ead5736a..11a43679 100644 --- a/adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go +++ b/adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go @@ -8,8 +8,8 @@ import ( "net/http" "os" "path" + "path/filepath" "strconv" - "strings" "syscall" "PCM/adaptor/PCM-CORE/api/internal/svc" @@ -19,8 +19,7 @@ type LoadBody struct { Stream string `json:"stream"` } -var windowsUploadTempPath = strings.ReplaceAll(path.Join(windowsUploadPath, "temp"), "/", "\\") -var linuxUploadTempPath = path.Join(linuxUploadPath) +var uploadTempPath = filepath.Join(uploadPath, "temp") func UploadImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -28,28 +27,30 @@ func UploadImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { index := r.PostFormValue("index") hash := r.PostFormValue("hash") // 合并路径 - chunksPath := path.Join(linuxUploadTempPath, hash) + chunksPath := filepath.Join(uploadTempPath, hash) // 文件路径 - filePath := path.Join(chunksPath + "/" + hash + "-" + index) + filePath := filepath.Join(chunksPath, hash+"-"+index) // 检查临时文件夹是否存在 isPathExists, err := PathExists(chunksPath) if !isPathExists { err = os.MkdirAll(chunksPath, os.ModePerm) } + // 检查文件是否存在 exists, err := PathExists(filePath) if exists { fileInfo, _ := os.Stat(filePath) if fileInfo.Size() == fileHeader.Size { + result2.HttpResult(r, w, nil, err) return } start := strconv.Itoa(int(fileInfo.Size())) - oldfile, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm) + oldFile, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm) defer file.Close() count, _ := strconv.ParseInt(start, 10, 64) fmt.Println("已上传:", count) // 设置读,写的偏移量 file.Seek(count, 0) - oldfile.Seek(count, 0) + oldFile.Seek(count, 0) data := make([]byte, 1024, 1024) for { total, err := file.Read(data) @@ -57,7 +58,7 @@ func UploadImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { fmt.Println("文件复制完毕") break } - oldfile.Write(data[:total]) + oldFile.Write(data[:total]) } } else { diff --git a/adaptor/PCM-CORE/api/internal/handler/routes.go b/adaptor/PCM-CORE/api/internal/handler/routes.go index 771c6be1..b724e308 100644 --- a/adaptor/PCM-CORE/api/internal/handler/routes.go +++ b/adaptor/PCM-CORE/api/internal/handler/routes.go @@ -292,6 +292,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Handler: image.CheckHandler(serverCtx), }, }, + rest.WithMaxBytes(111111111), rest.WithPrefix("/pcm/v1"), ) } diff --git a/adaptor/PCM-CORE/api/internal/logic/image/checklogic.go b/adaptor/PCM-CORE/api/internal/logic/image/checklogic.go index 5b3f954c..1c902604 100644 --- a/adaptor/PCM-CORE/api/internal/logic/image/checklogic.go +++ b/adaptor/PCM-CORE/api/internal/logic/image/checklogic.go @@ -1,14 +1,12 @@ package image import ( + "PCM/adaptor/PCM-CORE/api/internal/svc" + "PCM/adaptor/PCM-CORE/api/internal/types" "context" "io/ioutil" "os" - "path" - "strings" - - "PCM/adaptor/PCM-CORE/api/internal/svc" - "PCM/adaptor/PCM-CORE/api/internal/types" + "path/filepath" "github.com/zeromicro/go-zero/core/logx" ) @@ -20,8 +18,7 @@ type CheckLogic struct { } var dir, _ = os.Getwd() -var windowsUploadPath = strings.ReplaceAll(path.Join(dir, "uploads"), "/", "\\") -var linuxUploadPath = path.Join(dir, "uploads") +var uploadPath = filepath.Join(dir, "uploads") func NewCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckLogic { return &CheckLogic{ @@ -33,7 +30,7 @@ func NewCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckLogic func (l *CheckLogic) Check(req *types.CheckReq) (resp *types.CheckResp, err error) { // todo: add your logic here and delete this line - files, err := ioutil.ReadDir(windowsUploadPath) + files, err := ioutil.ReadDir(uploadPath) if err != nil { return nil, err } diff --git a/adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go b/adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go index a920ee6f..0981dfde 100644 --- a/adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go +++ b/adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go @@ -17,7 +17,6 @@ func InitCron(svc *svc.ServiceContext) { svc.Cron.Start() submitJobLogic := NewSubmitJobLogic(context.Background(), svc) listLogic := NewListJobLogic(context.Background(), svc) - historyListLogic := NewListHistoryJobLogic(context.Background(), svc) svc.Cron.AddFunc("*/5 * * * * ?", func() { // 查询core端分发下来的任务列表 @@ -31,7 +30,7 @@ func InitCron(svc *svc.ServiceContext) { return } // 提交任务 - submitJob(infoList, submitJobLogic, historyListLogic) + submitJob(infoList, submitJobLogic) // 查询运行中的任务列表同步信息 listReq := hpcTH.ListJobReq{} listJob, err := listLogic.ListJob(&listReq) @@ -63,7 +62,7 @@ func InitCron(svc *svc.ServiceContext) { }) } -func submitJob(infoList *pcmcoreclient.InfoListResp, submitJobLogic *SubmitJobLogic, historyListLogic *ListHistoryJobLogic) { +func submitJob(infoList *pcmcoreclient.InfoListResp, submitJobLogic *SubmitJobLogic) { for index, _ := range infoList.HpcInfoList { if infoList.HpcInfoList[index].Status == "Saved" { submitReq := hpcTH.SubmitJobReq{