Browse Source

文件上传

Former-commit-id: 8f0f03f780
pull/9/head
zhangwei 2 years ago
parent
commit
be7723ff07
2 changed files with 16 additions and 13 deletions
  1. +14
    -12
      adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go
  2. +2
    -1
      adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go

+ 14
- 12
adaptor/PCM-CORE/api/internal/handler/image/chunkhandler.go View File

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



+ 2
- 1
adaptor/PCM-CORE/api/internal/handler/image/uploadhandler.go View File

@@ -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)
// 设置读,写的偏移量


Loading…
Cancel
Save