From eb1d2e16e0ec4721ba48d101ced1e4b994b463fd Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期三, 12 六月 2024 16:59:21 +0800 Subject: [PATCH] multi files upload --- controllers/audio.go | 178 +++++++++++++++++++++++++++++++++------------------------- 1 files changed, 101 insertions(+), 77 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 7c58dd9..c1b2c2f 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -6,6 +6,7 @@ "github.com/gin-gonic/gin" "gorm.io/gorm" "io" + "mime/multipart" "os" "path" "speechAnalysis/constvar" @@ -27,91 +28,114 @@ // @Tags 闊抽 // @Summary 涓婁紶闊抽 // @Produce application/json -// @Param file formData file true "闊抽鏂囦欢" +// @Param file 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) { - _, header, err := c.Request.FormFile("file") - if err != nil { - util.ResponseFormat(c, code.RequestParamError, err.Error()) - return - } - filename := path.Base(header.Filename) + f := func(header *multipart.FileHeader) error { + logFormat := "%s锛�%s " + filename := path.Base(header.Filename) - arr := strings.Split(filename, "_") - if len(arr) != 6 { - util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍚嶇О閿欒") - return - } - - _, err = models.NewAudioSearch().SetName(filename).First() - if err != gorm.ErrRecordNotFound { - 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 - } - - 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 - } - - 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 - } - 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 + arr := strings.Split(filename, "_") + if len(arr) != 6 { + //util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍚嶇О閿欒") + return errors.New(fmt.Sprintf(logFormat, filename, "鏂囦欢鍚嶇О閿欒")) } - }() + _, 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.Success, "娣诲姞鎴愬姛") + 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, "涓婁紶澶辫触")) + } + + 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 + } + + }() + 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) { -- Gitblit v1.8.0