voice.go 9.4 KB

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