From db7e3c55cef94793dcc0b651c90e5c8b21b8bcc9 Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期一, 17 六月 2024 19:28:13 +0800 Subject: [PATCH] watch preloads file to autoload --- controllers/audio.go | 187 +++++++++++++++++++++++----------------------- 1 files changed, 95 insertions(+), 92 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index f2d410c..0717d9b 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -9,6 +9,7 @@ "mime/multipart" "os" "path" + "path/filepath" "speechAnalysis/constvar" "speechAnalysis/extend/code" "speechAnalysis/extend/util" @@ -28,114 +29,108 @@ // @Tags 闊抽 // @Summary 涓婁紶闊抽 // @Produce application/json -// @Param file formData file false "闊抽鏂囦欢" -// @Param files formData []file false "澶氫釜闊抽鏂囦欢" +// @Param files formData []file false "澶氭枃浠朵笂浼�" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/upload [post] func (slf AudioCtl) Upload(c *gin.Context) { - - f := func(header *multipart.FileHeader) error { - logFormat := "%s锛�%s " + var headers []*multipart.FileHeader + if len(c.Request.MultipartForm.File["file"]) > 1 { + headers = c.Request.MultipartForm.File["file"] + } else { + util.ResponseFormat(c, code.RequestParamError, "鏂囦欢闇�瑕佷竴涓�瀵瑰簲") + return + } + audio := &models.Audio{} + for _, header := range headers { filename := path.Base(header.Filename) - arr := strings.Split(filename, "_") if len(arr) != 6 { - //util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍚嶇О閿欒") - return errors.New(fmt.Sprintf(logFormat, filename, "鏂囦欢鍚嶇О閿欒")) + 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, "閲嶅涓婁紶")) + util.ResponseFormat(c, code.RequestParamError, "閲嶅涓婁紶") + return } - 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, "涓婁紶澶辫触")) + util.ResponseFormat(c, code.RequestParamError, "涓婁紶澶辫触") + return } - - timeStr := arr[4] + strings.Split(arr[5], ".")[0] - - t, err := time.ParseInLocation("20060102150405", timeStr, time.Local) - - if err != nil { - //util.ResponseFormat(c, code.RequestParamError, "鏃堕棿鏍煎紡涓嶅") - return errors.New(fmt.Sprintf(logFormat, filename, "涓婁紶澶辫触")) - } - - 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 = 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) - } - parent = *info + if filepath.Ext(filename) == ".mp3" || filepath.Ext(filename) == ".wav" { + timeStr := arr[4] + strings.Split(arr[5], ".")[0] + t, err := time.ParseInLocation("20060102150405", timeStr, time.Local) + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鏃堕棿鏍煎紡涓嶅") + return } - - }() - 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) + audio.Name = filename + audio.Size = header.Size + audio.FilePath = filePath + audio.AudioStatus = constvar.AudioStatusUploadOk + audio.LocomotiveNumber = arr[0] + audio.TrainNumber = arr[1] + audio.DriverNumber = arr[2] + audio.Station = arr[3] + audio.OccurrenceAt = t + audio.IsFollowed = 0 + } + if filepath.Ext(filename) == ".txt" { + audio.TxtFilePath = filePath + //璇诲彇filepath鏂囦欢鍐呭鍒癰ts + bts, err := os.ReadFile(filePath) + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "璇诲彇鏂囦欢澶辫触") + return + } + //瑙f瀽 浜よ矾鍙�:123_鍏噷鏍�:321 + fileds := string(bts) + arr = strings.Split(fileds, "_") + if len(arr) > 1 { + util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍐呭鏍煎紡涓嶅") + return + } else { + RouteNumber := strings.Split(arr[0], ":") + KilometerMarker := strings.Split(arr[1], ":") + if len(RouteNumber) > 1 && len(KilometerMarker) > 1 { + audio.RouteNumber = RouteNumber[1] + audio.KilometerMarker = KilometerMarker[1] + } else { + util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍐呭鏍煎紡涓嶅") + return + } + } } } - 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, "娣诲姞鎴愬姛") + if err := models.NewAudioSearch().Create(audio); err != nil { + util.ResponseFormat(c, code.SaveFail, "涓婁紶澶辫触") return } + go func() { + var trainInfoNames = []string{audio.LocomotiveNumber, audio.TrainNumber, audio.Station} + 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) + } + parent = *info + } + }() } func (slf AudioCtl) ParamsCheck(filename string) (err error) { @@ -289,13 +284,22 @@ util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触") return } - - if audio.FilePath == "" { + filepath := "" + if params.Filetype == 1 { + filepath = audio.FilePath + c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 + } + if params.Filetype == 2 { + filepath = audio.TxtFilePath + //璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷 + c.Header("Content-Type", "text/plain") + } + if filepath == "" { util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触") return } - file, err := os.Open(audio.FilePath) + file, err := os.Open(filepath) if err != nil { util.ResponseFormat(c, code.InternalError, "鏂囦欢鎵撳紑澶辫触") return @@ -310,7 +314,6 @@ c.Header("Content-Disposition", "inline; filename="+audio.Name) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� c.Header("Content-Length", fmt.Sprint(fileInfo.Size())) - c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 if _, err := io.Copy(c.Writer, file); err != nil { util.ResponseFormat(c, code.InternalError, "鏂囦欢浼犺緭澶辫触") -- Gitblit v1.8.0