文件上传

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) os.Remove(saveFile)
} }
fs, _ := os.OpenFile(saveFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm) fs, _ := os.OpenFile(saveFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
defer fs.Close()
var wg sync.WaitGroup var wg sync.WaitGroup
filesCount := len(files) filesCount := len(files)
if filesCount != len(filesSort) { if filesCount != len(filesSort) {
@ -66,7 +67,6 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
} }
wg.Wait() wg.Wait()
os.RemoveAll(chunksPath) os.RemoveAll(chunksPath)
defer fs.Close()
// 保存到数据库表里 // 保存到数据库表里
svcCtx.DbEngin.Create(&model.File{ svcCtx.DbEngin.Create(&model.File{
@ -77,17 +77,20 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
Bucket: "pcm"}) Bucket: "pcm"})
// 根据数据类型按需上传镜像推送到nexus 数据集和算法推送到云际存储) // 根据数据类型按需上传镜像推送到nexus 数据集和算法推送到云际存储)
//switch dataType { switch dataType {
//case "image": case "image":
// pushImage(svcCtx, name) pushImage(svcCtx, name)
//case "dataSet", "algorithm": case "dataSet", "algorithm":
// uploadStorage(svcCtx, name) uploadStorage(svcCtx, name)
//} }
err := pushImage(svcCtx, name) err := pushImage(svcCtx, name)
if err != nil { if err != nil {
result2.HttpResult(r, w, nil, err)
return return
} }
// 删除本地文件 避免占用本地存储资源
err = os.Remove(filepath.Join(uploadPath, name))
result2.HttpResult(r, w, nil, err) 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 { func pushImage(svcCtx *svc.ServiceContext, name string) error {
// 加载镜像文件到docker // 加载镜像文件到docker
fileInfo, err := os.Open(filepath.Join(uploadPath, name)) fileInfo, err := os.Open(filepath.Join(uploadPath, name))
defer fileInfo.Close()
reader := bufio.NewReader(fileInfo) reader := bufio.NewReader(fileInfo)
if err != nil { if err != nil {
return err return err
@ -111,12 +115,15 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error {
if err != nil { if err != nil {
return err return err
} }
bytes, err := ioutil.ReadAll(body.Body) bytes, err := ioutil.ReadAll(body.Body)
loadBody := LoadBody{} loadBody := LoadBody{}
err = json.Unmarshal(bytes, &loadBody) err = json.Unmarshal(bytes, &loadBody)
if err != nil { if err != nil {
return err return err
} }
imageName := strings.TrimSpace(loadBody.Stream[13:]) imageName := strings.TrimSpace(loadBody.Stream[13:])
privateImageName := "hub.jcce.dev:18445/" + imageName privateImageName := "hub.jcce.dev:18445/" + imageName
// 给镜像打上私有仓库的tag // 给镜像打上私有仓库的tag
@ -141,11 +148,6 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error {
if err != nil { if err != nil {
return err return err
} }
// 删除本地文件 避免占用本地存储资源
err = os.Remove(filepath.Join(uploadPath, name))
if err != nil {
return err
}
return nil return nil
} }

View File

@ -26,6 +26,7 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
index := r.PostFormValue("index") index := r.PostFormValue("index")
hash := r.PostFormValue("hash") hash := r.PostFormValue("hash")
defer file.Close()
// 合并路径 // 合并路径
chunksPath := filepath.Join(uploadTempPath, hash) chunksPath := filepath.Join(uploadTempPath, hash)
// 文件路径 // 文件路径
@ -46,7 +47,7 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
} }
start := strconv.Itoa(int(fileInfo.Size())) 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() defer oldFile.Close()
count, _ := strconv.ParseInt(start, 10, 64) count, _ := strconv.ParseInt(start, 10, 64)
fmt.Println("已上传:", count) fmt.Println("已上传:", count)
// 设置读,写的偏移量 // 设置读,写的偏移量