audio.go 10 KB

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