audio.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package controllers
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "gorm.io/gorm"
  6. "path"
  7. "speechAnalysis/constvar"
  8. "speechAnalysis/extend/code"
  9. "speechAnalysis/extend/util"
  10. "speechAnalysis/models"
  11. "speechAnalysis/pkg/logx"
  12. "speechAnalysis/request"
  13. "speechAnalysis/response"
  14. "speechAnalysis/service"
  15. "speechAnalysis/utils/upload"
  16. "strings"
  17. "time"
  18. )
  19. type AudioCtl struct{}
  20. // Upload
  21. // @Tags 音频
  22. // @Summary 上传音频
  23. // @Produce application/json
  24. // @Param file formData file true "音频文件"
  25. // @Success 200 {object} util.Response "成功"
  26. // @Router /api-sa/v1/audio/upload [post]
  27. func (slf AudioCtl) Upload(c *gin.Context) {
  28. _, header, err := c.Request.FormFile("file")
  29. if err != nil {
  30. util.ResponseFormat(c, code.RequestParamError, err.Error())
  31. return
  32. }
  33. filename := path.Base(header.Filename)
  34. arr := strings.Split(filename, "_")
  35. if len(arr) != 6 {
  36. util.ResponseFormat(c, code.RequestParamError, "文件名称错误")
  37. return
  38. }
  39. _, err = models.NewAudioSearch().SetName(filename).First()
  40. if err != gorm.ErrRecordNotFound {
  41. util.ResponseFormat(c, code.RequestParamError, "重复上传")
  42. return
  43. }
  44. oss := upload.NewOss()
  45. filePath, filename, uploadErr := oss.UploadFile(header)
  46. if uploadErr != nil {
  47. logx.Errorf("upload audio err: %v", err)
  48. util.ResponseFormat(c, code.RequestParamError, "上传失败")
  49. return
  50. }
  51. timeStr := arr[4] + strings.Split(arr[5], ".")[0]
  52. t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
  53. if err != nil {
  54. util.ResponseFormat(c, code.RequestParamError, "时间格式不对")
  55. return
  56. }
  57. audio := &models.Audio{
  58. Name: filename,
  59. Size: header.Size,
  60. FilePath: filePath,
  61. AudioStatus: constvar.AudioStatusUploadOk,
  62. LocomotiveNumber: arr[0],
  63. TrainNumber: arr[1],
  64. DriverNumber: arr[2],
  65. StationNumber: arr[3],
  66. OccurrenceAt: t,
  67. IsFollowed: 0,
  68. }
  69. if err = models.NewAudioSearch().Create(audio); err != nil {
  70. util.ResponseFormat(c, code.SaveFail, "上传失败")
  71. return
  72. }
  73. util.ResponseFormat(c, code.Success, "添加成功")
  74. }
  75. func (slf AudioCtl) ParamsCheck(filename string) (err error) {
  76. arr := strings.Split(filename, "_")
  77. if len(arr) != 6 {
  78. return errors.New("文件格式错误")
  79. }
  80. return nil
  81. }
  82. // List
  83. // @Tags 音频
  84. // @Summary 音频分析检索
  85. // @Produce application/json
  86. // @Param object query request.GetAudioList true "参数"
  87. // @Success 200 {object} util.ResponseList{data=[]models.Audio} "成功"
  88. // @Router /api-sa/v1/audio/list [get]
  89. func (slf AudioCtl) List(c *gin.Context) {
  90. var params request.GetAudioList
  91. if err := c.ShouldBindQuery(&params); err != nil {
  92. util.ResponseFormat(c, code.RequestParamError, err.Error())
  93. return
  94. }
  95. if !params.PageInfo.Check() {
  96. util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
  97. return
  98. }
  99. list, total, err := models.NewAudioSearch().
  100. SetPage(params.Page, params.PageSize).
  101. SetKeyword(params.Keyword).
  102. SetLocomotiveNumber(params.LocomotiveNumber).
  103. SetTrainNumber(params.TrainNumber).
  104. SetDriverNumber(params.DriverNumber).
  105. SetStationNumber(params.StationNumber).
  106. Find()
  107. if err != nil {
  108. util.ResponseFormat(c, code.RequestParamError, "查找失败")
  109. return
  110. }
  111. util.ResponseFormatList(c, code.Success, list, int(total))
  112. }
  113. // Process
  114. // @Tags 音频
  115. // @Summary 处理音频
  116. // @Produce application/json
  117. // @Param object body request.ProcessAudio true "参数"
  118. // @Success 200 {object} util.Response "成功"
  119. // @Router /api-sa/v1/audio/process [post]
  120. func (slf AudioCtl) Process(c *gin.Context) {
  121. var params request.ProcessAudio
  122. if err := c.ShouldBind(&params); err != nil {
  123. util.ResponseFormat(c, code.RequestParamError, err.Error())
  124. return
  125. }
  126. err := service.Process(params.ID)
  127. if err != nil {
  128. util.ResponseFormat(c, code.InternalError, err.Error())
  129. return
  130. }
  131. util.ResponseFormat(c, code.UpdateSuccess, "成功")
  132. }
  133. // BatchProcess
  134. // @Tags 音频
  135. // @Summary 批量处理音频
  136. // @Produce application/json
  137. // @Param object body request.BatchProcessAudio true "参数"
  138. // @Success 200 {object} util.Response "成功"
  139. // @Router /api-sa/v1/audio/batchProcess [post]
  140. func (slf AudioCtl) BatchProcess(c *gin.Context) {
  141. var params request.BatchProcessAudio
  142. if err := c.ShouldBind(&params); err != nil {
  143. util.ResponseFormat(c, code.RequestParamError, err.Error())
  144. return
  145. }
  146. var failedNumber int
  147. for _, audioID := range params.IDs {
  148. err := service.Process(audioID)
  149. if err != nil {
  150. logx.Errorf("%v,编号: %v", err.Error(), audioID)
  151. failedNumber++
  152. continue
  153. }
  154. }
  155. if failedNumber == 0 {
  156. util.ResponseFormat(c, code.UpdateSuccess, "成功")
  157. return
  158. } else if failedNumber < len(params.IDs) {
  159. util.ResponseFormat(c, code.RequestParamError, "部分处理失败")
  160. return
  161. } else {
  162. util.ResponseFormat(c, code.RequestParamError, "全部处理失败")
  163. return
  164. }
  165. }
  166. // Delete
  167. // @Tags 音频
  168. // @Summary 删除音频
  169. // @Produce application/json
  170. // @Param object body request.ProcessAudio true "参数"
  171. // @Success 200 {object} util.Response "成功"
  172. // @Router /api-sa/v1/audio/delete [post]
  173. func (slf AudioCtl) Delete(c *gin.Context) {
  174. var params request.ProcessAudio
  175. if err := c.ShouldBind(&params); err != nil {
  176. util.ResponseFormat(c, code.RequestParamError, err.Error())
  177. return
  178. }
  179. err := service.DeleteAudio(params.ID)
  180. if err != nil {
  181. util.ResponseFormat(c, code.InternalError, err.Error())
  182. return
  183. }
  184. util.ResponseFormat(c, code.DeleteSuccess, "成功")
  185. }
  186. // BatchDelete
  187. // @Tags 音频
  188. // @Summary 批量删除音频
  189. // @Produce application/json
  190. // @Param object body request.BatchProcessAudio true "参数"
  191. // @Success 200 {object} util.Response "成功"
  192. // @Router /api-sa/v1/audio/batchDelete [post]
  193. func (slf AudioCtl) BatchDelete(c *gin.Context) {
  194. var params request.BatchProcessAudio
  195. if err := c.ShouldBind(&params); err != nil {
  196. util.ResponseFormat(c, code.RequestParamError, err.Error())
  197. return
  198. }
  199. err := service.BatchDeleteAudio(params.IDs)
  200. if err != nil {
  201. util.ResponseFormat(c, code.InternalError, err.Error())
  202. return
  203. }
  204. util.ResponseFormat(c, code.DeleteSuccess, "成功")
  205. }
  206. // Follow
  207. // @Tags 音频
  208. // @Summary 关注/取消关注
  209. // @Produce application/json
  210. // @Param object body request.FollowReq true "参数"
  211. // @Success 200 {object} util.Response{data=response.FollowResp} "成功"
  212. // @Router /api-sa/v1/audio/follow [post]
  213. func (slf AudioCtl) Follow(c *gin.Context) {
  214. var params request.ProcessAudio
  215. if err := c.ShouldBind(&params); err != nil {
  216. util.ResponseFormat(c, code.RequestParamError, err.Error())
  217. return
  218. }
  219. followStatus, err := service.Follow(params.ID)
  220. if err != nil {
  221. util.ResponseFormat(c, code.InternalError, err.Error())
  222. return
  223. }
  224. resp := response.FollowResp{FollowStatus: followStatus}
  225. util.ResponseFormat(c, code.UpdateSuccess, resp)
  226. }