You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

package broker
import (
"base/app/common"
"base/app/config"
"base/app/lib/bean"
"base/app/logic/file"
"github.com/gin-gonic/gin"
"path/filepath"
"strings"
)
type Upload struct {
}
// Image @Title 图片上传
func (u *Upload) Image(c *gin.Context) {
upFile, err := c.FormFile("image")
if err != nil {
bean.Response.ResultFail(c, 10001, err.Error())
return
}
if upFile.Size > config.Config.Upload.MaxImage<<20 {
bean.Response.ResultFail(c, 10001, "文件太大")
return
}
ext := strings.ToLower(filepath.Ext(upFile.Filename))
if !common.InArray(ext, config.Config.Upload.ImageExts) {
bean.Response.ResultFail(c, 10001, "图片类型不支持")
return
}
path, err := file.UploadLogic.Upload(upFile, config.Config.Upload.ImagePath)
if err != nil {
bean.Response.ResultFail(c, 10001, err.Error())
return
}
bean.Response.ResultSuc(c, "上传成功", gin.H{
"path": path,
})
}
// File @Title 文件上传
func (u *Upload) File(c *gin.Context) {
upFile, err := c.FormFile("file")
if err != nil {
bean.Response.ResultFail(c, 10001, err.Error())
return
}
if upFile.Size > config.Config.Upload.MaxFile<<20 {
bean.Response.ResultFail(c, 10001, "文件太大")
return
}
ext := strings.ToLower(filepath.Ext(upFile.Filename))
if !common.InArray(ext, config.Config.Upload.FileExts) {
bean.Response.ResultFail(c, 10001, "图片类型不支持")
return
}
path, err := file.UploadLogic.Upload(upFile, config.Config.Upload.FilePath)
if err != nil {
bean.Response.ResultFail(c, 10001, err.Error())
return
}
bean.Response.ResultSuc(c, "上传成功", gin.H{
"path": path,
})
}