文件上传

Former-commit-id: 8f0f03f780f6b4ad4021b04c7c73afeac7910077
This commit is contained in:
zhangwei 2023-07-04 15:36:50 +08:00
parent eabf3c2edd
commit be7723ff07
2 changed files with 16 additions and 13 deletions

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
}

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