package controllers import ( "eta/eta_forum_admin/models" "eta/eta_forum_admin/models/system" "eta/eta_forum_admin/services" "eta/eta_forum_admin/utils" "github.com/h2non/filetype" "io/ioutil" "os" "path" "time" ) type ResourceController struct { BaseAuthController } // @Title 图片上传 // @Description 图片上传接口 // @Param File query file true "文件" // @Success 200 新增成功 // @router /image/upload [post] func (this *ResourceController) ImageUpload() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } f, h, err := this.GetFile("file") if err != nil { br.Msg = "获取资源信息失败" br.ErrMsg = "获取资源信息失败,Err:" + err.Error() return } defer f.Close() //关闭上传文件 // 不依赖于文件扩展名检查文件格式 fileData, e := ioutil.ReadAll(f) if e != nil { br.Msg = "上传失败" br.ErrMsg = "读取文件失败, Err: " + e.Error() return } pass := filetype.IsImage(fileData) if !pass { br.Msg = "文件格式有误" br.ErrMsg = "文件格式有误" return } ext := path.Ext(h.Filename) dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "image/" + dateDir err = os.MkdirAll(uploadDir, utils.DIR_MOD) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName err = this.SaveToFile("file", fpath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } defer func() { os.Remove(fpath) }() // 上传文件 resourceUrl := `` ossClient := services.NewOssClient() if ossClient == nil { br.Msg = "上传失败" br.ErrMsg = "初始化OSS服务失败" return } // 上传到阿里云 ossDir := utils.UploadDir + "images/" savePath := ossDir + time.Now().Format("200601/20060102/") + fileName resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } resp := new(system.ResourceResp) resp.ResourceUrl = resourceUrl br.Msg = "上传成功" br.Ret = 200 br.Success = true br.Data = resp return } // @Title 获取STSToken // @Description 获取STSToken // @Success 200 获取成功 // @router /oss/get_sts_token [get] func (this *ResourceController) OssSTSToken() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ossClient := services.NewOssClient() if ossClient == nil { br.Msg = "上传失败" br.ErrMsg = "初始化OSS服务失败" return } resp, e := ossClient.GetUploadToken() if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取OSS上传Token失败, Err: " + e.Error() return } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true //source, _ := this.GetInt("StorageSource") // //if source == utils.STORAGESOURCE_OSS { // resp, err := services.GetOssSTSToken() // if err != nil { // br.Msg = "获取失败" // br.ErrMsg = "获取STSToken失败, Err: " + err.Error() // return // } // br.Data = resp // br.Msg = "获取成功" // br.Ret = 200 // br.Success = true //} else if source == utils.STORAGESOURCE_MINIO { // resp, err := services.GetMinIOSTSToken() // if err != nil { // br.Msg = "获取失败" // br.ErrMsg = "获取STSToken失败, Err: " + err.Error() // return // } // br.Data = resp // br.Msg = "获取成功" // br.Ret = 200 // br.Success = true //} }