Browse Source

文件断点续传已完成

Former-commit-id: 8abc72dc72
pull/9/head
zhangwei 3 years ago
parent
commit
db8acc2e12
5 changed files with 23 additions and 27 deletions
  1. +6
    -8
      adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go
  2. +9
    -8
      adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go
  3. +1
    -0
      adaptor/PCM-CORE/api/internal/handler/routes.go
  4. +5
    -8
      adaptor/PCM-CORE/api/internal/logic/image/checklogic.go
  5. +2
    -3
      adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go

+ 6
- 8
adaptor/PCM-CORE/api/internal/handler/image/chunkimagehandler.go View File

@@ -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


+ 9
- 8
adaptor/PCM-CORE/api/internal/handler/image/uploadimagehandler.go View File

@@ -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 {


+ 1
- 0
adaptor/PCM-CORE/api/internal/handler/routes.go View File

@@ -292,6 +292,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: image.CheckHandler(serverCtx),
},
},
rest.WithMaxBytes(111111111),
rest.WithPrefix("/pcm/v1"),
)
}

+ 5
- 8
adaptor/PCM-CORE/api/internal/logic/image/checklogic.go View File

@@ -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
}


+ 2
- 3
adaptor/PCM-HPC/PCM-TH/rpc/internal/logic/cronlogic.go View File

@@ -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{


Loading…
Cancel
Save