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
---
request/audio.go | 3
models/audio.go | 5 +
docs/swagger.yaml | 6 -
controllers/audio.go | 187 +++++++++++++++++++++++-----------------------
docs/docs.go | 8 -
docs/swagger.json | 8 -
6 files changed, 104 insertions(+), 113 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, "鏂囦欢浼犺緭澶辫触")
diff --git a/docs/docs.go b/docs/docs.go
index 0a3a707..b9de7a5 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -466,18 +466,12 @@
"summary": "涓婁紶闊抽",
"parameters": [
{
- "type": "file",
- "description": "闊抽鏂囦欢",
- "name": "file",
- "in": "formData"
- },
- {
"type": "array",
"items": {
"type": "file"
},
"collectionFormat": "csv",
- "description": "澶氫釜闊抽鏂囦欢",
+ "description": "澶氭枃浠朵笂浼�",
"name": "files",
"in": "formData"
}
diff --git a/docs/swagger.json b/docs/swagger.json
index e04646a..8864966 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -455,18 +455,12 @@
"summary": "涓婁紶闊抽",
"parameters": [
{
- "type": "file",
- "description": "闊抽鏂囦欢",
- "name": "file",
- "in": "formData"
- },
- {
"type": "array",
"items": {
"type": "file"
},
"collectionFormat": "csv",
- "description": "澶氫釜闊抽鏂囦欢",
+ "description": "澶氭枃浠朵笂浼�",
"name": "files",
"in": "formData"
}
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index d463d34..42c9890 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -484,12 +484,8 @@
/api-sa/v1/audio/upload:
post:
parameters:
- - description: 闊抽鏂囦欢
- in: formData
- name: file
- type: file
- collectionFormat: csv
- description: 澶氫釜闊抽鏂囦欢
+ description: 澶氭枃浠朵笂浼�
in: formData
items:
type: file
diff --git a/models/audio.go b/models/audio.go
index f46be7c..6bbb698 100644
--- a/models/audio.go
+++ b/models/audio.go
@@ -15,12 +15,15 @@
gorm.Model
Name string `gorm:"index;type:varchar(255);not null;default:'';comment:闊抽鍚嶇О" json:"name"` // 闊抽鍚嶇О
Size int64 `gorm:"type:int;not null;default:0;comment:鏂囦欢澶у皬" json:"size"` // 闊抽澶у皬
- FilePath string `gorm:"type:varchar(255);not null;default:'';comment:闊抽璺緞" json:"-"` //闊抽璺緞 // 闊抽璺緞
+ FilePath string `gorm:"type:varchar(255);not null;default:'';comment:闊抽璺緞" json:"-"` //闊抽璺緞
+ TxtFilePath string `gorm:"type:varchar(255);not null;default:'';comment:txt璺緞" json:"-"` //txt璺緞
AudioStatus constvar.AudioStatus `gorm:"type:tinyint;not null;default:0;comment:鐘舵��" json:"audioStatus"` // 闊抽鐘舵��
LocomotiveNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:鏈鸿溅鍙�" json:"locomotiveNumber"` // 鏈鸿溅鍙�
TrainNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:杞︽" json:"trainNumber"` // 杞︽
DriverNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:鍙告満鍙�" json:"driverNumber"` // 鍙告満鍙�
Station string `gorm:"index;type:varchar(255);not null;default:'';comment:杞︾珯鍙�" json:"station"` // 杞︾珯
+ RouteNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:浜よ矾鍙�" json:"station"` // 浜よ矾鍙�
+ KilometerMarker string `gorm:"index;type:varchar(255);not null;default:'';comment:鍏噷鏍�" json:"station"` // 鍏噷鏍�
OccurrenceAt time.Time `json:"-"`
OccurrenceTime string `json:"occurrenceTime" gorm:"-"`
IsFollowed constvar.BoolType `gorm:"type:tinyint;not null;default:2;comment:鏄惁鍏虫敞"` //鏄惁鍏虫敞 1鍏虫敞 2鏈叧娉�
diff --git a/request/audio.go b/request/audio.go
index 1fe4ef9..3a69c72 100644
--- a/request/audio.go
+++ b/request/audio.go
@@ -19,7 +19,8 @@
}
type ProcessAudio struct {
- ID uint `json:"id" form:"id" binding:"required"`
+ ID uint `json:"id" form:"id" binding:"required"`
+ Filetype int `json:"fileType" form:"fileType"`
}
type BatchProcessAudio struct {
--
Gitblit v1.8.0