package controllers import ( "encoding/json" "eta/eta_mini_crm_ht/models" "eta/eta_mini_crm_ht/models/request" "eta/eta_mini_crm_ht/models/response" "eta/eta_mini_crm_ht/services" "eta/eta_mini_crm_ht/services/elastic" "eta/eta_mini_crm_ht/utils" "fmt" "github.com/tcolgate/mp3" "os" "path" "strconv" "time" ) type AudioController struct { BaseAuthController } // UploadAudio @Title 上传视频 // @Description 上传视频 // @Param File query file true "文件" // @Success 200 {object} models.ReportAuthorResp // @router /uploadAudio [post] func (this *AudioController) UploadAudio() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() f, h, err := this.GetFile("File") if err != nil { br.Msg = "获取资源信息失败" br.ErrMsg = "获取资源信息失败,Err:" + err.Error() return } defer f.Close() size, err := strconv.Atoi(utils.UPLOAD_AUDIO_SIZE) if err != nil { size = 100 } if h.Size > 1024*1024*int64(size) { br.Msg = fmt.Sprintf("音频大小不能超过%dM", size) br.ErrMsg = "音频上传失败,Err:" + err.Error() return } ext := path.Ext(h.Filename) if ext != ".mp3" { br.Msg = "音频格式不正确" br.ErrMsg = "音频上传失败,Err:" + err.Error() return } dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir err = os.MkdirAll(uploadDir, utils.DIR_MOD) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName err = this.SaveToFile("File", fpath) if err != nil { br.Msg = "音频上传失败" br.ErrMsg = "音频上传失败,Err:" + err.Error() return } mp3File := mp3.NewDecoder(f) if err != nil { br.Msg = "音频上传失败" br.ErrMsg = "解析音频时常失败,Err:" + err.Error() return } var totalDuration time.Duration skipped := 0 for { frame := mp3.Frame{} if err = mp3File.Decode(&frame, &skipped); err != nil { break } totalDuration += frame.Duration() } duration := int(totalDuration.Milliseconds()) audioUploadDir := utils.RESOURCE_DIR + "audio/" savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/") audioName := utils.GetRandStringNoSpecialChar(28) savePdfToOssPath += audioName + ext defer func() { err = os.Remove(fpath) fmt.Sprintf("删除文件失败:%v", err) }() ossClient := services.NewOssClient() if ossClient == nil { br.Msg = "音频上传失败" br.ErrMsg = "初始化OSS服务失败" return } mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath) if err != nil { br.Msg = "音频上传失败" br.ErrMsg = "音频上传失败,Err:" + err.Error() return } base := path.Base(h.Filename) resp := new(response.MediaUploadResp) resp.Url = mp3Url resp.FileName = base resp.DurationMillisecond = duration br.Data = resp br.Msg = "上传成功" br.Ret = 200 br.Success = true } // AddAudio @Title 新增音频 // @Description 新增音频 // @Param File query file true "文件" // @Success 200 {object} models.ReportAuthorResp // @router /addAudio [post] func (this *AudioController) AddAudio() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.AudioReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AudioName == "" { br.Msg = "音频名称为空" return } if req.AnalystId == 0 { br.Msg = "研究员ID为空" return } if req.AnalystName == "" { br.Msg = "研究员名称为空" return } if req.DurationMillisecond == 0 { br.Msg = "音频时长为空" return } if req.SrcUrl == "" { br.Msg = "音频地址为空" return } var err error audioInsert := &models.Media{ AuthorId: req.AnalystId, AuthorName: req.AnalystName, MediaType: models.Audio, Src: req.SrcUrl, MediaName: req.AudioName, SourceType: "mp3", MediaPlayMilliseconds: req.DurationMillisecond, PermissionIds: req.PermissionIds, PublishedTime: time.Now(), Deleted: 0, CreatedTime: time.Now(), } err = models.InsertMedia(audioInsert) if err != nil { br.Msg = "添加失败" br.ErrMsg = "音频新增失败,Err:" + err.Error() return } //audioInsert.Id = int(insertId) // 添加es go func(audio *models.ESMedia) { docId := strconv.Itoa(audio.MediaId) err = elastic.EsAddOrEditMedia("media_index", docId, audio) if err != nil { utils.FileLog.Info("音频记录es新增失败,Err:" + err.Error()) return } utils.FileLog.Info("音频记录es新增成功, pdfId:" + docId) }(audioInsert.ToView()) // 创建消息 _, _ = services.CreateMeta(audioInsert.AuthorName, audioInsert.AuthorId, audioInsert.Id, audioInsert.PublishedTime.Format(time.DateTime), models.AudioSourceType) br.Msg = "添加成功" br.Ret = 200 br.Success = true } // @Title 编辑音频 // // @Description 编辑音频 // @Success 200 {object} models.ReportAuthorResp // @router /editAudio [post] func (this *AudioController) EditAudio() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.AudioReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AudioId <= 0 { br.Msg = "音频ID为空" return } if req.AudioName == "" { br.Msg = "音频名称为空" return } if req.AnalystId == 0 { br.Msg = "研究员ID为空" return } if req.AnalystName == "" { br.Msg = "研究员名称为空" return } if req.DurationMillisecond == 0 { br.Msg = "音频时长为空" return } if req.SrcUrl == "" { br.Msg = "音频地址为空" return } var err error audioEdit := &models.Media{ Id: req.AudioId, AuthorId: req.AnalystId, AuthorName: req.AnalystName, MediaType: models.Audio, Src: req.SrcUrl, MediaName: req.AudioName, SourceType: "mp3", MediaPlayMilliseconds: req.DurationMillisecond, PermissionIds: req.PermissionIds, PublishedTime: time.Now(), Deleted: 0, CreatedTime: time.Now(), } _, err = models.UpdateMedia(audioEdit) if err != nil { br.Msg = "更新失败" br.ErrMsg = "音频更新失败,Err:" + err.Error() return } // 添加es go func(audio *models.ESMedia) { docId := strconv.Itoa(audio.MediaId) err = elastic.EsAddOrEditMedia("media_index", docId, audio) if err != nil { utils.FileLog.Info("音频记录es更新失败,Err:" + err.Error()) return } utils.FileLog.Info("音频记录es更新成功, pdfId:" + docId) }(audioEdit.ToView()) br.Msg = "编辑成功" br.Ret = 200 br.Success = true } // DeleteAudio // @Title 删除音频 // // @Description 删除音频 // @Success 200 {object} models.ReportAuthorResp // @router /deleteAudio [post] func (this *AudioController) DeleteAudio() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.AudioReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AudioId <= 0 { br.Msg = "音频ID为空" return } var err error audioDelete := &models.Media{ Id: req.AudioId, Deleted: 1, } err = models.DeleteMedia(audioDelete) if err != nil { br.Msg = "删除失败" br.ErrMsg = "音频删除失败,Err:" + err.Error() return } // 添加es go func(audio *models.ESMedia) { docId := strconv.Itoa(audio.MediaId) err = elastic.EsDeleteData("media_index", docId) if err != nil { utils.FileLog.Info("音频记录es删除失败,Err:" + err.Error()) return } utils.FileLog.Info("音频记录es删除成功, pdfId:" + docId) }(audioDelete.ToView()) br.Msg = "删除成功" br.Ret = 200 br.Success = true }