Browse Source

fix:章节无文本内容时,音频不生成

Roc 8 months ago
parent
commit
fe24870512
2 changed files with 22 additions and 39 deletions
  1. 3 8
      services/report_v2.go
  2. 19 31
      services/video.go

+ 3 - 8
services/report_v2.go

@@ -1090,7 +1090,7 @@ func PublishReport(reportId int, reportUrl string, sysUser *system.Admin) (tips
 		go func() {
 			// 生成音频
 			if reportInfo.VideoUrl == "" {
-				_ = CreateVideo(reportInfo)
+				go UpdateReportVideo(reportInfo)
 			}
 			// 更新报告Es
 			_ = UpdateReportEs(reportId, 2)
@@ -1162,7 +1162,6 @@ func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *
 		return
 	}
 
-	reportChapterIdList := make([]int, 0)
 	// 获取报告中的所有章节列表
 	chapterList, err := models.GetChapterListByReportId(reportId)
 	if err != nil {
@@ -1175,7 +1174,6 @@ func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *
 			err = errors.New(tips)
 			return
 		}
-		reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
 	}
 
 	// 根据审批开关及审批流判断当前报告状态
@@ -1235,11 +1233,8 @@ func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *
 		return
 	}
 	// 生成章节音频
-	if len(reportChapterIdList) > 0 {
-		go func() {
-			_ = UpdateChaptersVideo(reportChapterIdList)
-		}()
-	}
+	go UpdateReportVideo(reportInfo)
+
 	// 更新报告ES
 	go func() {
 		_ = UpdateReportEs(reportInfo.Id, 2)

+ 19 - 31
services/video.go

@@ -78,9 +78,11 @@ func CreateVideo(report *models.Report) (err error) {
 
 	saveName := utils.GetRandStringNoSpecialChar(16) + ".mp3"
 	savePath := "./" + saveName
-	//if utils.FileIsExist(savePath) {
-	//	os.Remove(savePath)
-	//}
+	defer func() {
+		if utils.FileIsExist(savePath) {
+			os.Remove(savePath)
+		}
+	}()
 
 	// 如果没有文本内容,那么就不生成了
 	videoContent = strings.TrimSpace(videoContent)
@@ -106,20 +108,6 @@ func CreateVideo(report *models.Report) (err error) {
 	}
 
 	uploadUrl := ``
-	//上传到阿里云 和 minio
-	//if utils.ObjectStorageClient == "minio" {
-	//	uploadUrl, err = UploadAudioToMinIo(saveName, savePath)
-	//	if err != nil {
-	//		err = errors.New("UploadAudioAliyun Err:" + err.Error())
-	//		return
-	//	}
-	//} else {
-	//	uploadUrl, err = UploadAudioAliyun(saveName, savePath)
-	//	if err != nil {
-	//		err = errors.New("UploadAudioAliyun Err:" + err.Error())
-	//		return
-	//	}
-	//}
 	ossClient := NewOssClient()
 	if ossClient == nil {
 		err = fmt.Errorf("初始化OSS服务失败")
@@ -145,12 +133,9 @@ func CreateVideo(report *models.Report) (err error) {
 		}
 	}
 
-	if playSeconds > 0 {
-		if utils.FileIsExist(savePath) {
-			os.Remove(savePath)
-		}
-	}
+	// 修改报告的音频信息
 	err = models.ModifyReportVideo(report.Id, uploadUrl, videoName, sizeStr, playSeconds)
+
 	return
 }
 
@@ -242,6 +227,7 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "CreateReportVideo, reportTitle:" + reportTitle +", Err:"+err.Error(), utils.EmailSendToUsers)
 		}
 	}()
+
 	// 如果没有文本内容,那么就不生成了
 	reportContent = strings.TrimSpace(reportContent)
 	if reportContent == `` {
@@ -297,11 +283,19 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 	param.Data.Status = 2
 	videoContent := doc.Text()
 
+	// 如果没有文本内容,那么就不生成了
+	videoContent = strings.TrimSpace(videoContent)
+	if videoContent == `` {
+		return
+	}
+
 	saveName := utils.GetRandStringNoSpecialChar(16) + ".mp3"
 	savePath := "./" + saveName
-	//if utils.FileIsExist(savePath) {
-	//	os.Remove(savePath)
-	//}
+	defer func() {
+		if utils.FileIsExist(savePath) {
+			os.Remove(savePath)
+		}
+	}()
 	contentArr := GetChineseCount(videoContent)
 	for _, v := range contentArr {
 		newText := v
@@ -358,11 +352,5 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 		}
 	}
 
-	if playSeconds > 0 {
-		if utils.FileIsExist(savePath) {
-			os.Remove(savePath)
-		}
-	}
-
 	return
 }