voice.go 7.3 KB

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