105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package image
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/repository/result"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils/fileutils"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"syscall"
|
|
)
|
|
|
|
type LoadBody struct {
|
|
Stream string `json:"stream"`
|
|
}
|
|
|
|
var uploadTempPath = filepath.Join(uploadPath, "temp")
|
|
|
|
func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
file, fileHeader, err := r.FormFile("file")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
result.HttpResult(r, w, nil, err)
|
|
return
|
|
}
|
|
index := r.PostFormValue("index")
|
|
hash := r.PostFormValue("hash")
|
|
|
|
defer file.Close()
|
|
// 合并路径
|
|
chunksPath := filepath.Join(uploadTempPath, hash)
|
|
// 文件路径
|
|
filePath := filepath.Join(chunksPath, hash+"-"+index)
|
|
// 检查临时文件夹是否存在 不存在则创建文件夹
|
|
isPathExists, err := fileutils.PathExists(chunksPath)
|
|
if !isPathExists {
|
|
err = os.MkdirAll(chunksPath, os.ModePerm)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
result.HttpResult(r, w, nil, err)
|
|
return
|
|
}
|
|
}
|
|
// 检查文件是否存在
|
|
exists, err := fileutils.PathExists(filePath)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
result.HttpResult(r, w, nil, err)
|
|
return
|
|
}
|
|
// 文件存在 进行断点续传
|
|
if exists {
|
|
fileInfo, _ := os.Stat(filePath)
|
|
if fileInfo.Size() == fileHeader.Size {
|
|
result.HttpResult(r, w, nil, err)
|
|
return
|
|
}
|
|
start := strconv.Itoa(int(fileInfo.Size()))
|
|
oldFile, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
|
defer oldFile.Close()
|
|
count, _ := strconv.ParseInt(start, 10, 64)
|
|
fmt.Println("已上传:", count)
|
|
// 设置读,写的偏移量
|
|
file.Seek(count, 0)
|
|
oldFile.Seek(count, 0)
|
|
data := make([]byte, 1024, 1024)
|
|
for {
|
|
total, err := file.Read(data)
|
|
if err == io.EOF {
|
|
fmt.Println("文件复制完毕")
|
|
break
|
|
}
|
|
oldFile.Write(data[:total])
|
|
|
|
}
|
|
// 文件不存在 直接上传
|
|
} else {
|
|
destFile, _ := os.OpenFile(filepath.Join(chunksPath, hash+"-"+index), syscall.O_CREAT|syscall.O_WRONLY, 0777)
|
|
reader := bufio.NewReader(file)
|
|
writer := bufio.NewWriter(destFile)
|
|
buf := make([]byte, 1024*1024) // 1M buf
|
|
for {
|
|
n, err := reader.Read(buf)
|
|
if err == io.EOF {
|
|
writer.Flush()
|
|
break
|
|
} else if err != nil {
|
|
return
|
|
} else {
|
|
writer.Write(buf[:n])
|
|
}
|
|
}
|
|
defer file.Close()
|
|
defer destFile.Close()
|
|
}
|
|
result.HttpResult(r, w, nil, err)
|
|
}
|
|
}
|