voice.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package controllers
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/file"
  4. "github.com/rdlucklib/rdluck_tools/http"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/services"
  7. "hongze/hz_crm_api/utils"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "time"
  12. )
  13. // 音频
  14. type VoiceController struct {
  15. BaseAuthController
  16. }
  17. // @Title 音频上传
  18. // @Description 音频上传接口
  19. // @Param file query file true "文件"
  20. // @Param ReportId query int true "报告ID"
  21. // @Success Ret=200 上传成功
  22. // @router /upload [post]
  23. func (this *VoiceController) Upload() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. f, h, err := this.GetFile("file")
  30. if err != nil {
  31. br.Msg = "获取资源信息失败"
  32. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  33. return
  34. }
  35. reportId, err := this.GetInt("ReportId")
  36. if err != nil {
  37. br.Msg = "获取资源信息失败"
  38. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  39. return
  40. }
  41. report, err := models.GetReportItemById(reportId)
  42. if err != nil {
  43. br.Msg = "获取报告信息失败"
  44. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  45. return
  46. }
  47. ext := path.Ext(h.Filename)
  48. dateDir := time.Now().Format("20060102")
  49. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  50. err = os.MkdirAll(uploadDir, 777)
  51. if err != nil {
  52. br.Msg = "存储目录创建失败"
  53. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  54. return
  55. }
  56. randStr := utils.GetRandStringNoSpecialChar(28)
  57. fileName := randStr + ext
  58. fpath := uploadDir + "/" + fileName
  59. defer f.Close() //关闭上传文件
  60. err = this.SaveToFile("file", fpath)
  61. if err != nil {
  62. br.Msg = "文件上传失败"
  63. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  64. return
  65. }
  66. //上传到阿里云
  67. resourceUrl, err := services.UploadAudioAliyun(fileName, fpath)
  68. if err != nil {
  69. br.Msg = "文件上传失败"
  70. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  71. return
  72. }
  73. defer func() {
  74. os.Remove(fpath)
  75. }()
  76. item := new(models.Resource)
  77. item.ResourceUrl = resourceUrl
  78. item.ResourceType = 2
  79. item.CreateTime = time.Now()
  80. newId, err := models.AddResource(item)
  81. if err != nil {
  82. br.Msg = "资源上传失败"
  83. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  84. return
  85. }
  86. playSeconds, err := utils.GetVideoPlaySeconds(fpath)
  87. if err != nil {
  88. br.Msg = "获取音频时间失败"
  89. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  90. return
  91. }
  92. createTime := report.CreateTime.Format("0102")
  93. videoName := report.Title + "(" + createTime + ")"
  94. fileBody, err := ioutil.ReadFile(fpath)
  95. videoSize := len(fileBody)
  96. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  97. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  98. err = models.ModifyReportVideo(reportId, resourceUrl, videoName, sizeStr, playSeconds)
  99. if err != nil {
  100. br.Msg = "上传失败"
  101. br.ErrMsg = "上传失败,Err:" + err.Error()
  102. return
  103. }
  104. resp := new(models.ResourceResp)
  105. resp.Id = newId
  106. resp.ResourceUrl = resourceUrl
  107. br.Msg = "上传成功"
  108. br.Ret = 200
  109. br.Success = true
  110. br.Data = resp
  111. return
  112. }
  113. // 音频
  114. type VoiceCommonController struct {
  115. BaseCommonController
  116. }
  117. // @Title 音频下载
  118. // @Description 音频下载接口
  119. // @Param ReportId query int true "报告ID"
  120. // @Success Ret=200 下载成功
  121. // @router /download [get]
  122. func (this *VoiceCommonController) Download() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. reportId, err := this.GetInt("ReportId")
  129. if err != nil {
  130. br.Msg = "参数错误"
  131. br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  132. return
  133. }
  134. report, err := models.GetReportById(reportId)
  135. if err != nil {
  136. br.Msg = "获取信息失败"
  137. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  138. return
  139. }
  140. savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  141. fileBody, err := http.Get(report.VideoUrl)
  142. if err != nil {
  143. br.Msg = "获取信息失败"
  144. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  145. return
  146. }
  147. err = file.SaveFile(fileBody, savePath)
  148. if err != nil {
  149. br.Msg = "保存信息失败"
  150. br.ErrMsg = "保存信息失败,Err:" + err.Error()
  151. return
  152. }
  153. fileName := report.VideoName + ".mp3"
  154. this.Ctx.Output.Download(savePath, fileName)
  155. defer func() {
  156. os.Remove(savePath)
  157. }()
  158. br.Ret = 200
  159. br.Msg = "下载成功"
  160. br.Success = true
  161. return
  162. }