Browse Source

fix: 智能研报音频上传

hsun 1 year ago
parent
commit
8643fed8af
1 changed files with 31 additions and 33 deletions
  1. 31 33
      controllers/smart_report/smart_report.go

+ 31 - 33
controllers/smart_report/smart_report.go

@@ -1094,18 +1094,21 @@ func (this *SmartReportController) VoiceUpload() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
-	f, h, err := this.GetFile("file")
-	if err != nil {
-		br.Msg = "获取资源信息失败"
-		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
-		return
-	}
 	reportId, _ := this.GetInt("SmartReportId")
 	if reportId <= 0 {
 		br.Msg = "参数有误"
 		br.ErrMsg = fmt.Sprintf("参数有误, SmartReportId: %d", reportId)
 		return
 	}
+	f, h, err := this.GetFile("file")
+	if err != nil {
+		br.Msg = "获取资源信息失败"
+		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
+		return
+	}
+	defer func() {
+		_ = f.Close()
+	}()
 
 	reportOb := new(smart_report.SmartReport)
 	item, e := reportOb.GetItemById(reportId)
@@ -1120,47 +1123,42 @@ func (this *SmartReportController) VoiceUpload() {
 	}
 
 	ext := path.Ext(h.Filename)
-	dateDir := time.Now().Format("20060102")
-	uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
-	err = os.MkdirAll(uploadDir, 0777)
-	if err != nil {
+	uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
+	if e = os.MkdirAll(uploadDir, 0766); e != nil {
 		br.Msg = "存储目录创建失败"
-		br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
+		br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
 		return
 	}
-	randStr := utils.GetRandStringNoSpecialChar(28)
-	fileName := randStr + ext
-	fpath := uploadDir + "/" + fileName
-	defer f.Close() //关闭上传文件
-	err = this.SaveToFile("file", fpath)
-	if err != nil {
-		br.Msg = "文件上传失败"
-		br.ErrMsg = "文件上传失败,Err:" + err.Error()
+	ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
+	filePath := uploadDir + "/" + ossFileName
+	if e = this.SaveToFile("file", filePath); e != nil {
+		br.Msg = "文件保存失败"
+		br.ErrMsg = "文件保存失败, Err:" + e.Error()
 		return
 	}
+	defer func() {
+		_ = os.Remove(filePath)
+	}()
+	ossDir := "static/audio/"
 
 	resourceUrl := ``
 	//上传到阿里云 和 minio
 	if utils.ObjectStorageClient == "minio" {
-		resourceUrl, err = services.UploadAudioToMinIo(fileName, fpath)
-		if err != nil {
+		resourceUrl, e = services.UploadMinIoToDir(ossFileName, filePath, ossDir, "")
+		if e != nil {
 			br.Msg = "文件上传失败"
-			br.ErrMsg = "文件上传失败,Err:" + err.Error()
+			br.ErrMsg = "文件上传失败, Err:" + e.Error()
 			return
 		}
 	} else {
-		resourceUrl, err = services.UploadAudioAliyun(fileName, fpath)
-		if err != nil {
+		resourceUrl, e = services.UploadAliyunToDir(ossFileName, filePath, ossDir, "")
+		if e != nil {
 			br.Msg = "文件上传失败"
-			br.ErrMsg = "文件上传失败,Err:" + err.Error()
+			br.ErrMsg = "文件上传失败, Err:" + e.Error()
 			return
 		}
 	}
 
-	defer func() {
-		_ = os.Remove(fpath)
-	}()
-
 	resource := new(models.Resource)
 	resource.ResourceUrl = resourceUrl
 	resource.ResourceType = 2
@@ -1171,10 +1169,10 @@ func (this *SmartReportController) VoiceUpload() {
 		br.ErrMsg = "资源上传失败,Err:" + err.Error()
 		return
 	}
-	fmt.Println(fpath)
-	playSeconds, err := mp3duration.Calculate(fpath)
+	//fmt.Println(filePath)
+	playSeconds, err := mp3duration.Calculate(filePath)
 	if playSeconds <= 0 {
-		playSeconds, err = utils.GetVideoPlaySeconds(fpath)
+		playSeconds, err = utils.GetVideoPlaySeconds(filePath)
 		if err != nil {
 			br.Msg = "获取音频时间失败"
 			br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
@@ -1183,7 +1181,7 @@ func (this *SmartReportController) VoiceUpload() {
 	}
 	createTime := item.CreateTime.Format("0102")
 	videoName := item.Title + "(" + createTime + ")"
-	fileBody, err := ioutil.ReadFile(fpath)
+	fileBody, err := ioutil.ReadFile(filePath)
 	videoSize := len(fileBody)
 	sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
 	sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)