voice.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package controllers
  2. import (
  3. "archive/zip"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/file"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hz_eta_api/models"
  8. "hongze/hz_eta_api/services"
  9. "hongze/hz_eta_api/utils"
  10. "io/ioutil"
  11. "os"
  12. "path"
  13. "time"
  14. )
  15. // VoiceController 音频
  16. type VoiceController struct {
  17. BaseAuthController
  18. }
  19. // Upload
  20. // @Title 音频上传
  21. // @Description 音频上传接口
  22. // @Param file query file true "文件"
  23. // @Param ReportId query int true "报告ID"
  24. // @Success Ret=200 上传成功
  25. // @router /upload [post]
  26. func (this *VoiceController) Upload() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. f, h, err := this.GetFile("file")
  33. if err != nil {
  34. br.Msg = "获取资源信息失败"
  35. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  36. return
  37. }
  38. reportId, err := this.GetInt("ReportId")
  39. if err != nil {
  40. br.Msg = "获取资源信息失败"
  41. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  42. return
  43. }
  44. report, err := models.GetReportItemById(reportId)
  45. if err != nil {
  46. br.Msg = "获取报告信息失败"
  47. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  48. return
  49. }
  50. ext := path.Ext(h.Filename)
  51. dateDir := time.Now().Format("20060102")
  52. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  53. err = os.MkdirAll(uploadDir, 777)
  54. if err != nil {
  55. br.Msg = "存储目录创建失败"
  56. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  57. return
  58. }
  59. randStr := utils.GetRandStringNoSpecialChar(28)
  60. fileName := randStr + ext
  61. fpath := uploadDir + "/" + fileName
  62. defer f.Close() //关闭上传文件
  63. err = this.SaveToFile("file", fpath)
  64. if err != nil {
  65. br.Msg = "文件上传失败"
  66. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  67. return
  68. }
  69. //上传到阿里云
  70. resourceUrl, err := services.UploadAudioAliyun(fileName, fpath)
  71. if err != nil {
  72. br.Msg = "文件上传失败"
  73. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  74. return
  75. }
  76. defer func() {
  77. os.Remove(fpath)
  78. }()
  79. item := new(models.Resource)
  80. item.ResourceUrl = resourceUrl
  81. item.ResourceType = 2
  82. item.CreateTime = time.Now()
  83. newId, err := models.AddResource(item)
  84. if err != nil {
  85. br.Msg = "资源上传失败"
  86. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  87. return
  88. }
  89. playSeconds, err := utils.GetVideoPlaySeconds(fpath)
  90. if err != nil {
  91. br.Msg = "获取音频时间失败"
  92. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  93. return
  94. }
  95. createTime := report.CreateTime.Format("0102")
  96. videoName := report.Title + "(" + createTime + ")"
  97. fileBody, err := ioutil.ReadFile(fpath)
  98. videoSize := len(fileBody)
  99. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  100. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  101. err = models.ModifyReportVideo(reportId, resourceUrl, videoName, sizeStr, playSeconds)
  102. if err != nil {
  103. br.Msg = "上传失败"
  104. br.ErrMsg = "上传失败,Err:" + err.Error()
  105. return
  106. }
  107. resp := new(models.ResourceResp)
  108. resp.Id = newId
  109. resp.ResourceUrl = resourceUrl
  110. br.Msg = "上传成功"
  111. br.Ret = 200
  112. br.Success = true
  113. br.Data = resp
  114. return
  115. }
  116. // VoiceCommonController 音频
  117. type VoiceCommonController struct {
  118. BaseCommonController
  119. }
  120. // Download
  121. // @Title 音频下载
  122. // @Description 音频下载接口
  123. // @Param ReportId query int true "报告ID"
  124. // @Success Ret=200 下载成功
  125. // @router /download [get]
  126. func (this *VoiceCommonController) Download() {
  127. br := new(models.BaseResponse).Init()
  128. defer func() {
  129. this.Data["json"] = br
  130. this.ServeJSON()
  131. }()
  132. reportId, err := this.GetInt("ReportId")
  133. if err != nil {
  134. br.Msg = "参数错误"
  135. br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  136. return
  137. }
  138. report, err := models.GetReportById(reportId)
  139. if err != nil {
  140. br.Msg = "获取信息失败"
  141. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  142. return
  143. }
  144. savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  145. fileBody, err := http.Get(report.VideoUrl)
  146. if err != nil {
  147. br.Msg = "获取信息失败"
  148. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  149. return
  150. }
  151. err = file.SaveFile(fileBody, savePath)
  152. if err != nil {
  153. br.Msg = "保存信息失败"
  154. br.ErrMsg = "保存信息失败,Err:" + err.Error()
  155. return
  156. }
  157. fileName := report.VideoName + ".mp3"
  158. this.Ctx.Output.Download(savePath, fileName)
  159. defer func() {
  160. os.Remove(savePath)
  161. }()
  162. br.Ret = 200
  163. br.Msg = "下载成功"
  164. br.Success = true
  165. return
  166. }
  167. // ReportChapterDownload
  168. // @Title 报告章节列表音频下载
  169. // @Description 音频下载接口
  170. // @Param ChapterIds query string true "章节IDs"
  171. // @Success Ret=200 下载成功
  172. // @router /report/download [get]
  173. func (this *VoiceCommonController) ReportChapterDownload() {
  174. br := new(models.BaseResponse).Init()
  175. defer func() {
  176. this.Data["json"] = br
  177. this.ServeJSON()
  178. }()
  179. chapterIds := this.GetString("ChapterIds")
  180. if chapterIds == "" {
  181. br.Msg = "参数错误"
  182. return
  183. }
  184. videoList, err := models.GetReportChapterVideoListByChapterIds(chapterIds)
  185. if err != nil {
  186. br.Msg = "获取音频列表失败"
  187. br.ErrMsg = "获取音频列表失败, Err: " + err.Error()
  188. return
  189. }
  190. if len(videoList) > 0 {
  191. reportId := videoList[0].ReportId
  192. reportInfo, err := models.GetReportById(reportId)
  193. if err != nil {
  194. br.Msg = "报告信息有误"
  195. br.ErrMsg = "获取报告信息失败, Err: " + err.Error()
  196. return
  197. }
  198. // 创建zip
  199. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  200. savePath := zipName
  201. zipFile, err := os.Create(zipName)
  202. if err != nil {
  203. return
  204. }
  205. zipWriter := zip.NewWriter(zipFile)
  206. // 生成zip过程中报错关闭
  207. defer func() {
  208. if err != nil {
  209. zipWriter.Close()
  210. zipFile.Close()
  211. }
  212. os.Remove(savePath)
  213. }()
  214. // 获取音频,写入zip
  215. for i := 0; i < len(videoList); i++ {
  216. item := videoList[i]
  217. if item.VideoName == "" || item.VideoUrl == "" {
  218. continue
  219. }
  220. // 音频文件后缀
  221. ext := path.Ext(item.VideoUrl)
  222. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  223. if err != nil {
  224. if os.IsPermission(err) {
  225. fmt.Println("权限不足: ", err)
  226. return
  227. }
  228. return
  229. }
  230. var content []byte
  231. content, err = http.Get(item.VideoUrl)
  232. if err != nil {
  233. content = []byte("")
  234. }
  235. ioWriter.Write(content)
  236. }
  237. // 生成zip后关闭,否则下载文件会损坏
  238. zipWriter.Close()
  239. zipFile.Close()
  240. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", reportInfo.Title))
  241. }
  242. br.Ret = 200
  243. br.Msg = "下载成功"
  244. br.Success = true
  245. return
  246. }