Roc 9 сар өмнө
parent
commit
fb7c2ad9ce
2 өөрчлөгдсөн 156 нэмэгдсэн , 16 устгасан
  1. 59 16
      controllers/voice.go
  2. 97 0
      services/report_v2.go

+ 59 - 16
controllers/voice.go

@@ -7,7 +7,6 @@ import (
 	"eta/eta_api/utils"
 	"fmt"
 	"github.com/kgiannakakis/mp3duration/src/mp3duration"
-	"github.com/rdlucklib/rdluck_tools/file"
 	"github.com/rdlucklib/rdluck_tools/http"
 	"io/ioutil"
 	"os"
@@ -194,36 +193,80 @@ func (this *VoiceCommonController) Download() {
 		br.ErrMsg = "获取,ReportId,Err:" + err.Error()
 		return
 	}
-	report, err := models.GetReportById(reportId)
+	reportInfo, err := models.GetReportById(reportId)
 	if err != nil {
 		br.Msg = "获取信息失败"
 		br.ErrMsg = "获取信息失败,Err:" + err.Error()
 		return
 	}
-	savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
-	fileBody, err := http.Get(report.VideoUrl)
-	if err != nil {
-		br.Msg = "获取信息失败"
-		br.ErrMsg = "获取信息失败,Err:" + err.Error()
-		return
+
+	savePath, fileName, err, errMsg := services.DownloadVoice(reportInfo)
+	// 如果生成了文件,那么就在退出的时候,删除该文件
+	if savePath != `` {
+		defer func() {
+			os.Remove(savePath)
+		}()
 	}
-	err = file.SaveFile(fileBody, savePath)
+
 	if err != nil {
-		br.Msg = "保存信息失败"
-		br.ErrMsg = "保存信息失败,Err:" + err.Error()
+		br.Msg = errMsg
+		br.ErrMsg = "下载失败,Err:" + err.Error()
 		return
 	}
-	fileName := report.VideoName + ".mp3"
-	this.Ctx.Output.Download(savePath, fileName)
-	defer func() {
-		os.Remove(savePath)
-	}()
+
+	if savePath != `` {
+		this.Ctx.Output.Download(savePath, fileName)
+	}
+
 	br.Ret = 200
 	br.Msg = "下载成功"
 	br.Success = true
+
 	return
 }
 
+//func (this *VoiceCommonController) Download() {
+//	br := new(models.BaseResponse).Init()
+//	defer func() {
+//		this.Data["json"] = br
+//		this.ServeJSON()
+//	}()
+//	reportId, err := this.GetInt("ReportId")
+//	if err != nil {
+//		br.Msg = "参数错误"
+//		br.ErrMsg = "获取,ReportId,Err:" + err.Error()
+//		return
+//	}
+//	report, err := models.GetReportById(reportId)
+//	if err != nil {
+//		br.Msg = "获取信息失败"
+//		br.ErrMsg = "获取信息失败,Err:" + err.Error()
+//		return
+//	}
+//	savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
+//	fileBody, err := http.Get(report.VideoUrl)
+//	if err != nil {
+//		br.Msg = "获取信息失败"
+//		br.ErrMsg = "获取信息失败,Err:" + err.Error()
+//		return
+//	}
+//	err = file.SaveFile(fileBody, savePath)
+//	if err != nil {
+//		br.Msg = "保存信息失败"
+//		br.ErrMsg = "保存信息失败,Err:" + err.Error()
+//		return
+//	}
+//	fileName := report.VideoName + ".mp3"
+//	this.Ctx.Output.Download(savePath, fileName)
+//	defer func() {
+//		os.Remove(savePath)
+//	}()
+//	br.Ret = 200
+//	br.Msg = "下载成功"
+//	br.Success = true
+//	return
+//}
+
 // ReportChapterDownload
 // @Title 报告章节列表音频下载
 // @Description 音频下载接口

+ 97 - 0
services/report_v2.go

@@ -1,11 +1,17 @@
 package services
 
 import (
+	"archive/zip"
 	"errors"
 	"eta/eta_api/models"
 	"eta/eta_api/models/report"
 	"eta/eta_api/models/system"
 	"eta/eta_api/utils"
+	"fmt"
+	"github.com/rdlucklib/rdluck_tools/file"
+	"github.com/rdlucklib/rdluck_tools/http"
+	"os"
+	"path"
 	"strconv"
 	"time"
 )
@@ -740,3 +746,94 @@ func DelChapter(reportInfo *models.Report, reportChapterInfo *models.ReportChapt
 
 	return
 }
+
+// DownloadVoice
+// @Description:  下载报告音频文件
+// @author: Roc
+// @datetime 2024-06-13 15:36:46
+// @param reportInfo *models.ReportDetail
+// @return savePath string
+// @return fileName string
+// @return err error
+// @return errMsg string
+func DownloadVoice(reportInfo *models.ReportDetail) (savePath, fileName string, err error, errMsg string) {
+	errMsg = `下载失败`
+	// 如果报告有音频,那么下载音频
+	if reportInfo.VideoUrl != `` {
+		savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
+		fileName = reportInfo.VideoName + ".mp3"
+		fileBody, tmpErr := http.Get(reportInfo.VideoUrl)
+		if tmpErr != nil {
+			err = tmpErr
+			errMsg = "音频下载失败"
+			return
+		}
+		err = file.SaveFile(fileBody, savePath)
+		if err != nil {
+			errMsg = "音频保存失败"
+			return
+		}
+	}
+
+	// 如果是章节报告,那么就下载压缩包
+	if reportInfo.HasChapter == 1 {
+		videoList, tmpErr := models.GetReportChapterVideoList(reportInfo.Id)
+		if tmpErr != nil {
+			err = tmpErr
+			errMsg = "获取音频列表失败"
+			return
+		}
+
+		if len(videoList) > 0 {
+			// 创建zip
+			savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
+			fileName = reportInfo.VideoName + ".zip"
+
+			zipFile, tmpErr := os.Create(savePath)
+			if tmpErr != nil {
+				err = tmpErr
+				return
+			}
+			zipWriter := zip.NewWriter(zipFile)
+			// 生成zip过程中报错关闭
+			defer func() {
+				if err != nil {
+					zipWriter.Close()
+					zipFile.Close()
+				}
+			}()
+
+			// 获取音频,写入zip
+			for i := 0; i < len(videoList); i++ {
+				item := videoList[i]
+				if item.VideoName == "" || item.VideoUrl == "" {
+					continue
+				}
+				// 音频文件后缀
+				ext := path.Ext(item.VideoUrl)
+				ioWriter, tmpErr := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
+				if tmpErr != nil {
+					err = tmpErr
+					if os.IsPermission(err) {
+						fmt.Println("权限不足: ", err)
+						return
+					}
+					return
+				}
+
+				var content []byte
+				content, err = http.Get(item.VideoUrl)
+				if err != nil {
+					content = []byte("")
+				}
+
+				ioWriter.Write(content)
+			}
+			// 生成zip后关闭,否则下载文件会损坏
+			zipWriter.Close()
+			zipFile.Close()
+		}
+	}
+
+	return
+}