|
|
package file
|
|
|
|
|
|
import (
|
|
|
"goskeleton/app/global/consts"
|
|
|
"goskeleton/app/http/controller/web"
|
|
|
"goskeleton/app/http/validator/core/data_transfer"
|
|
|
"goskeleton/app/utils/response"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
)
|
|
|
|
|
|
type FileSave struct {
|
|
|
// 表单参数验证结构体支持匿名结构体嵌套
|
|
|
BaseField
|
|
|
FileUpload
|
|
|
}
|
|
|
|
|
|
// 验证器语法,参见 Register.go文件,有详细说明
|
|
|
|
|
|
func (f FileSave) CheckParams(context *gin.Context) {
|
|
|
|
|
|
//1.基本的验证规则没有通过
|
|
|
if err := context.ShouldBind(&f); err != nil {
|
|
|
response.ValidatorError(context, err)
|
|
|
return
|
|
|
}
|
|
|
// 尝试获取上传的文件,检查文件是否存在
|
|
|
//file, header, err := context.Request.FormFile("file")
|
|
|
//if err != nil {
|
|
|
// response.Fail(context, consts.CurdFileUploadErrorCode, consts.CurdFileUploadErrorMsg, "文件不存在或获取文件出错")
|
|
|
// return
|
|
|
//}
|
|
|
//defer file.Close()
|
|
|
|
|
|
// 对文件名后缀进行额外校验,确保是.docx文件(假设只保存该类型文件)
|
|
|
//if filepath.Ext(header.Filename) != ".docx" {
|
|
|
// response.Fail(context, consts.CurdFileTypeErrorCode, consts.CurdFileTypeErrorMsg, "只支持上传.docx文件")
|
|
|
// return
|
|
|
//}
|
|
|
|
|
|
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
|
|
|
extraAddBindDataContext := data_transfer.DataAddContext(f, consts.ValidatorPrefix, context)
|
|
|
if extraAddBindDataContext == nil {
|
|
|
response.ErrorSystem(context, "FileSave表单验证器json化失败", "")
|
|
|
} else {
|
|
|
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
|
|
(&web.File{}).Save(extraAddBindDataContext)
|
|
|
}
|
|
|
|
|
|
}
|