package controllers import ( resourceResp "hongze/hongze_mobile_admin/models/response/response" "hongze/hongze_mobile_admin/models/tables/resource" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/utils" "os" "path" "regexp" "strings" "time" ) //资源管理-图片上传,合同上传等 type ResourceCommon struct { BaseAuth } // @Title 图片上传 // @Description 图片上传接口 // @Param file query file true "文件" // @Success 200 新增成功 // @router /upload [post] func (c *ResourceCommon) Upload() { f, h, err := c.GetFile("file") if err != nil { c.FailWithMessage("获取资源信息失败", "获取资源信息失败,Err:"+err.Error()) return } ext := path.Ext(h.Filename) dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "hongze/" + dateDir err = os.MkdirAll(uploadDir, 0766) if err != nil { c.FailWithMessage("存储目录创建失败", "存储目录创建失败,Err:"+err.Error()) return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName defer f.Close() //关闭上传文件 err = c.SaveToFile("file", fpath) if err != nil { c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error()) return } //上传到阿里云 resourceUrl, err := services.UploadAliyun(fileName, fpath) if err != nil { c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error()) return } defer func() { os.Remove(fpath) }() item := new(resource.Resource) item.ResourceUrl = resourceUrl item.ResourceType = 1 item.CreateTime = time.Now() newId, err := resource.AddResource(item) if err != nil { c.FailWithMessage("资源上传失败", "资源上传失败,Err:"+err.Error()) return } c.OkDetailed(resourceResp.ResourceResp{ Id: newId, ResourceUrl: resourceUrl, }, "上传成功") return } // @Title 上传图片 // @Description 上传图片 // @Param Image query string false "图片,base64字符串" // @Success 200 {object} models.ImageResponse // @router /upload_image_base64 [post] func (c *ResourceCommon) UploadImageBase64() { var err error ext := ".png" uploadDir := "./static" randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName image := c.Ctx.Request.FormValue("Image") if image == "" { c.FailWithMessage("参数错误", "图片内容不能为空") return } b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, image) if !b { c.FailWithMessage("图片格式不正确", "图片格式不正确") return } re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`) base64Str := re.ReplaceAllString(image, "") base64Str = strings.Replace(base64Str, " ", "", -1) //imgByte, err := base64.StdEncoding.DecodeString(base64Str) //err=ioutil.WriteFile(fpath,imgByte,os.ModeAppend) //if err!=nil { // br.Msg = "图片保存失败" // br.ErrMsg = "图片保存失败,Err:"+err.Error() // return //} err = utils.SaveBase64ToFile(base64Str, fpath) if err != nil { c.FailWithMessage("图片保存失败", "图片保存失败,Err:"+err.Error()) return } defer os.Remove(fpath) savePath := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/") savePath += fileName //上传到阿里云 err = services.UploadVideoAliyun(fileName, fpath, savePath) if err != nil { c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error()) return } utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime)) utils.FileLog.Info("end update oss ") resourceUrl := utils.Imghost + savePath item := new(resource.Resource) item.ResourceUrl = resourceUrl item.ResourceType = 1 item.CreateTime = time.Now() newId, err := resource.AddResource(item) if err != nil { c.FailWithMessage("资源上传失败", "资源上传失败,Err:"+err.Error()) return } utils.FileLog.Info("end VideoUpload ") utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime)) c.OkDetailed( resourceResp.ResourceResp{ Id: newId, ResourceUrl: resourceUrl, }, "上传成功") } //判断文件是否存在 func IsFileExist(filename string) bool { _, err := os.Stat(filename) if os.IsNotExist(err) { return false } return true }