voice.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. type VoiceController struct {
  17. BaseAuthController
  18. }
  19. // @router /upload [post]
  20. func (this *VoiceController) Upload() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. f, h, err := this.GetFile("file")
  27. if err != nil {
  28. br.Msg = "获取资源信息失败"
  29. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  30. return
  31. }
  32. reportId, err := this.GetInt("ReportId")
  33. if err != nil {
  34. br.Msg = "报告id异常"
  35. br.ErrMsg = "报告id异常,Err:" + err.Error()
  36. return
  37. }
  38. reportInfo, err := models.GetReportByReportId(reportId)
  39. if err != nil {
  40. br.Msg = "获取报告信息失败"
  41. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  42. return
  43. }
  44. ext := path.Ext(h.Filename)
  45. dateDir := time.Now().Format("20060102")
  46. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  47. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  48. if err != nil {
  49. br.Msg = "存储目录创建失败"
  50. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  51. return
  52. }
  53. randStr := utils.GetRandStringNoSpecialChar(28)
  54. fileName := randStr + ext
  55. fpath := uploadDir + "/" + fileName
  56. defer f.Close() //关闭上传文件
  57. err = this.SaveToFile("file", fpath)
  58. if err != nil {
  59. br.Msg = "文件上传失败"
  60. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  61. return
  62. }
  63. resourceUrl := ``
  64. ossClient := services.NewOssClient()
  65. if ossClient == nil {
  66. br.Msg = "上传失败"
  67. br.ErrMsg = "初始化OSS服务失败"
  68. return
  69. }
  70. resourceUrl, err = ossClient.UploadFile(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. var playSeconds float64
  90. playSeconds, err = mp3duration.Calculate(fpath)
  91. if playSeconds <= 0 {
  92. playSeconds, err = utils.GetVideoPlaySeconds(fpath)
  93. if err != nil {
  94. br.Msg = "获取音频时间失败"
  95. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  96. return
  97. }
  98. }
  99. fileBody, err := os.ReadFile(fpath)
  100. videoSize := len(fileBody)
  101. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  102. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  103. {
  104. createTimeStr := reportInfo.CreateTime.Format("0102")
  105. videoName := reportInfo.Title + "(" + createTimeStr + ")"
  106. reportInfo.VideoUrl = resourceUrl
  107. reportInfo.VideoName = videoName
  108. reportInfo.VideoPlaySeconds = fmt.Sprint(playSeconds)
  109. reportInfo.VideoSize = sizeStr
  110. reportInfo.LastModifyAdminId = this.SysUser.AdminId
  111. reportInfo.LastModifyAdminName = this.SysUser.RealName
  112. reportInfo.VoiceGenerateType = 1
  113. reportInfo.ModifyTime = time.Now()
  114. err = reportInfo.UpdateReport([]string{"VideoUrl", "VideoName", "VideoPlaySeconds", "VideoSize", "LastModifyAdminId", "LastModifyAdminName", "VoiceGenerateType", "ModifyTime"})
  115. if err != nil {
  116. br.Msg = "上传失败"
  117. br.ErrMsg = "修改报告的音频信息失败,Err:" + err.Error()
  118. return
  119. }
  120. }
  121. resp := new(models.ResourceResp)
  122. resp.Id = newId
  123. resp.ResourceUrl = resourceUrl
  124. br.Msg = "上传成功"
  125. br.Ret = 200
  126. br.Success = true
  127. br.Data = resp
  128. return
  129. }
  130. type VoiceCommonController struct {
  131. BaseCommonController
  132. }
  133. // @router /download [get]
  134. func (this *VoiceCommonController) Download() {
  135. br := new(models.BaseResponse).Init()
  136. defer func() {
  137. this.Data["json"] = br
  138. this.ServeJSON()
  139. }()
  140. reportId, err := this.GetInt("ReportId")
  141. if err != nil {
  142. br.Msg = "参数错误"
  143. br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  144. return
  145. }
  146. reportInfo, err := models.GetReportById(reportId)
  147. if err != nil {
  148. br.Msg = "获取信息失败"
  149. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  150. return
  151. }
  152. savePath, fileName, err, errMsg := services.DownloadVoice(reportInfo)
  153. if savePath != `` {
  154. defer func() {
  155. os.Remove(savePath)
  156. }()
  157. }
  158. if err != nil {
  159. br.Msg = errMsg
  160. br.ErrMsg = "下载失败,Err:" + err.Error()
  161. return
  162. }
  163. if savePath != `` {
  164. this.Ctx.Output.Download(savePath, fileName)
  165. }
  166. br.Ret = 200
  167. br.Msg = "下载成功"
  168. br.Success = true
  169. return
  170. }
  171. // @router /report/download [get]
  172. func (this *VoiceCommonController) ReportChapterDownload() {
  173. br := new(models.BaseResponse).Init()
  174. defer func() {
  175. this.Data["json"] = br
  176. this.ServeJSON()
  177. }()
  178. strChapterIds := this.GetString("ChapterIds")
  179. if strChapterIds == "" {
  180. br.Msg = "参数错误"
  181. return
  182. }
  183. chapterIds := make([]int, 0)
  184. chapterIdArr := strings.Split(strChapterIds, ",")
  185. for _, v := range chapterIdArr {
  186. id, e := strconv.Atoi(v)
  187. if e != nil {
  188. br.Msg = "参数有误"
  189. br.ErrMsg = "章节ID有误, Err: " + e.Error()
  190. return
  191. }
  192. chapterIds = append(chapterIds, id)
  193. }
  194. if len(chapterIds) == 0 {
  195. br.Msg = "参数有误"
  196. return
  197. }
  198. videoList, err := models.GetReportChapterVideoListByChapterIds(chapterIds)
  199. if err != nil {
  200. br.Msg = "获取音频列表失败"
  201. br.ErrMsg = "获取音频列表失败, Err: " + err.Error()
  202. return
  203. }
  204. if len(videoList) > 0 {
  205. reportId := videoList[0].ReportId
  206. reportInfo, err := models.GetReportById(reportId)
  207. if err != nil {
  208. br.Msg = "报告信息有误"
  209. br.ErrMsg = "获取报告信息失败, Err: " + err.Error()
  210. return
  211. }
  212. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  213. savePath := zipName
  214. zipFile, err := os.Create(zipName)
  215. if err != nil {
  216. return
  217. }
  218. zipWriter := zip.NewWriter(zipFile)
  219. defer func() {
  220. if err != nil {
  221. zipWriter.Close()
  222. zipFile.Close()
  223. }
  224. os.Remove(savePath)
  225. }()
  226. for i := 0; i < len(videoList); i++ {
  227. item := videoList[i]
  228. if item.VideoName == "" || item.VideoUrl == "" {
  229. continue
  230. }
  231. ext := path.Ext(item.VideoUrl)
  232. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  233. if err != nil {
  234. if os.IsPermission(err) {
  235. fmt.Println("权限不足: ", err)
  236. return
  237. }
  238. return
  239. }
  240. var content []byte
  241. content, err = http.Get(item.VideoUrl)
  242. if err != nil {
  243. content = []byte("")
  244. }
  245. ioWriter.Write(content)
  246. }
  247. zipWriter.Close()
  248. zipFile.Close()
  249. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", reportInfo.Title))
  250. }
  251. br.Ret = 200
  252. br.Msg = "下载成功"
  253. br.Success = true
  254. return
  255. }