audio.go 11 KB

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