voice.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package controllers
  2. import (
  3. "archive/zip"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services"
  6. "eta_gn/eta_api/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. createTimeStr := reportInfo.CreateTime.Format("0102")
  129. videoName := reportInfo.Title + "(" + createTimeStr + ")"
  130. reportInfo.VideoUrl = resourceUrl
  131. reportInfo.VideoName = videoName
  132. reportInfo.VideoPlaySeconds = fmt.Sprint(playSeconds)
  133. reportInfo.VideoSize = sizeStr
  134. reportInfo.LastModifyAdminId = this.SysUser.AdminId
  135. reportInfo.LastModifyAdminName = this.SysUser.RealName
  136. reportInfo.VoiceGenerateType = 1
  137. reportInfo.ModifyTime = time.Now()
  138. err = reportInfo.UpdateReport([]string{"VideoUrl", "VideoName", "VideoPlaySeconds", "VideoSize", "LastModifyAdminId", "LastModifyAdminName", "VoiceGenerateType", "ModifyTime"})
  139. if err != nil {
  140. br.Msg = "上传失败"
  141. br.ErrMsg = "修改报告的音频信息失败,Err:" + err.Error()
  142. return
  143. }
  144. }
  145. resp := new(models.ResourceResp)
  146. resp.Id = newId
  147. resp.ResourceUrl = resourceUrl
  148. br.Msg = "上传成功"
  149. br.Ret = 200
  150. br.Success = true
  151. br.Data = resp
  152. return
  153. }
  154. // VoiceCommonController 音频
  155. type VoiceCommonController struct {
  156. BaseCommonController
  157. }
  158. // Download
  159. // @Title 音频下载
  160. // @Description 音频下载接口
  161. // @Param ReportId query int true "报告ID"
  162. // @Success Ret=200 下载成功
  163. // @router /download [get]
  164. func (this *VoiceCommonController) Download() {
  165. br := new(models.BaseResponse).Init()
  166. defer func() {
  167. this.Data["json"] = br
  168. this.ServeJSON()
  169. }()
  170. reportId, err := this.GetInt("ReportId")
  171. if err != nil {
  172. br.Msg = "参数错误"
  173. br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  174. return
  175. }
  176. reportInfo, err := models.GetReportById(reportId)
  177. if err != nil {
  178. br.Msg = "获取信息失败"
  179. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  180. return
  181. }
  182. savePath, fileName, err, errMsg := services.DownloadVoice(reportInfo)
  183. // 如果生成了文件,那么就在退出的时候,删除该文件
  184. if savePath != `` {
  185. defer func() {
  186. os.Remove(savePath)
  187. }()
  188. }
  189. if err != nil {
  190. br.Msg = errMsg
  191. br.ErrMsg = "下载失败,Err:" + err.Error()
  192. return
  193. }
  194. if savePath != `` {
  195. this.Ctx.Output.Download(savePath, fileName)
  196. }
  197. br.Ret = 200
  198. br.Msg = "下载成功"
  199. br.Success = true
  200. return
  201. }
  202. //func (this *VoiceCommonController) Download() {
  203. // br := new(models.BaseResponse).Init()
  204. // defer func() {
  205. // this.Data["json"] = br
  206. // this.ServeJSON()
  207. // }()
  208. // reportId, err := this.GetInt("ReportId")
  209. // if err != nil {
  210. // br.Msg = "参数错误"
  211. // br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  212. // return
  213. // }
  214. // report, err := models.GetReportById(reportId)
  215. // if err != nil {
  216. // br.Msg = "获取信息失败"
  217. // br.ErrMsg = "获取信息失败,Err:" + err.Error()
  218. // return
  219. // }
  220. // savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  221. // fileBody, err := http.Get(report.VideoUrl)
  222. // if err != nil {
  223. // br.Msg = "获取信息失败"
  224. // br.ErrMsg = "获取信息失败,Err:" + err.Error()
  225. // return
  226. // }
  227. // err = file.SaveFile(fileBody, savePath)
  228. // if err != nil {
  229. // br.Msg = "保存信息失败"
  230. // br.ErrMsg = "保存信息失败,Err:" + err.Error()
  231. // return
  232. // }
  233. // fileName := report.VideoName + ".mp3"
  234. // this.Ctx.Output.Download(savePath, fileName)
  235. // defer func() {
  236. // os.Remove(savePath)
  237. // }()
  238. // br.Ret = 200
  239. // br.Msg = "下载成功"
  240. // br.Success = true
  241. // return
  242. //}
  243. // ReportChapterDownload
  244. // @Title 报告章节列表音频下载
  245. // @Description 音频下载接口
  246. // @Param ChapterIds query string true "章节IDs"
  247. // @Success Ret=200 下载成功
  248. // @router /report/download [get]
  249. func (this *VoiceCommonController) ReportChapterDownload() {
  250. br := new(models.BaseResponse).Init()
  251. defer func() {
  252. this.Data["json"] = br
  253. this.ServeJSON()
  254. }()
  255. strChapterIds := this.GetString("ChapterIds")
  256. if strChapterIds == "" {
  257. br.Msg = "参数错误"
  258. return
  259. }
  260. chapterIds := make([]int, 0)
  261. chapterIdArr := strings.Split(strChapterIds, ",")
  262. for _, v := range chapterIdArr {
  263. id, e := strconv.Atoi(v)
  264. if e != nil {
  265. br.Msg = "参数有误"
  266. br.ErrMsg = "章节ID有误, Err: " + e.Error()
  267. return
  268. }
  269. chapterIds = append(chapterIds, id)
  270. }
  271. if len(chapterIds) == 0 {
  272. br.Msg = "参数有误"
  273. return
  274. }
  275. videoList, err := models.GetReportChapterVideoListByChapterIds(chapterIds)
  276. if err != nil {
  277. br.Msg = "获取音频列表失败"
  278. br.ErrMsg = "获取音频列表失败, Err: " + err.Error()
  279. return
  280. }
  281. if len(videoList) > 0 {
  282. reportId := videoList[0].ReportId
  283. reportInfo, err := models.GetReportById(reportId)
  284. if err != nil {
  285. br.Msg = "报告信息有误"
  286. br.ErrMsg = "获取报告信息失败, Err: " + err.Error()
  287. return
  288. }
  289. // 创建zip
  290. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  291. savePath := zipName
  292. zipFile, err := os.Create(zipName)
  293. if err != nil {
  294. return
  295. }
  296. zipWriter := zip.NewWriter(zipFile)
  297. // 生成zip过程中报错关闭
  298. defer func() {
  299. if err != nil {
  300. zipWriter.Close()
  301. zipFile.Close()
  302. }
  303. os.Remove(savePath)
  304. }()
  305. // 获取音频,写入zip
  306. for i := 0; i < len(videoList); i++ {
  307. item := videoList[i]
  308. if item.VideoName == "" || item.VideoUrl == "" {
  309. continue
  310. }
  311. // 音频文件后缀
  312. ext := path.Ext(item.VideoUrl)
  313. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  314. if err != nil {
  315. if os.IsPermission(err) {
  316. fmt.Println("权限不足: ", err)
  317. return
  318. }
  319. return
  320. }
  321. var content []byte
  322. content, err = http.Get(item.VideoUrl)
  323. if err != nil {
  324. content = []byte("")
  325. }
  326. ioWriter.Write(content)
  327. }
  328. // 生成zip后关闭,否则下载文件会损坏
  329. zipWriter.Close()
  330. zipFile.Close()
  331. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", reportInfo.Title))
  332. }
  333. br.Ret = 200
  334. br.Msg = "下载成功"
  335. br.Success = true
  336. return
  337. }