|
|
|
|
@@ -82,39 +82,41 @@ func (s *FileController) Download(c *gin.Context) {
|
|
|
|
|
// @Description Upload a file, interface param use <fileName>
|
|
|
|
|
// @Router /file/upload [post]
|
|
|
|
|
func (s *FileController) Upload(c *gin.Context) {
|
|
|
|
|
language := reqctx.AcceptLanguage(c)
|
|
|
|
|
// 上传的文件
|
|
|
|
|
formFile, err := c.FormFile("file")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
c.JSON(400, resp.CodeMsg(40010, "bind err: file is empty"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 子路径
|
|
|
|
|
// 子路径需要在指定范围内
|
|
|
|
|
subPath := c.PostForm("subPath")
|
|
|
|
|
if _, ok := constants.UPLOAD_SUB_PATH[subPath]; !ok {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
_, ok := constants.UPLOAD_SUB_PATH[subPath]
|
|
|
|
|
if subPath != "" && !ok {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(40010, "bind err: subPath not in range"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if subPath == "" {
|
|
|
|
|
subPath = constants.UPLOAD_COMMON
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上传文件转存
|
|
|
|
|
upFilePath, err := file.TransferUploadFile(formFile, subPath, nil)
|
|
|
|
|
uploadFilePath, err := file.TransferUploadFile(formFile, subPath, []string{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newFileName := upFilePath[strings.LastIndex(upFilePath, "/")+1:]
|
|
|
|
|
c.JSON(200, resp.OkData(map[string]string{
|
|
|
|
|
"url": "//" + c.Request.Host + upFilePath,
|
|
|
|
|
"fileName": upFilePath,
|
|
|
|
|
"newFileName": newFileName,
|
|
|
|
|
"url": "//" + c.Request.Host + uploadFilePath,
|
|
|
|
|
"filePath": uploadFilePath,
|
|
|
|
|
"newFileName": filepath.Base(uploadFilePath),
|
|
|
|
|
"originalFileName": formFile.Filename,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 切片文件检查
|
|
|
|
|
//
|
|
|
|
|
// POST /chunkCheck
|
|
|
|
|
// POST /chunk-check
|
|
|
|
|
//
|
|
|
|
|
// @Tags common/file
|
|
|
|
|
// @Accept json
|
|
|
|
|
@@ -124,18 +126,15 @@ func (s *FileController) Upload(c *gin.Context) {
|
|
|
|
|
// @Security TokenAuth
|
|
|
|
|
// @Summary Slice file checking
|
|
|
|
|
// @Description Slice file checking
|
|
|
|
|
// @Router /file/chunkCheck [post]
|
|
|
|
|
// @Router /file/chunk-check [post]
|
|
|
|
|
func (s *FileController) ChunkCheck(c *gin.Context) {
|
|
|
|
|
language := reqctx.AcceptLanguage(c)
|
|
|
|
|
var body struct {
|
|
|
|
|
// 唯一标识
|
|
|
|
|
Identifier string `json:"identifier" binding:"required"`
|
|
|
|
|
// 文件名
|
|
|
|
|
FileName string `json:"fileName" binding:"required"`
|
|
|
|
|
Identifier string `json:"identifier" binding:"required"` // 唯一标识
|
|
|
|
|
FileName string `json:"fileName" binding:"required"` // 文件名
|
|
|
|
|
}
|
|
|
|
|
err := c.ShouldBindJSON(&body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
|
|
|
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
|
|
|
|
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -150,7 +149,7 @@ func (s *FileController) ChunkCheck(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
// 切片文件合并
|
|
|
|
|
//
|
|
|
|
|
// POST /chunkMerge
|
|
|
|
|
// POST /chunk-merge
|
|
|
|
|
//
|
|
|
|
|
// @Tags common/file
|
|
|
|
|
// @Accept json
|
|
|
|
|
@@ -160,26 +159,26 @@ func (s *FileController) ChunkCheck(c *gin.Context) {
|
|
|
|
|
// @Security TokenAuth
|
|
|
|
|
// @Summary Slice file merge
|
|
|
|
|
// @Description Slice file merge
|
|
|
|
|
// @Router /file/chunkMerge [post]
|
|
|
|
|
// @Router /file/chunk-merge [post]
|
|
|
|
|
func (s *FileController) ChunkMerge(c *gin.Context) {
|
|
|
|
|
language := reqctx.AcceptLanguage(c)
|
|
|
|
|
var body struct {
|
|
|
|
|
// 唯一标识
|
|
|
|
|
Identifier string `json:"identifier" binding:"required"`
|
|
|
|
|
// 文件名
|
|
|
|
|
FileName string `json:"fileName" binding:"required"`
|
|
|
|
|
// 子路径类型
|
|
|
|
|
SubPath string `json:"subPath" binding:"required"`
|
|
|
|
|
Identifier string `json:"identifier" binding:"required"` // 唯一标识
|
|
|
|
|
FileName string `json:"fileName" binding:"required"` // 文件名
|
|
|
|
|
SubPath string `json:"subPath"` // 子路径类型
|
|
|
|
|
}
|
|
|
|
|
err := c.ShouldBindJSON(&body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
|
|
|
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
|
|
|
|
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if _, ok := constants.UPLOAD_SUB_PATH[body.SubPath]; !ok {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
// 子路径需要在指定范围内
|
|
|
|
|
if _, ok := constants.UPLOAD_SUB_PATH[body.SubPath]; body.SubPath != "" && !ok {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(40010, "bind err: subPath not in range"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if body.SubPath == "" {
|
|
|
|
|
body.SubPath = constants.UPLOAD_COMMON
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 切片文件合并
|
|
|
|
|
mergeFilePath, err := file.ChunkMergeFile(body.Identifier, body.FileName, body.SubPath)
|
|
|
|
|
@@ -188,18 +187,17 @@ func (s *FileController) ChunkMerge(c *gin.Context) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newFileName := mergeFilePath[strings.LastIndex(mergeFilePath, "/")+1:]
|
|
|
|
|
c.JSON(200, resp.OkData(map[string]string{
|
|
|
|
|
"url": "//" + c.Request.Host + mergeFilePath,
|
|
|
|
|
"fileName": mergeFilePath,
|
|
|
|
|
"newFileName": newFileName,
|
|
|
|
|
"filePath": mergeFilePath,
|
|
|
|
|
"newFileName": filepath.Base(mergeFilePath),
|
|
|
|
|
"originalFileName": body.FileName,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 切片文件上传
|
|
|
|
|
//
|
|
|
|
|
// POST /chunkUpload
|
|
|
|
|
// POST /chunk-upload
|
|
|
|
|
//
|
|
|
|
|
// @Tags common/file
|
|
|
|
|
// @Accept multipart/form-data
|
|
|
|
|
@@ -211,17 +209,20 @@ func (s *FileController) ChunkMerge(c *gin.Context) {
|
|
|
|
|
// @Security TokenAuth
|
|
|
|
|
// @Summary Sliced file upload
|
|
|
|
|
// @Description Sliced file upload
|
|
|
|
|
// @Router /file/chunkUpload [post]
|
|
|
|
|
// @Router /file/chunk-upload [post]
|
|
|
|
|
func (s *FileController) ChunkUpload(c *gin.Context) {
|
|
|
|
|
language := reqctx.AcceptLanguage(c)
|
|
|
|
|
// 切片编号
|
|
|
|
|
index := c.PostForm("index")
|
|
|
|
|
// 切片唯一标识
|
|
|
|
|
identifier := c.PostForm("identifier")
|
|
|
|
|
if index == "" || identifier == "" {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(40010, "bind err: index and identifier must be set"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 上传的文件
|
|
|
|
|
formFile, err := c.FormFile("file")
|
|
|
|
|
if index == "" || identifier == "" || err != nil {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, resp.CodeMsg(40010, "bind err: file is empty"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -236,7 +237,7 @@ func (s *FileController) ChunkUpload(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
// 转存指定对应文件到静态目录
|
|
|
|
|
//
|
|
|
|
|
// POST /transferStaticFile
|
|
|
|
|
// POST /transfer-static-file
|
|
|
|
|
func (s *FileController) TransferStaticFile(c *gin.Context) {
|
|
|
|
|
var body struct {
|
|
|
|
|
UploadPath string `json:"uploadPath" binding:"required"`
|
|
|
|
|
|