voice.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/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. ossClient := services.NewOssClient()
  90. if ossClient == nil {
  91. br.Msg = "上传失败"
  92. br.ErrMsg = "初始化OSS服务失败"
  93. return
  94. }
  95. resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
  96. if err != nil {
  97. br.Msg = "文件上传失败"
  98. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  99. return
  100. }
  101. defer func() {
  102. os.Remove(fpath)
  103. }()
  104. item := new(models.Resource)
  105. item.ResourceUrl = resourceUrl
  106. item.ResourceType = 2
  107. item.CreateTime = time.Now()
  108. newId, err := models.AddResource(item)
  109. if err != nil {
  110. br.Msg = "资源上传失败"
  111. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  112. return
  113. }
  114. var playSeconds float64
  115. playSeconds, err = mp3duration.Calculate(fpath)
  116. if playSeconds <= 0 {
  117. playSeconds, err = utils.GetVideoPlaySeconds(fpath)
  118. if err != nil {
  119. br.Msg = "获取音频时间失败"
  120. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  121. return
  122. }
  123. }
  124. createTime := report.CreateTime.Format("0102")
  125. videoName := report.Title + "(" + createTime + ")"
  126. fileBody, err := ioutil.ReadFile(fpath)
  127. videoSize := len(fileBody)
  128. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  129. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  130. err = models.ModifyReportVideo(reportId, resourceUrl, videoName, sizeStr, playSeconds)
  131. if err != nil {
  132. br.Msg = "上传失败"
  133. br.ErrMsg = "上传失败,Err:" + err.Error()
  134. return
  135. }
  136. resp := new(models.ResourceResp)
  137. resp.Id = newId
  138. resp.ResourceUrl = resourceUrl
  139. br.Msg = "上传成功"
  140. br.Ret = 200
  141. br.Success = true
  142. br.Data = resp
  143. return
  144. }
  145. // VoiceCommonController 音频
  146. type VoiceCommonController struct {
  147. BaseCommonController
  148. }
  149. // Download
  150. // @Title 音频下载
  151. // @Description 音频下载接口
  152. // @Param ReportId query int true "报告ID"
  153. // @Success Ret=200 下载成功
  154. // @router /download [get]
  155. func (this *VoiceCommonController) Download() {
  156. br := new(models.BaseResponse).Init()
  157. defer func() {
  158. this.Data["json"] = br
  159. this.ServeJSON()
  160. }()
  161. reportId, err := this.GetInt("ReportId")
  162. if err != nil {
  163. br.Msg = "参数错误"
  164. br.ErrMsg = "获取,ReportId,Err:" + err.Error()
  165. return
  166. }
  167. report, err := models.GetReportById(reportId)
  168. if err != nil {
  169. br.Msg = "获取信息失败"
  170. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  171. return
  172. }
  173. savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  174. fileBody, err := http.Get(report.VideoUrl)
  175. if err != nil {
  176. br.Msg = "获取信息失败"
  177. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  178. return
  179. }
  180. err = file.SaveFile(fileBody, savePath)
  181. if err != nil {
  182. br.Msg = "保存信息失败"
  183. br.ErrMsg = "保存信息失败,Err:" + err.Error()
  184. return
  185. }
  186. fileName := report.VideoName + ".mp3"
  187. this.Ctx.Output.Download(savePath, fileName)
  188. defer func() {
  189. os.Remove(savePath)
  190. }()
  191. br.Ret = 200
  192. br.Msg = "下载成功"
  193. br.Success = true
  194. return
  195. }
  196. // ReportChapterDownload
  197. // @Title 报告章节列表音频下载
  198. // @Description 音频下载接口
  199. // @Param ChapterIds query string true "章节IDs"
  200. // @Success Ret=200 下载成功
  201. // @router /report/download [get]
  202. func (this *VoiceCommonController) ReportChapterDownload() {
  203. br := new(models.BaseResponse).Init()
  204. defer func() {
  205. this.Data["json"] = br
  206. this.ServeJSON()
  207. }()
  208. strChapterIds := this.GetString("ChapterIds")
  209. if strChapterIds == "" {
  210. br.Msg = "参数错误"
  211. return
  212. }
  213. chapterIds := make([]int, 0)
  214. chapterIdArr := strings.Split(strChapterIds, ",")
  215. for _, v := range chapterIdArr {
  216. id, e := strconv.Atoi(v)
  217. if e != nil {
  218. br.Msg = "参数有误"
  219. br.ErrMsg = "章节ID有误, Err: " + e.Error()
  220. return
  221. }
  222. chapterIds = append(chapterIds, id)
  223. }
  224. if len(chapterIds) == 0 {
  225. br.Msg = "参数有误"
  226. return
  227. }
  228. videoList, err := models.GetReportChapterVideoListByChapterIds(chapterIds)
  229. if err != nil {
  230. br.Msg = "获取音频列表失败"
  231. br.ErrMsg = "获取音频列表失败, Err: " + err.Error()
  232. return
  233. }
  234. if len(videoList) > 0 {
  235. reportId := videoList[0].ReportId
  236. reportInfo, err := models.GetReportById(reportId)
  237. if err != nil {
  238. br.Msg = "报告信息有误"
  239. br.ErrMsg = "获取报告信息失败, Err: " + err.Error()
  240. return
  241. }
  242. // 创建zip
  243. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  244. savePath := zipName
  245. zipFile, err := os.Create(zipName)
  246. if err != nil {
  247. return
  248. }
  249. zipWriter := zip.NewWriter(zipFile)
  250. // 生成zip过程中报错关闭
  251. defer func() {
  252. if err != nil {
  253. zipWriter.Close()
  254. zipFile.Close()
  255. }
  256. os.Remove(savePath)
  257. }()
  258. // 获取音频,写入zip
  259. for i := 0; i < len(videoList); i++ {
  260. item := videoList[i]
  261. if item.VideoName == "" || item.VideoUrl == "" {
  262. continue
  263. }
  264. // 音频文件后缀
  265. ext := path.Ext(item.VideoUrl)
  266. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  267. if err != nil {
  268. if os.IsPermission(err) {
  269. fmt.Println("权限不足: ", err)
  270. return
  271. }
  272. return
  273. }
  274. var content []byte
  275. content, err = http.Get(item.VideoUrl)
  276. if err != nil {
  277. content = []byte("")
  278. }
  279. ioWriter.Write(content)
  280. }
  281. // 生成zip后关闭,否则下载文件会损坏
  282. zipWriter.Close()
  283. zipFile.Close()
  284. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", reportInfo.Title))
  285. }
  286. br.Ret = 200
  287. br.Msg = "下载成功"
  288. br.Success = true
  289. return
  290. }