voice.go 9.3 KB

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