| | |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "gorm.io/gorm" |
| | | "io" |
| | | "os" |
| | | "path" |
| | | "speechAnalysis/constvar" |
| | | "speechAnalysis/extend/code" |
| | |
| | | LocomotiveNumber: arr[0], |
| | | TrainNumber: arr[1], |
| | | DriverNumber: arr[2], |
| | | StationNumber: arr[3], |
| | | Station: arr[3], |
| | | OccurrenceAt: t, |
| | | IsFollowed: 0, |
| | | } |
| | |
| | | util.ResponseFormat(c, code.SaveFail, "上传失败") |
| | | return |
| | | } |
| | | go func() { |
| | | |
| | | var trainInfoNames = []string{arr[0], arr[1], arr[3]} |
| | | |
| | | var ( |
| | | info *models.TrainInfo |
| | | err error |
| | | parent models.TrainInfo |
| | | ) |
| | | for i := 0; i < 3; i++ { |
| | | name := trainInfoNames[i] |
| | | class := constvar.Class(i + 1) |
| | | info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First() |
| | | if err == gorm.ErrRecordNotFound { |
| | | info = &models.TrainInfo{ |
| | | Name: name, |
| | | Class: class, |
| | | ParentID: parent.ID, |
| | | } |
| | | _ = models.NewTrainInfoSearch().Create(info) |
| | | } |
| | | parent = *info |
| | | } |
| | | |
| | | }() |
| | | |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | } |
| | |
| | | return errors.New("文件格式错误") |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | // TrainInfoList |
| | | // @Tags 音频 |
| | | // @Summary 获取火车信息 |
| | | // @Produce application/json |
| | | // @Param object query request.GetTrainInfoList true "参数" |
| | | // @Success 200 {object} util.ResponseList{data=[]models.TrainInfo} "成功" |
| | | // @Router /api-sa/v1/audio/trainInfoList [get] |
| | | func (slf AudioCtl) TrainInfoList(c *gin.Context) { |
| | | var params request.GetTrainInfoList |
| | | if err := c.ShouldBindQuery(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | if !params.PageInfo.Check() { |
| | | util.ResponseFormat(c, code.RequestParamError, "分页参数错误") |
| | | return |
| | | } |
| | | |
| | | list, total, err := models.NewTrainInfoSearch(). |
| | | SetPage(params.Page, params.PageSize). |
| | | SetClass(params.Class). |
| | | SetParentId(params.ParentID). |
| | | Find() |
| | | |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找失败") |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormatList(c, code.Success, list, total) |
| | | } |
| | | |
| | | // List |
| | |
| | | SetLocomotiveNumber(params.LocomotiveNumber). |
| | | SetTrainNumber(params.TrainNumber). |
| | | SetDriverNumber(params.DriverNumber). |
| | | SetStationNumber(params.StationNumber). |
| | | SetStation(params.StationNumber). |
| | | SetBeginTime(params.BeginTime). |
| | | SetEndTime(params.EndTime). |
| | | SetIsFollowed(params.IsFollowed). |
| | | SetAudioStatusList(params.StatusList). |
| | | Find() |
| | | |
| | | if err != nil { |
| | |
| | | return |
| | | } |
| | | |
| | | util.ResponseFormatList(c, code.Success, list, int(total)) |
| | | util.ResponseFormatList(c, code.Success, list, total) |
| | | } |
| | | |
| | | // Process |
| | |
| | | } |
| | | |
| | | util.ResponseFormat(c, code.UpdateSuccess, "成功") |
| | | } |
| | | |
| | | // AudioInfo |
| | | // @Tags 音频 |
| | | // @Summary 音频详情,含解析结果 |
| | | // @Produce application/json |
| | | // @Param object query request.ProcessAudio true "参数" |
| | | // @Success 200 {object} util.Response{data=models.Audio} "成功" |
| | | // @Router /api-sa/v1/audio/info [get] |
| | | func (slf AudioCtl) AudioInfo(c *gin.Context) { |
| | | var params request.ProcessAudio |
| | | if err := c.ShouldBindQuery(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | audio, err := models.NewAudioSearch().SetID(params.ID).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "请求失败") |
| | | return |
| | | } |
| | | audioText, err := models.NewAudioTextSearch().SetAudioID(audio.ID).First() |
| | | if err == nil { |
| | | audio.AudioText = audioText.AudioText |
| | | } |
| | | |
| | | util.ResponseFormat(c, code.UpdateSuccess, audio) |
| | | } |
| | | |
| | | // AudioDownload |
| | | // @Tags 音频 |
| | | // @Summary 音频下载 |
| | | // @Produce application/json |
| | | // @Param object query request.ProcessAudio true "参数" |
| | | // @Success 200 {object} util.Response{data=models.Audio} "成功" |
| | | // @Router /api-sa/v1/audio/download [get] |
| | | func (slf AudioCtl) AudioDownload(c *gin.Context) { |
| | | var params request.ProcessAudio |
| | | if err := c.ShouldBindQuery(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | audio, err := models.NewAudioSearch().SetID(params.ID).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "查询失败") |
| | | return |
| | | } |
| | | |
| | | if audio.FilePath == "" { |
| | | util.ResponseFormat(c, code.InternalError, "查询失败") |
| | | return |
| | | } |
| | | |
| | | file, err := os.Open(audio.FilePath) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "文件打开失败") |
| | | return |
| | | } |
| | | defer file.Close() |
| | | |
| | | fileInfo, err := file.Stat() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "获取文件信息失败") |
| | | return |
| | | } |
| | | |
| | | c.Header("Content-Disposition", "inline; filename="+audio.Name) // 在浏览器中直接打开 |
| | | c.Header("Content-Length", fmt.Sprint(fileInfo.Size())) |
| | | c.Header("Content-Type", "audio/mpeg") // 设置音频文件类型 |
| | | |
| | | if _, err := io.Copy(c.Writer, file); err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "文件传输失败") |
| | | return |
| | | } |
| | | } |
| | | |
| | | // BatchProcess |
| | |
| | | // @Produce application/json |
| | | // @Param object body request.ProcessAudio true "参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-sa/v1/audio/delete [post] |
| | | // @Router /api-sa/v1/audio/delete [delete] |
| | | func (slf AudioCtl) Delete(c *gin.Context) { |
| | | var params request.ProcessAudio |
| | | if err := c.ShouldBind(¶ms); err != nil { |
| | |
| | | return |
| | | } |
| | | |
| | | err := service.DeleteAudio(params.ID) |
| | | audio, err := models.NewAudioSearch().SetID(params.ID).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "音频不存在") |
| | | return |
| | | } |
| | | |
| | | if audio.AudioStatus == constvar.AudioStatusProcessing || audio.AudioStatus == constvar.AudioStatusFinish { |
| | | util.ResponseFormat(c, code.RequestParamError, "音频正在处理或者处理完成,不可删除") |
| | | return |
| | | } |
| | | |
| | | err = service.DeleteAudio(params.ID) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | go func() { |
| | | err = os.Remove(audio.FilePath) |
| | | if err != nil { |
| | | logx.Warnf("remove file err:%v, file:%v", err, audio.FilePath) |
| | | } |
| | | }() |
| | | |
| | | util.ResponseFormat(c, code.DeleteSuccess, "成功") |
| | | } |
| | |
| | | // @Produce application/json |
| | | // @Param object body request.BatchProcessAudio true "参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-sa/v1/audio/batchDelete [post] |
| | | // @Router /api-sa/v1/audio/batchDelete [delete] |
| | | func (slf AudioCtl) BatchDelete(c *gin.Context) { |
| | | var params request.BatchProcessAudio |
| | | if err := c.ShouldBind(¶ms); err != nil { |
| | |
| | | return |
| | | } |
| | | |
| | | err := service.BatchDeleteAudio(params.IDs) |
| | | audioList, err := models.NewAudioSearch().SetIDs(params.IDs).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, "内部错误") |
| | | return |
| | | } |
| | | |
| | | for _, audio := range audioList { |
| | | if audio.AudioStatus == constvar.AudioStatusProcessing || audio.AudioStatus == constvar.AudioStatusFinish { |
| | | util.ResponseFormat(c, code.RequestParamError, "音频正在处理或者处理完成,不可删除") |
| | | return |
| | | } |
| | | } |
| | | |
| | | err = service.BatchDeleteAudio(params.IDs) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.InternalError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | go func() { |
| | | for _, audio := range audioList { |
| | | err = os.Remove(audio.FilePath) |
| | | if err != nil { |
| | | logx.Warnf("remove file err:%v, file:%v", err, audio.FilePath) |
| | | } |
| | | } |
| | | }() |
| | | |
| | | util.ResponseFormat(c, code.DeleteSuccess, "成功") |
| | | } |
| | | |