文件上传
Former-commit-id: 4d246847cd2379c6df4d4ab9277ede9ceaeebe65
This commit is contained in:
parent
ef9651c028
commit
fc81eae5aa
|
|
@ -3,6 +3,7 @@ package image
|
|||
import (
|
||||
"PCM/adaptor/PCM-CORE/model"
|
||||
result2 "PCM/common/result"
|
||||
"PCM/common/tool"
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
|
|
@ -32,7 +33,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
name := r.PostFormValue("name")
|
||||
dataType := r.PostFormValue("dataType")
|
||||
// 对比合并请求的文件大小和已上传文件夹大小
|
||||
toSize, _ := getDirSize(filepath.Join(uploadTempPath, hash))
|
||||
toSize, _ := tool.GetDirSize(filepath.Join(uploadTempPath, hash))
|
||||
if size != toSize {
|
||||
fmt.Fprintf(w, "文件上传错误")
|
||||
}
|
||||
|
|
@ -45,7 +46,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
filesSort[nameArr[1]] = f.Name()
|
||||
}
|
||||
saveFile := filepath.Join(uploadPath, name)
|
||||
if exists, _ := PathExists(saveFile); exists {
|
||||
if exists, _ := tool.PathExists(saveFile); exists {
|
||||
os.Remove(saveFile)
|
||||
}
|
||||
fs, _ := os.OpenFile(saveFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
|
||||
|
|
@ -62,7 +63,6 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
data, err := ioutil.ReadFile(fileName)
|
||||
fmt.Println(err)
|
||||
fs.Write(data)
|
||||
|
||||
wg.Done()
|
||||
}
|
||||
wg.Wait()
|
||||
|
|
@ -90,7 +90,7 @@ func ChunkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
// 删除本地文件 避免占用本地存储资源
|
||||
err = os.Remove(filepath.Join(uploadPath, name))
|
||||
go os.Remove(filepath.Join(uploadPath, name))
|
||||
result2.HttpResult(r, w, nil, err)
|
||||
}
|
||||
}
|
||||
|
|
@ -108,14 +108,15 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error {
|
|||
fileInfo, err := os.Open(filepath.Join(uploadPath, name))
|
||||
defer fileInfo.Close()
|
||||
reader := bufio.NewReader(fileInfo)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := svcCtx.DockerClient.ImageLoad(context.Background(), reader, false)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadAll(body.Body)
|
||||
|
||||
loadBody := LoadBody{}
|
||||
|
|
@ -150,15 +151,3 @@ func pushImage(svcCtx *svc.ServiceContext, name string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DirSize 获取整体文件夹大小
|
||||
func getDirSize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package image
|
|||
|
||||
import (
|
||||
result2 "PCM/common/result"
|
||||
"PCM/common/tool"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -32,12 +33,12 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
// 文件路径
|
||||
filePath := filepath.Join(chunksPath, hash+"-"+index)
|
||||
// 检查临时文件夹是否存在 不存在则创建文件夹
|
||||
isPathExists, err := PathExists(chunksPath)
|
||||
isPathExists, err := tool.PathExists(chunksPath)
|
||||
if !isPathExists {
|
||||
err = os.MkdirAll(chunksPath, os.ModePerm)
|
||||
}
|
||||
// 检查文件是否存在
|
||||
exists, err := PathExists(filePath)
|
||||
exists, err := tool.PathExists(filePath)
|
||||
// 文件存在 进行断点续传
|
||||
if exists {
|
||||
fileInfo, _ := os.Stat(filePath)
|
||||
|
|
@ -86,15 +87,3 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
result2.HttpResult(r, w, nil, err)
|
||||
}
|
||||
}
|
||||
|
||||
// PathExists 判断文件夹是否存在
|
||||
func PathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package tool
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// DirSize 获取整体文件夹大小
|
||||
func GetDirSize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
||||
// PathExists 判断文件夹是否存在
|
||||
func PathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
Loading…
Reference in New Issue