resource.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package controllers
  2. import (
  3. resourceResp "hongze/hongze_mobile_admin/models/response/response"
  4. "hongze/hongze_mobile_admin/models/tables/resource"
  5. "hongze/hongze_mobile_admin/services"
  6. "hongze/hongze_mobile_admin/utils"
  7. "os"
  8. "path"
  9. "regexp"
  10. "strings"
  11. "time"
  12. )
  13. //资源管理-图片上传,合同上传等
  14. type ResourceCommon struct {
  15. BaseAuth
  16. }
  17. // @Title 图片上传
  18. // @Description 图片上传接口
  19. // @Param file query file true "文件"
  20. // @Success 200 新增成功
  21. // @router /upload [post]
  22. func (c *ResourceCommon) Upload() {
  23. f, h, err := c.GetFile("file")
  24. if err != nil {
  25. c.FailWithMessage("获取资源信息失败", "获取资源信息失败,Err:"+err.Error())
  26. return
  27. }
  28. ext := path.Ext(h.Filename)
  29. dateDir := time.Now().Format("20060102")
  30. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  31. err = os.MkdirAll(uploadDir, 0766)
  32. if err != nil {
  33. c.FailWithMessage("存储目录创建失败", "存储目录创建失败,Err:"+err.Error())
  34. return
  35. }
  36. randStr := utils.GetRandStringNoSpecialChar(28)
  37. fileName := randStr + ext
  38. fpath := uploadDir + "/" + fileName
  39. defer f.Close() //关闭上传文件
  40. err = c.SaveToFile("file", fpath)
  41. if err != nil {
  42. c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error())
  43. return
  44. }
  45. //上传到阿里云
  46. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  47. if err != nil {
  48. c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error())
  49. return
  50. }
  51. defer func() {
  52. os.Remove(fpath)
  53. }()
  54. item := new(resource.Resource)
  55. item.ResourceUrl = resourceUrl
  56. item.ResourceType = 1
  57. item.CreateTime = time.Now()
  58. newId, err := resource.AddResource(item)
  59. if err != nil {
  60. c.FailWithMessage("资源上传失败", "资源上传失败,Err:"+err.Error())
  61. return
  62. }
  63. c.OkDetailed(resourceResp.ResourceResp{
  64. Id: newId,
  65. ResourceUrl: resourceUrl,
  66. }, "上传成功")
  67. return
  68. }
  69. // @Title 上传图片
  70. // @Description 上传图片
  71. // @Param Image query string false "图片,base64字符串"
  72. // @Success 200 {object} models.ImageResponse
  73. // @router /upload_image_base64 [post]
  74. func (c *ResourceCommon) UploadImageBase64() {
  75. var err error
  76. ext := ".png"
  77. uploadDir := "./static"
  78. randStr := utils.GetRandStringNoSpecialChar(28)
  79. fileName := randStr + ext
  80. fpath := uploadDir + "/" + fileName
  81. image := c.Ctx.Request.FormValue("Image")
  82. if image == "" {
  83. c.FailWithMessage("参数错误", "图片内容不能为空")
  84. return
  85. }
  86. b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, image)
  87. if !b {
  88. c.FailWithMessage("图片格式不正确", "图片格式不正确")
  89. return
  90. }
  91. re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
  92. base64Str := re.ReplaceAllString(image, "")
  93. base64Str = strings.Replace(base64Str, " ", "", -1)
  94. //imgByte, err := base64.StdEncoding.DecodeString(base64Str)
  95. //err=ioutil.WriteFile(fpath,imgByte,os.ModeAppend)
  96. //if err!=nil {
  97. // br.Msg = "图片保存失败"
  98. // br.ErrMsg = "图片保存失败,Err:"+err.Error()
  99. // return
  100. //}
  101. err = utils.SaveBase64ToFile(base64Str, fpath)
  102. if err != nil {
  103. c.FailWithMessage("图片保存失败", "图片保存失败,Err:"+err.Error())
  104. return
  105. }
  106. defer os.Remove(fpath)
  107. savePath := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  108. savePath += fileName
  109. //上传到阿里云
  110. err = services.UploadVideoAliyun(fileName, fpath, savePath)
  111. if err != nil {
  112. c.FailWithMessage("文件上传失败", "文件上传失败,Err:"+err.Error())
  113. return
  114. }
  115. utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
  116. utils.FileLog.Info("end update oss ")
  117. resourceUrl := utils.Imghost + savePath
  118. item := new(resource.Resource)
  119. item.ResourceUrl = resourceUrl
  120. item.ResourceType = 1
  121. item.CreateTime = time.Now()
  122. newId, err := resource.AddResource(item)
  123. if err != nil {
  124. c.FailWithMessage("资源上传失败", "资源上传失败,Err:"+err.Error())
  125. return
  126. }
  127. utils.FileLog.Info("end VideoUpload ")
  128. utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
  129. c.OkDetailed(
  130. resourceResp.ResourceResp{
  131. Id: newId,
  132. ResourceUrl: resourceUrl,
  133. }, "上传成功")
  134. }
  135. //判断文件是否存在
  136. func IsFileExist(filename string) bool {
  137. _, err := os.Stat(filename)
  138. if os.IsNotExist(err) {
  139. return false
  140. }
  141. return true
  142. }