mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-06 00:27:00 +08:00
121 lines
4.0 KiB
Go
121 lines
4.0 KiB
Go
package commonController
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"x_admin/core/response"
|
|
"x_admin/service/commonService"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func UploadChunkRoute(rg *gin.RouterGroup) {
|
|
handle := uploadChunkHandler{
|
|
uploadPath: "./uploads",
|
|
tmpPath: "./uploads/.tmp",
|
|
}
|
|
os.MkdirAll(handle.uploadPath, 0755)
|
|
os.MkdirAll(handle.tmpPath, 0755)
|
|
|
|
rg = rg.Group("/common")
|
|
rg.GET("/uploadChunk/CheckFileExist", handle.CheckFileExist)
|
|
// rg.GET("/uploadChunk/CheckChunkExist", handle.CheckChunkExist)
|
|
rg.GET("/uploadChunk/HasChunk", handle.HasChunk)
|
|
|
|
rg.POST("/uploadChunk/UploadChunk", handle.UploadChunk)
|
|
rg.POST("/uploadChunk/MergeChunk", handle.MergeChunk)
|
|
}
|
|
|
|
type uploadChunkHandler struct {
|
|
uploadPath string
|
|
tmpPath string
|
|
}
|
|
|
|
func (uh uploadChunkHandler) getFilePath(fileMd5 string) string {
|
|
return fmt.Sprintf("%s/%s", uh.uploadPath, fileMd5)
|
|
}
|
|
func (uh uploadChunkHandler) getChunkDir(fileMd5 string, chunkSize string) string {
|
|
return fmt.Sprintf("%s/%s_%s", uh.tmpPath, fileMd5, chunkSize)
|
|
}
|
|
func (uh uploadChunkHandler) getChunkPath(fileMd5 string, chunkSize string, index string) string {
|
|
return fmt.Sprintf("%s/%s_%s/%s", uh.tmpPath, fileMd5, chunkSize, index)
|
|
}
|
|
|
|
func (uh uploadChunkHandler) CheckFileExist(c *gin.Context) {
|
|
var fileMd5 = c.Query("fileMd5")
|
|
|
|
// var fileName = url.QueryEscape(c.Query("fileName"))
|
|
// 正则检查MD5
|
|
// reg := regexp.MustCompile(`^[a-fA-F0-9_]{32+}$`)
|
|
// if !reg.MatchString(fileMd5) {
|
|
// response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
|
// return
|
|
// }
|
|
var filePath = fmt.Sprintf("%s/%s", uh.uploadPath, fileMd5)
|
|
// 检查文件是否存在
|
|
if commonService.UploadChunkService.CheckFileExist(filePath) {
|
|
response.OkWithData(c, filePath)
|
|
return
|
|
}
|
|
response.OkWithData(c, nil)
|
|
}
|
|
func (uh uploadChunkHandler) HasChunk(c *gin.Context) {
|
|
var fileMd5 = c.Query("fileMd5")
|
|
var chunkSize = c.Query("chunkSize")
|
|
var chunkDir = uh.getChunkDir(fileMd5, chunkSize)
|
|
var HasChunk = commonService.UploadChunkService.HasChunk(chunkDir)
|
|
response.OkWithData(c, HasChunk)
|
|
}
|
|
|
|
// 检查chunk是否存在
|
|
// func (uh uploadChunkHandler) CheckChunkExist(c *gin.Context) {
|
|
// fileMd5 := c.Query("fileMd5") // 上传文件的md5
|
|
// chunkSize := c.Query("chunkSize")
|
|
// index := c.Query("index") // 分片序号
|
|
// // chunkPath := fmt.Sprintf("%s/%s/%s", uh.tmpPath, fileMd5, index)
|
|
// chunkPath := uh.getChunkPath(fileMd5, chunkSize, index)
|
|
// if commonService.UploadChunkService.CheckFileExist(chunkPath) {
|
|
// response.OkWithData(c, 1)
|
|
// return
|
|
// }
|
|
// response.OkWithData(c, 0)
|
|
// }
|
|
|
|
// UploadChunk 上传分片
|
|
func (uh uploadChunkHandler) UploadChunk(c *gin.Context) {
|
|
chunk, _ := c.FormFile("chunk") // 分片文件
|
|
chunkSize := c.PostForm("chunkSize") // 分片分割的大小
|
|
index := c.PostForm("index") // 分片序号
|
|
fileMd5 := c.PostForm("fileMd5") // 上传文件的md5
|
|
|
|
chunkDir := uh.getChunkDir(fileMd5, chunkSize)
|
|
chunkPath := uh.getChunkPath(fileMd5, chunkSize, index)
|
|
err := commonService.UploadChunkService.UploadChunk(chunkDir, chunkPath, chunk)
|
|
if err != nil {
|
|
response.FailWithMsg(c, response.SystemError, err.Error())
|
|
return
|
|
}
|
|
response.Ok(c)
|
|
}
|
|
func (uh uploadChunkHandler) MergeChunk(c *gin.Context) {
|
|
var MergeChunk struct {
|
|
FileMd5 string `json:"fileMd5"` // 上传文件的md5
|
|
FileName string `json:"fileName"` // 文件名
|
|
ChunkCount int `json:"chunkCount"` // 分片数量
|
|
ChunkSize int `json:"chunkSize"` // 分片分割的大小,作用:确保不同分片大小不放在同一目录
|
|
}
|
|
bindErr := c.ShouldBindJSON(&MergeChunk)
|
|
if bindErr != nil {
|
|
response.FailWithMsg(c, response.SystemError, bindErr.Error())
|
|
return
|
|
}
|
|
var filePath = uh.getFilePath(MergeChunk.FileMd5)
|
|
var chunkDir = uh.getChunkDir(MergeChunk.FileMd5, fmt.Sprintf("%d", MergeChunk.ChunkSize))
|
|
err := commonService.UploadChunkService.MergeChunk(chunkDir, filePath, MergeChunk.ChunkCount)
|
|
if err != nil {
|
|
response.FailWithMsg(c, response.SystemError, err.Error())
|
|
return
|
|
}
|
|
response.OkWithData(c, filePath)
|
|
}
|