Updata:增加分片上传测试接口
This commit is contained in:
parent
c54aae2daf
commit
ef07239dc6
@ -171,6 +171,7 @@ func NewRouter() *gin.Engine {
|
|||||||
resourceRoute.POST("/upload", file.Upload)
|
resourceRoute.POST("/upload", file.Upload)
|
||||||
resourceRoute.POST("/tus/create", file.TusCreate)
|
resourceRoute.POST("/tus/create", file.TusCreate)
|
||||||
resourceRoute.POST("/tus/upload", file.TusUpload)
|
resourceRoute.POST("/tus/upload", file.TusUpload)
|
||||||
|
resourceRoute.POST("/tus/upload/mp4", file.TusUploadMP4)
|
||||||
v1.Group("/resource").GET("/raw/*path", file.Raw)
|
v1.Group("/resource").GET("/raw/*path", file.Raw)
|
||||||
resourceRoute.GET("/dir/raw", file.DirDownload)
|
resourceRoute.GET("/dir/raw", file.DirDownload)
|
||||||
resourceRoute.GET("/preview/:size/*path", file.Preview)
|
resourceRoute.GET("/preview/:size/*path", file.Preview)
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -238,6 +239,85 @@ func TusUpload(ctx *gin.Context) {
|
|||||||
service.Success(ctx, nil)
|
service.Success(ctx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TusUploadMP4(ctx *gin.Context) {
|
||||||
|
//读取传入的本地路径文件
|
||||||
|
var req files.TusCreateReq
|
||||||
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
|
service.Error(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userPath := req.UserSpacePath
|
||||||
|
req.UserSpacePath = getUserSpacePath(ctx)
|
||||||
|
fileInfo, err := os.Stat(userPath)
|
||||||
|
if err != nil {
|
||||||
|
service.Error(ctx, errors.New(common.GetFileInfoFailed))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fileInfo.IsDir() {
|
||||||
|
service.Error(ctx, errors.New("localPath 不能是目录"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
totalSize := fileInfo.Size()
|
||||||
|
if totalSize <= 0 {
|
||||||
|
service.Error(ctx, errors.New("文件为空"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//如果是.mp4文件则判断文件大小
|
||||||
|
if strings.HasSuffix(strings.ToLower(req.Path), ".mp4") && fileInfo.Size() > 1024*1024 {
|
||||||
|
//大于1mb的文件进行分块上传
|
||||||
|
f, err := os.Open(userPath)
|
||||||
|
if err != nil {
|
||||||
|
service.Error(ctx, errors.New(common.ERROR_OPEN_FILE))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
const chunkSize int64 = 2*1024*1024 - 100
|
||||||
|
|
||||||
|
for offset := int64(0); offset < totalSize; {
|
||||||
|
end := offset + chunkSize
|
||||||
|
if end > totalSize {
|
||||||
|
end = totalSize
|
||||||
|
}
|
||||||
|
n := end - offset
|
||||||
|
buf := make([]byte, n)
|
||||||
|
// 用 ReadAt 读指定偏移的分片
|
||||||
|
readN, readErr := f.ReadAt(buf, offset)
|
||||||
|
if readErr != nil && readErr != io.EOF {
|
||||||
|
service.Error(ctx, errors.New(common.ERROR_OPEN_FILE))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if int64(readN) != n {
|
||||||
|
service.Error(ctx, errors.New("读取分片长度不一致"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 4) 首包 MP4 机器码校验(offset==0)
|
||||||
|
if offset == 0 {
|
||||||
|
if !isLikelyMP4(buf) {
|
||||||
|
service.Error(ctx, errors.New("mp4机器码校验失败,该文件可能不是标准 mp4"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 5) TusUpload 写入当前分片
|
||||||
|
_, err = service.FilesProvider.TusUpload(ctx, &files.TusUploadReq{
|
||||||
|
Path: req.Path,
|
||||||
|
UploadOffset: offset,
|
||||||
|
Content: buf,
|
||||||
|
UserSpacePath: req.UserSpacePath,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
service.Error(ctx, errors.New(common.TusUploadFailed))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
offset = end
|
||||||
|
}
|
||||||
|
service.Success(ctx, gin.H{
|
||||||
|
"destPath": req.Path,
|
||||||
|
"localPath": userPath,
|
||||||
|
"uploadLength": totalSize,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Preview(ctx *gin.Context) {
|
func Preview(ctx *gin.Context) {
|
||||||
var size int
|
var size int
|
||||||
size, err := strconv.Atoi(ctx.Param("size"))
|
size, err := strconv.Atoi(ctx.Param("size"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user