audio.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/services/elastic"
  9. "eta/eta_mini_crm_ht/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "github.com/tcolgate/mp3"
  13. "os"
  14. "path"
  15. "strconv"
  16. "time"
  17. )
  18. type AudioController struct {
  19. BaseAuthController
  20. }
  21. // UploadAudio @Title 上传视频
  22. // @Description 上传视频
  23. // @Param File query file true "文件"
  24. // @Success 200 {object} models.ReportAuthorResp
  25. // @router /uploadAudio [post]
  26. func (this *AudioController) UploadAudio() {
  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. defer f.Close()
  39. size, err := strconv.Atoi(utils.UPLOAD_AUDIO_SIZE)
  40. if err != nil {
  41. size = 100
  42. }
  43. if h.Size > 1024*1024*int64(size) {
  44. br.Msg = fmt.Sprintf("音频大小不能超过%dM", size)
  45. br.ErrMsg = "音频上传失败,Err:" + err.Error()
  46. return
  47. }
  48. ext := path.Ext(h.Filename)
  49. if ext != ".mp3" {
  50. br.Msg = "音频格式不正确"
  51. br.ErrMsg = "音频上传失败,Err:" + err.Error()
  52. return
  53. }
  54. dateDir := time.Now().Format("20060102")
  55. uploadDir := utils.STATIC_DIR + "ht/audio" + 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. err = this.SaveToFile("File", fpath)
  66. if err != nil {
  67. br.Msg = "音频上传失败"
  68. br.ErrMsg = "音频上传失败,Err:" + err.Error()
  69. return
  70. }
  71. mp3File := mp3.NewDecoder(f)
  72. if err != nil {
  73. br.Msg = "音频上传失败"
  74. br.ErrMsg = "解析音频时常失败,Err:" + err.Error()
  75. return
  76. }
  77. var totalDuration time.Duration
  78. skipped := 0
  79. for {
  80. frame := mp3.Frame{}
  81. if err = mp3File.Decode(&frame, &skipped); err != nil {
  82. break
  83. }
  84. totalDuration += frame.Duration()
  85. }
  86. duration := int(totalDuration.Milliseconds())
  87. audioUploadDir := utils.RESOURCE_DIR + "audio/"
  88. savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
  89. audioName := utils.GetRandStringNoSpecialChar(28)
  90. savePdfToOssPath += audioName + ext
  91. defer func() {
  92. err = os.Remove(fpath)
  93. fmt.Sprintf("删除文件失败:%v", err)
  94. }()
  95. ossClient := services.NewOssClient()
  96. if ossClient == nil {
  97. br.Msg = "音频上传失败"
  98. br.ErrMsg = "初始化OSS服务失败"
  99. return
  100. }
  101. mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  102. if err != nil {
  103. br.Msg = "音频上传失败"
  104. br.ErrMsg = "音频上传失败,Err:" + err.Error()
  105. return
  106. }
  107. base := path.Base(h.Filename)
  108. resp := new(response.MediaUploadResp)
  109. resp.Url = mp3Url
  110. resp.FileName = base
  111. resp.DurationMillisecond = duration
  112. br.Data = resp
  113. br.Msg = "上传成功"
  114. br.Ret = 200
  115. br.Success = true
  116. }
  117. // AddAudio @Title 新增音频
  118. // @Description 新增音频
  119. // @Param File query file true "文件"
  120. // @Success 200 {object} models.ReportAuthorResp
  121. // @router /addAudio [post]
  122. func (this *AudioController) AddAudio() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. var req request.AudioReq
  129. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  130. br.Msg = "参数解析失败"
  131. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  132. return
  133. }
  134. if req.AudioName == "" {
  135. br.Msg = "音频名称为空"
  136. return
  137. }
  138. if req.AnalystId == 0 {
  139. br.Msg = "研究员ID为空"
  140. return
  141. }
  142. if req.AnalystName == "" {
  143. br.Msg = "研究员名称为空"
  144. return
  145. }
  146. if req.DurationMillisecond == 0 {
  147. br.Msg = "音频时长为空"
  148. return
  149. }
  150. if req.SrcUrl == "" {
  151. br.Msg = "音频地址为空"
  152. return
  153. }
  154. var err error
  155. audioInsert := &models.Media{
  156. AuthorId: req.AnalystId,
  157. AuthorName: req.AnalystName,
  158. MediaType: models.Audio,
  159. Src: req.SrcUrl,
  160. MediaName: req.AudioName,
  161. SourceType: "mp3",
  162. MediaPlayMilliseconds: req.DurationMillisecond,
  163. PermissionIds: req.PermissionIds,
  164. SendStatus: models.UNSEND,
  165. PublishedTime: time.Now(),
  166. Deleted: 0,
  167. CreatedTime: time.Now(),
  168. }
  169. err = models.InsertMedia(audioInsert)
  170. if err != nil {
  171. br.Msg = "添加失败"
  172. br.ErrMsg = "音频新增失败,Err:" + err.Error()
  173. return
  174. }
  175. //audioInsert.Id = int(insertId)
  176. // 添加es
  177. go func(audio *models.ESMedia) {
  178. docId := strconv.Itoa(audio.MediaId)
  179. err = elastic.EsAddOrEditMedia(utils.MEDIA_INDEX, docId, audio)
  180. if err != nil {
  181. utils.FileLog.Info("音频记录es新增失败,Err:" + err.Error())
  182. return
  183. }
  184. utils.FileLog.Info("音频记录es新增成功, pdfId:" + docId)
  185. }(audioInsert.ToView())
  186. // 创建消息
  187. _, _ = services.CreateMeta(audioInsert.AuthorName, audioInsert.AuthorId, audioInsert.Id, audioInsert.PublishedTime.Format(time.DateTime), models.AudioSourceType)
  188. br.Msg = "添加成功"
  189. br.Ret = 200
  190. br.Success = true
  191. }
  192. // @Title 编辑音频
  193. //
  194. // @Description 编辑音频
  195. // @Success 200 {object} models.ReportAuthorResp
  196. // @router /editAudio [post]
  197. func (this *AudioController) EditAudio() {
  198. br := new(models.BaseResponse).Init()
  199. defer func() {
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. var req request.AudioReq
  204. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  205. br.Msg = "参数解析失败"
  206. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  207. return
  208. }
  209. if req.AudioId <= 0 {
  210. br.Msg = "音频ID为空"
  211. return
  212. }
  213. if req.AudioName == "" {
  214. br.Msg = "音频名称为空"
  215. return
  216. }
  217. if req.AnalystId == 0 {
  218. br.Msg = "研究员ID为空"
  219. return
  220. }
  221. if req.AnalystName == "" {
  222. br.Msg = "研究员名称为空"
  223. return
  224. }
  225. if req.DurationMillisecond == 0 {
  226. br.Msg = "音频时长为空"
  227. return
  228. }
  229. if req.SrcUrl == "" {
  230. br.Msg = "音频地址为空"
  231. return
  232. }
  233. var err error
  234. audioEdit := &models.Media{
  235. Id: req.AudioId,
  236. AuthorId: req.AnalystId,
  237. AuthorName: req.AnalystName,
  238. MediaType: models.Audio,
  239. Src: req.SrcUrl,
  240. MediaName: req.AudioName,
  241. SourceType: "mp3",
  242. MediaPlayMilliseconds: req.DurationMillisecond,
  243. PermissionIds: req.PermissionIds,
  244. }
  245. _, err = models.UpdateMedia(audioEdit)
  246. if err != nil {
  247. br.Msg = "更新失败"
  248. br.ErrMsg = "音频更新失败,Err:" + err.Error()
  249. return
  250. }
  251. // 添加es
  252. go func(audio *models.ESMedia) {
  253. docId := strconv.Itoa(audio.MediaId)
  254. err = elastic.EsAddOrEditMedia(utils.MEDIA_INDEX, docId, audio)
  255. if err != nil {
  256. utils.FileLog.Info("音频记录es更新失败,Err:" + err.Error())
  257. return
  258. }
  259. utils.FileLog.Info("音频记录es更新成功, pdfId:" + docId)
  260. }(audioEdit.ToView())
  261. br.Msg = "编辑成功"
  262. br.Ret = 200
  263. br.Success = true
  264. }
  265. // DeleteAudio
  266. // @Title 删除音频
  267. //
  268. // @Description 删除音频
  269. // @Success 200 {object} models.ReportAuthorResp
  270. // @router /deleteAudio [post]
  271. func (this *AudioController) DeleteAudio() {
  272. br := new(models.BaseResponse).Init()
  273. defer func() {
  274. this.Data["json"] = br
  275. this.ServeJSON()
  276. }()
  277. var req request.AudioReq
  278. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  279. br.Msg = "参数解析失败"
  280. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  281. return
  282. }
  283. if req.AudioId <= 0 {
  284. br.Msg = "音频ID为空"
  285. return
  286. }
  287. var err error
  288. audioDelete := &models.Media{
  289. Id: req.AudioId,
  290. Deleted: 1,
  291. }
  292. err = models.DeleteMedia(audioDelete)
  293. if err != nil {
  294. br.Msg = "删除失败"
  295. br.ErrMsg = "音频删除失败,Err:" + err.Error()
  296. return
  297. }
  298. // 添加es
  299. go func(audio *models.ESMedia) {
  300. docId := strconv.Itoa(audio.MediaId)
  301. err = elastic.EsDeleteData(utils.MEDIA_INDEX, docId)
  302. if err != nil {
  303. utils.FileLog.Info("音频记录es删除失败,Err:" + err.Error())
  304. return
  305. }
  306. utils.FileLog.Info("音频记录es删除成功, pdfId:" + docId)
  307. }(audioDelete.ToView())
  308. br.Msg = "删除成功"
  309. br.Ret = 200
  310. br.Success = true
  311. }
  312. // AudioList
  313. // @Title 研报列表
  314. // @Description pdf研报列表
  315. // @Param PageSize query int true "每页数据条数"
  316. // @Param CurrentIndex query int true "当前页页码,从1开始"
  317. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  318. // @Param KeyWord query string true "报告标题/创建人"
  319. // @Param SortType query string true "排序方式"
  320. // @Success 200 {object} models.ReportAuthorResp
  321. // @router /audioList [get]
  322. func (this *AudioController) AudioList() {
  323. br := new(models.BaseResponse).Init()
  324. defer func() {
  325. this.Data["json"] = br
  326. this.ServeJSON()
  327. }()
  328. pageSize, _ := this.GetInt("PageSize")
  329. currentIndex, _ := this.GetInt("CurrentIndex")
  330. sortType := this.GetString("SortType")
  331. KeyWord := this.GetString("KeyWord")
  332. var condition string
  333. var pars []interface{}
  334. if pageSize <= 0 {
  335. pageSize = utils.PageSize20
  336. }
  337. if currentIndex <= 0 {
  338. currentIndex = 1
  339. }
  340. if KeyWord != "" {
  341. condition += " AND media_name like '%" + KeyWord + "%'"
  342. }
  343. sortCondition := " ORDER BY published_time "
  344. if sortType == "" {
  345. sortType = "DESC"
  346. }
  347. sortCondition = sortCondition + sortType
  348. total, err := models.GetMediaCountByCondition(models.Audio, condition, pars)
  349. if err != nil {
  350. br.Msg = "获取音频列表失败"
  351. br.ErrMsg = "获取音频列表统计失败,Err:" + err.Error()
  352. return
  353. }
  354. startSize := utils.StartIndex(currentIndex, pageSize)
  355. reportList, err := models.GetMediaByCondition(models.Audio, condition, sortCondition, pars, startSize, pageSize)
  356. if err != nil {
  357. br.Msg = "获取音频列表失败"
  358. br.ErrMsg = "获取音频列表失败,Err:" + err.Error()
  359. return
  360. }
  361. var reportViewList []*models.MediaView
  362. for _, report := range reportList {
  363. reportView := report.ToMediaView()
  364. reportViewList = append(reportViewList, reportView)
  365. }
  366. page := paging.GetPaging(currentIndex, pageSize, total)
  367. resp := new(response.MediaListResp)
  368. resp.List = reportViewList
  369. resp.Paging = page
  370. br.Ret = 200
  371. br.Success = true
  372. br.Data = resp
  373. br.Msg = "获取成功"
  374. }