|
@@ -6,6 +6,7 @@ import (
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm"
|
|
|
"io"
|
|
"io"
|
|
|
|
|
+ "mime/multipart"
|
|
|
"os"
|
|
"os"
|
|
|
"path"
|
|
"path"
|
|
|
"speechAnalysis/constvar"
|
|
"speechAnalysis/constvar"
|
|
@@ -27,91 +28,114 @@ type AudioCtl struct{}
|
|
|
// @Tags 音频
|
|
// @Tags 音频
|
|
|
// @Summary 上传音频
|
|
// @Summary 上传音频
|
|
|
// @Produce application/json
|
|
// @Produce application/json
|
|
|
-// @Param file formData file true "音频文件"
|
|
|
|
|
|
|
+// @Param file formData file false "音频文件"
|
|
|
|
|
+// @Param files formData []file false "多个音频文件"
|
|
|
// @Success 200 {object} util.Response "成功"
|
|
// @Success 200 {object} util.Response "成功"
|
|
|
// @Router /api-sa/v1/audio/upload [post]
|
|
// @Router /api-sa/v1/audio/upload [post]
|
|
|
func (slf AudioCtl) Upload(c *gin.Context) {
|
|
func (slf AudioCtl) Upload(c *gin.Context) {
|
|
|
- _, header, err := c.Request.FormFile("file")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- util.ResponseFormat(c, code.RequestParamError, err.Error())
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- filename := path.Base(header.Filename)
|
|
|
|
|
|
|
|
|
|
- arr := strings.Split(filename, "_")
|
|
|
|
|
- if len(arr) != 6 {
|
|
|
|
|
- util.ResponseFormat(c, code.RequestParamError, "文件名称错误")
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ f := func(header *multipart.FileHeader) error {
|
|
|
|
|
+ logFormat := "%s,%s "
|
|
|
|
|
+ filename := path.Base(header.Filename)
|
|
|
|
|
|
|
|
- _, err = models.NewAudioSearch().SetName(filename).First()
|
|
|
|
|
- if err != gorm.ErrRecordNotFound {
|
|
|
|
|
- util.ResponseFormat(c, code.RequestParamError, "重复上传")
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ arr := strings.Split(filename, "_")
|
|
|
|
|
+ if len(arr) != 6 {
|
|
|
|
|
+ //util.ResponseFormat(c, code.RequestParamError, "文件名称错误")
|
|
|
|
|
+ return errors.New(fmt.Sprintf(logFormat, filename, "文件名称错误"))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- oss := upload.NewOss()
|
|
|
|
|
- filePath, filename, uploadErr := oss.UploadFile(header)
|
|
|
|
|
- if uploadErr != nil {
|
|
|
|
|
- logx.Errorf("upload audio err: %v", err)
|
|
|
|
|
- util.ResponseFormat(c, code.RequestParamError, "上传失败")
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ _, err := models.NewAudioSearch().SetName(filename).First()
|
|
|
|
|
+ if err != gorm.ErrRecordNotFound {
|
|
|
|
|
+ //util.ResponseFormat(c, code.RequestParamError, "重复上传")
|
|
|
|
|
+ return errors.New(fmt.Sprintf(logFormat, filename, "重复上传"))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- timeStr := arr[4] + strings.Split(arr[5], ".")[0]
|
|
|
|
|
|
|
+ oss := upload.NewOss()
|
|
|
|
|
+ filePath, filename, uploadErr := oss.UploadFile(header)
|
|
|
|
|
+ if uploadErr != nil {
|
|
|
|
|
+ logx.Errorf("upload audio err: %v", err)
|
|
|
|
|
+ //util.ResponseFormat(c, code.RequestParamError, "上传失败")
|
|
|
|
|
+ return errors.New(fmt.Sprintf(logFormat, filename, "上传失败"))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
|
|
|
|
|
|
|
+ timeStr := arr[4] + strings.Split(arr[5], ".")[0]
|
|
|
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- util.ResponseFormat(c, code.RequestParamError, "时间格式不对")
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
|
|
|
|
|
|
|
|
- audio := &models.Audio{
|
|
|
|
|
- Name: filename,
|
|
|
|
|
- Size: header.Size,
|
|
|
|
|
- FilePath: filePath,
|
|
|
|
|
- AudioStatus: constvar.AudioStatusUploadOk,
|
|
|
|
|
- LocomotiveNumber: arr[0],
|
|
|
|
|
- TrainNumber: arr[1],
|
|
|
|
|
- DriverNumber: arr[2],
|
|
|
|
|
- Station: arr[3],
|
|
|
|
|
- OccurrenceAt: t,
|
|
|
|
|
- IsFollowed: 0,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ //util.ResponseFormat(c, code.RequestParamError, "时间格式不对")
|
|
|
|
|
+ return errors.New(fmt.Sprintf(logFormat, filename, "上传失败"))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if err = models.NewAudioSearch().Create(audio); err != nil {
|
|
|
|
|
- util.ResponseFormat(c, code.SaveFail, "上传失败")
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- go func() {
|
|
|
|
|
|
|
+ audio := &models.Audio{
|
|
|
|
|
+ Name: filename,
|
|
|
|
|
+ Size: header.Size,
|
|
|
|
|
+ FilePath: filePath,
|
|
|
|
|
+ AudioStatus: constvar.AudioStatusUploadOk,
|
|
|
|
|
+ LocomotiveNumber: arr[0],
|
|
|
|
|
+ TrainNumber: arr[1],
|
|
|
|
|
+ DriverNumber: arr[2],
|
|
|
|
|
+ Station: arr[3],
|
|
|
|
|
+ OccurrenceAt: t,
|
|
|
|
|
+ IsFollowed: 0,
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- 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,
|
|
|
|
|
|
|
+ if err = models.NewAudioSearch().Create(audio); err != nil {
|
|
|
|
|
+ //util.ResponseFormat(c, code.SaveFail, "上传失败")
|
|
|
|
|
+ return errors.New(fmt.Sprintf(logFormat, filename, "上传失败"))
|
|
|
|
|
+ }
|
|
|
|
|
+ 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)
|
|
|
}
|
|
}
|
|
|
- _ = models.NewTrainInfoSearch().Create(info)
|
|
|
|
|
|
|
+ parent = *info
|
|
|
}
|
|
}
|
|
|
- parent = *info
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }()
|
|
|
|
|
|
|
|
|
|
- util.ResponseFormat(c, code.Success, "添加成功")
|
|
|
|
|
|
|
+ }()
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ var headers []*multipart.FileHeader
|
|
|
|
|
+ _, header, _ := c.Request.FormFile("file")
|
|
|
|
|
+ if header != nil {
|
|
|
|
|
+ headers = append(headers, header)
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(c.Request.MultipartForm.File["files"]) > 0 {
|
|
|
|
|
+ headers = c.Request.MultipartForm.File["files"]
|
|
|
|
|
+ }
|
|
|
|
|
+ var errs []error
|
|
|
|
|
+ for _, h := range headers {
|
|
|
|
|
+ if e := f(h); e != nil {
|
|
|
|
|
+ errs = append(errs, e)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(errs) > 0 {
|
|
|
|
|
+ var r strings.Builder
|
|
|
|
|
+ for _, e := range errs {
|
|
|
|
|
+ r.WriteString(e.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+ util.ResponseFormat(c, code.RequestParamError, r.String())
|
|
|
|
|
+ return
|
|
|
|
|
+ } else {
|
|
|
|
|
+ util.ResponseFormat(c, code.Success, "添加成功")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (slf AudioCtl) ParamsCheck(filename string) (err error) {
|
|
func (slf AudioCtl) ParamsCheck(filename string) (err error) {
|