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, }) }