From 500985cf7556808ee42e2e9e3066bb4648a8e534 Mon Sep 17 00:00:00 2001
From: yinbangzhong <zhongbangyin@126.com>
Date: 星期二, 18 六月 2024 14:28:16 +0800
Subject: [PATCH] watch preloads file to autoload

---
 service/process.go   |  241 +++++++++++++++++++++++++--------------
 docs/swagger.yaml    |   14 ++
 controllers/audio.go |   18 +-
 docs/docs.go         |   20 +++
 docs/swagger.json    |   20 +++
 5 files changed, 213 insertions(+), 100 deletions(-)

diff --git a/controllers/audio.go b/controllers/audio.go
index 0717d9b..8f5f108 100644
--- a/controllers/audio.go
+++ b/controllers/audio.go
@@ -7,6 +7,7 @@
 	"gorm.io/gorm"
 	"io"
 	"mime/multipart"
+	"net/url"
 	"os"
 	"path"
 	"path/filepath"
@@ -28,14 +29,16 @@
 // Upload
 // @Tags      闊抽
 // @Summary   涓婁紶闊抽
+// @Accept     multipart/form-data
 // @Produce   application/json
-// @Param files formData []file false "澶氭枃浠朵笂浼�"
+// @Param file formData []file false "澶氭枃浠朵笂浼�"
 // @Success   200 {object} util.Response "鎴愬姛"
 // @Router    /api-sa/v1/audio/upload [post]
 func (slf AudioCtl) Upload(c *gin.Context) {
 	var headers []*multipart.FileHeader
-	if len(c.Request.MultipartForm.File["file"]) > 1 {
-		headers = c.Request.MultipartForm.File["file"]
+	files, _ := c.MultipartForm()
+	if len(files.File["file"]) > 1 {
+		headers = files.File["file"]
 	} else {
 		util.ResponseFormat(c, code.RequestParamError, "鏂囦欢闇�瑕佷竴涓�瀵瑰簲")
 		return
@@ -89,7 +92,7 @@
 			//瑙f瀽 浜よ矾鍙�:123_鍏噷鏍�:321
 			fileds := string(bts)
 			arr = strings.Split(fileds, "_")
-			if len(arr) > 1 {
+			if len(arr) != 2 {
 				util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍐呭鏍煎紡涓嶅")
 				return
 			} else {
@@ -291,8 +294,9 @@
 	}
 	if params.Filetype == 2 {
 		filepath = audio.TxtFilePath
-		//璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷
-		c.Header("Content-Type", "text/plain")
+		//璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷,閬垮厤涓枃涔辩爜
+		c.Header("Content-Type", "text/plain; charset=utf-8")
+		c.Header("Content-Transfer-Encoding", "binary")
 	}
 	if filepath == "" {
 		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
@@ -312,7 +316,7 @@
 		return
 	}
 
-	c.Header("Content-Disposition", "inline; filename="+audio.Name) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮�
+	c.Header("Content-Disposition", "inline; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮�
 	c.Header("Content-Length", fmt.Sprint(fileInfo.Size()))
 
 	if _, err := io.Copy(c.Writer, file); err != nil {
diff --git a/docs/docs.go b/docs/docs.go
index b9de7a5..cad5c53 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -117,6 +117,11 @@
                 "parameters": [
                     {
                         "type": "integer",
+                        "name": "fileType",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
                         "name": "id",
                         "in": "query",
                         "required": true
@@ -196,6 +201,11 @@
                 ],
                 "summary": "闊抽璇︽儏锛屽惈瑙f瀽缁撴灉",
                 "parameters": [
+                    {
+                        "type": "integer",
+                        "name": "fileType",
+                        "in": "query"
+                    },
                     {
                         "type": "integer",
                         "name": "id",
@@ -457,6 +467,9 @@
         },
         "/api-sa/v1/audio/upload": {
             "post": {
+                "consumes": [
+                    "multipart/form-data"
+                ],
                 "produces": [
                     "application/json"
                 ],
@@ -472,7 +485,7 @@
                         },
                         "collectionFormat": "csv",
                         "description": "澶氭枃浠朵笂浼�",
-                        "name": "files",
+                        "name": "file",
                         "in": "formData"
                     }
                 ],
@@ -697,7 +710,7 @@
                     "type": "integer"
                 },
                 "station": {
-                    "description": "杞︾珯",
+                    "description": "鍏噷鏍�",
                     "type": "string"
                 },
                 "trainNumber": {
@@ -822,6 +835,9 @@
                 "id"
             ],
             "properties": {
+                "fileType": {
+                    "type": "integer"
+                },
                 "id": {
                     "type": "integer"
                 }
diff --git a/docs/swagger.json b/docs/swagger.json
index 8864966..11b18d9 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -106,6 +106,11 @@
                 "parameters": [
                     {
                         "type": "integer",
+                        "name": "fileType",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
                         "name": "id",
                         "in": "query",
                         "required": true
@@ -185,6 +190,11 @@
                 ],
                 "summary": "闊抽璇︽儏锛屽惈瑙f瀽缁撴灉",
                 "parameters": [
+                    {
+                        "type": "integer",
+                        "name": "fileType",
+                        "in": "query"
+                    },
                     {
                         "type": "integer",
                         "name": "id",
@@ -446,6 +456,9 @@
         },
         "/api-sa/v1/audio/upload": {
             "post": {
+                "consumes": [
+                    "multipart/form-data"
+                ],
                 "produces": [
                     "application/json"
                 ],
@@ -461,7 +474,7 @@
                         },
                         "collectionFormat": "csv",
                         "description": "澶氭枃浠朵笂浼�",
-                        "name": "files",
+                        "name": "file",
                         "in": "formData"
                     }
                 ],
@@ -686,7 +699,7 @@
                     "type": "integer"
                 },
                 "station": {
-                    "description": "杞︾珯",
+                    "description": "鍏噷鏍�",
                     "type": "string"
                 },
                 "trainNumber": {
@@ -811,6 +824,9 @@
                 "id"
             ],
             "properties": {
+                "fileType": {
+                    "type": "integer"
+                },
                 "id": {
                     "type": "integer"
                 }
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 42c9890..3d2642c 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -89,7 +89,7 @@
         description: 闊抽澶у皬
         type: integer
       station:
-        description: 杞︾珯
+        description: 鍏噷鏍�
         type: string
       trainNumber:
         description: 杞︽
@@ -170,6 +170,8 @@
     type: object
   request.ProcessAudio:
     properties:
+      fileType:
+        type: integer
       id:
         type: integer
     required:
@@ -268,6 +270,9 @@
     get:
       parameters:
       - in: query
+        name: fileType
+        type: integer
+      - in: query
         name: id
         required: true
         type: integer
@@ -313,6 +318,9 @@
   /api-sa/v1/audio/info:
     get:
       parameters:
+      - in: query
+        name: fileType
+        type: integer
       - in: query
         name: id
         required: true
@@ -483,13 +491,15 @@
       - 闊抽
   /api-sa/v1/audio/upload:
     post:
+      consumes:
+      - multipart/form-data
       parameters:
       - collectionFormat: csv
         description: 澶氭枃浠朵笂浼�
         in: formData
         items:
           type: file
-        name: files
+        name: file
         type: array
       produces:
       - application/json
diff --git a/service/process.go b/service/process.go
index 0b67c93..4470791 100644
--- a/service/process.go
+++ b/service/process.go
@@ -165,6 +165,10 @@
 	if mkdirErr != nil {
 		logx.Errorf("function os.MkdirAll() err:%v", mkdirErr)
 	}
+	mkdirErr1 := os.MkdirAll(conf.LocalConf.StorePath, os.ModePerm)
+	if mkdirErr1 != nil {
+		logx.Errorf("function os.MkdirAll() err:%v", mkdirErr1)
+	}
 	//鏂囦欢澶逛笅鏂板闊抽鏂囦欢鏃惰Е鍙�
 	watcher, err := fsnotify.NewWatcher()
 	if err != nil {
@@ -175,6 +179,111 @@
 	if err != nil {
 		log.Fatal(err)
 	}
+
+	audoF := func(eventName, fileName string, audio *models.Audio) bool {
+		time.Sleep(time.Second * 1)
+		//璁剧疆鏂囦欢璁块棶鏉冮檺
+		err = os.Chmod(eventName, 0777)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", eventName, "璁剧疆鏂囦欢鏉冮檺澶辫触"))
+		}
+
+		//鏍¢獙鏂囦欢鍛藉悕
+		arr := strings.Split(fileName, "_")
+		if len(arr) != 6 {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏂囦欢鍚嶇О閿欒"))
+			return false
+		}
+		timeStr := arr[4] + strings.Split(arr[5], ".")[0]
+		t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏃堕棿鏍煎紡涓嶅"))
+		}
+
+		//鏌ラ噸
+		_, err = models.NewAudioSearch().SetName(fileName).First()
+		if err != gorm.ErrRecordNotFound {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "閲嶅涓婁紶"))
+			return false
+		}
+
+		//灏嗘枃浠剁Щ鍔ㄥ埌uploads鏂囦欢澶逛笅
+		//鍒ゆ柇storePath涓湯灏炬槸鍚﹀甫
+		var src string
+		if strings.HasSuffix(conf.LocalConf.StorePath, "/") {
+			src = conf.LocalConf.StorePath + fileName
+		} else {
+			src = conf.LocalConf.StorePath + "/" + fileName
+		}
+		err = os.Rename(eventName, src)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "绉诲姩鏂囦欢澶辫触"))
+			return false
+		}
+		// 璇诲彇鏂囦欢澶у皬
+		fileInfo, err := os.Stat(src)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鑾峰彇鏂囦欢澶у皬澶辫触"))
+			return false
+		}
+		size := fileInfo.Size()
+		fmt.Println("fileName:", fileName, "size:", size, "src1", src)
+
+		audio.Name = fileName
+		audio.Size = size
+		audio.FilePath = src
+		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
+		return true
+	}
+
+	txtF := func(filePath string, audio *models.Audio) bool {
+		fileName := filepath.Base(filePath)
+		//璇诲彇filepath鏂囦欢鍐呭鍒癰ts
+		bts, err := os.ReadFile(filePath)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", filePath, "璇诲彇txt鏂囦欢澶辫触"))
+			return false
+		}
+		//瑙f瀽 浜よ矾鍙�:123_鍏噷鏍�:321
+		fileds := string(bts)
+		arr := strings.Split(fileds, "_")
+		if len(arr) != 2 {
+			logx.Errorf(fmt.Sprintf("%s:%s", filePath, "璇诲彇txt鏂囦欢鍐呭鏍煎紡涓嶅"))
+			return false
+		} 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 {
+				logx.Errorf(fmt.Sprintf("%s:%s", filePath, "鏂囦欢鍐呭鏍煎紡涓嶅"))
+				return false
+			}
+		}
+		var src string
+		if strings.HasSuffix(conf.LocalConf.StorePath, "/") {
+			src = conf.LocalConf.StorePath + fileName
+		} else {
+			src = conf.LocalConf.StorePath + "/" + fileName
+		}
+		err = os.Rename(filePath, src)
+		if err != nil {
+			logx.Errorf(fmt.Sprintf("%s:%s", fileName, "绉诲姩鏂囦欢澶辫触"))
+			return false
+		}
+		audio.TxtFilePath = src
+		return true
+	}
+
+	//鎴愬鍙橀噺
+	pair := make(map[string]string)
 FOR:
 	for {
 		select {
@@ -186,98 +295,56 @@
 				continue
 			}
 			if event.Op&fsnotify.Create == fsnotify.Create {
-				// 鍒ゆ柇鏂囦欢绫诲瀷鏄惁涓�.mp3鎴�.wav
-				if filepath.Ext(event.Name) == ".mp3" || filepath.Ext(event.Name) == ".wav" {
-					time.Sleep(time.Second * 1)
-					//璁剧疆鏂囦欢璁块棶鏉冮檺
-					err = os.Chmod(event.Name, 0777)
-					if err != nil {
-						logx.Errorf(fmt.Sprintf("%s:%s", event.Name, "璁剧疆鏂囦欢鏉冮檺澶辫触"))
+				// 鏂囦欢鍚�
+				fileName := filepath.Base(event.Name)
+				//鑾峰彇涓嶅甫鎵╁睍鍚嶇殑鏂囦欢鍚�
+				name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
+				//鍒ゆ柇鏂囦欢鍦╬air涓�
+				if _, ok := pair[name]; !ok {
+					pair[name] = event.Name
+				} else {
+					audio := &models.Audio{}
+					isOk := true
+					// 鍒ゆ柇鏂囦欢绫诲瀷鏄惁涓�.mp3鎴�.wav
+					if filepath.Ext(event.Name) == ".mp3" || filepath.Ext(event.Name) == ".wav" {
+						isOk = audoF(event.Name, fileName, audio) && txtF(pair[name], audio)
 					}
-					// 鏂囦欢鍚�
-					fileName := filepath.Base(event.Name)
-					//鏍¢獙鏂囦欢鍛藉悕
-					arr := strings.Split(fileName, "_")
-					if len(arr) != 6 {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏂囦欢鍚嶇О閿欒"))
+					if filepath.Ext(event.Name) == ".txt" {
+						isOk = audoF(pair[name], filepath.Base(pair[name]), audio) && txtF(event.Name, audio)
+					}
+					if !isOk {
+						delete(pair, name)
 						continue
 					}
-					timeStr := arr[4] + strings.Split(arr[5], ".")[0]
-					t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
-					if err != nil {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏃堕棿鏍煎紡涓嶅"))
-					}
-
-					//鏌ラ噸
-					_, err = models.NewAudioSearch().SetName(fileName).First()
-					if err != gorm.ErrRecordNotFound {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "閲嶅涓婁紶"))
-						continue
-					}
-
-					//灏嗘枃浠剁Щ鍔ㄥ埌uploads鏂囦欢澶逛笅
-					//鍒ゆ柇storePath涓湯灏炬槸鍚﹀甫
-					var src string
-					if strings.HasSuffix(conf.LocalConf.StorePath, "/") {
-						src = conf.LocalConf.StorePath + fileName
-					} else {
-						src = conf.LocalConf.StorePath + "/" + fileName
-					}
-					err = os.Rename(event.Name, src)
-					if err != nil {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "绉诲姩鏂囦欢澶辫触"))
-						continue
-					}
-					// 璇诲彇鏂囦欢澶у皬
-					fileInfo, err := os.Stat(src)
-					if err != nil {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鑾峰彇鏂囦欢澶у皬澶辫触"))
-						continue
-					}
-					size := fileInfo.Size()
-					fmt.Println("fileName:", fileName, "size:", size, "src1", src)
-
-					audio := &models.Audio{
-						Name:             fileName,
-						Size:             size,
-						FilePath:         src,
-						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 {
-						logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏁版嵁搴揷reate澶辫触"))
-						continue
-					}
-
-					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 len(audio.Name) > 0 {
+						if err = models.NewAudioSearch().Create(audio); err != nil {
+							logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏁版嵁搴揷reate澶辫触"))
+							continue
 						}
+						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
+							}
 
-					}()
+						}()
+					}
 				}
 			}
 		case err, ok := <-watcher.Errors:

--
Gitblit v1.8.0