From 777978a2a2ac2232050ba44521f107b85646081b Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期五, 14 六月 2024 15:40:45 +0800 Subject: [PATCH] watch preloads file to autoload --- go.sum | 2 service/process.go | 115 ++++++++++++++++++++++++++++++++++++++ conf/config.go | 3 go.mod | 3 main.go | 6 ++ conf/config.yaml | 1 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/conf/config.go b/conf/config.go index 76a13b6..f6d5ed9 100644 --- a/conf/config.go +++ b/conf/config.go @@ -39,7 +39,8 @@ } localConf struct { - StorePath string // 鏈湴鏂囦欢瀛樺偍璺緞 + StorePath string // 鏈湴鏂囦欢瀛樺偍璺緞 + PreLoadPath string // 鏈湴鏂囦欢棰勫姞杞借矾寰� } Analysis struct { Url string // 鏈湴鏂囦欢瀛樺偍璺緞 diff --git a/conf/config.yaml b/conf/config.yaml index d2f5493..19ec80e 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -18,5 +18,6 @@ RotateDays: 5 # 鏃ュ織鏂囦欢鐨勬渶澶т繚鐣欏ぉ鏁� local: storePath: uploads + preLoadPath: preloads analysis: url: http://192.168.20.116:5000/recognition diff --git a/go.mod b/go.mod index 24c0b5f..b2a32ee 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( basic.com/aps/nsqclient.git v0.0.0-20230517072415-37491f4a5d25 github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/fsnotify/fsnotify v1.7.0 github.com/gin-gonic/gin v1.10.0 github.com/nsqio/go-nsq v1.1.0 github.com/spf13/viper v1.18.2 @@ -14,6 +15,7 @@ go.uber.org/zap v1.27.0 golang.org/x/crypto v0.23.0 google.golang.org/grpc v1.63.2 + gopkg.in/fsnotify.v1 v1.4.7 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gorm.io/driver/mysql v1.5.6 gorm.io/gorm v1.25.10 @@ -28,7 +30,6 @@ github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect diff --git a/go.sum b/go.sum index 378b07d..f978762 100644 --- a/go.sum +++ b/go.sum @@ -225,6 +225,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= diff --git a/main.go b/main.go index a9819f7..cfe0598 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ "speechAnalysis/models" "speechAnalysis/pkg/logx" "speechAnalysis/router" + "speechAnalysis/service" "syscall" "time" ) @@ -31,6 +32,11 @@ return } + //鐩戞帶棰勫姞杞介煶棰戞枃浠� + cxt, cancel := context.WithCancel(context.Background()) + defer cancel() + go service.PreLoad(cxt) + logx.Infof("server start serve...") server := &http.Server{ Addr: ":" + conf.WebConf.Port, diff --git a/service/process.go b/service/process.go index 29eb5f3..1c08e55 100644 --- a/service/process.go +++ b/service/process.go @@ -2,18 +2,24 @@ import ( "bytes" + "context" "encoding/json" "errors" + "fmt" + "github.com/fsnotify/fsnotify" "gorm.io/gorm" "io" + "log" "mime/multipart" "net/http" "os" + "path/filepath" "speechAnalysis/conf" "speechAnalysis/constvar" "speechAnalysis/models" "speechAnalysis/pkg/logx" "strings" + "time" ) // Response 缁撴瀯浣撶敤浜庡瓨鍌ㄥ搷搴斾綋鐨勫唴瀹� @@ -153,3 +159,112 @@ } return words } + +func PreLoad(cxt context.Context) { + mkdirErr := os.MkdirAll(conf.LocalConf.PreLoadPath, os.ModePerm) + if mkdirErr != nil { + logx.Errorf("function os.MkdirAll() err:%v", mkdirErr) + } + //鏂囦欢澶逛笅鏂板闊抽鏂囦欢鏃惰Е鍙� + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + err = watcher.Add(conf.LocalConf.PreLoadPath) + if err != nil { + log.Fatal(err) + } + for { + select { + case <-cxt.Done(): + fmt.Println("preload stop") + case event, ok := <-watcher.Events: + if !ok { + continue + } + if event.Op&fsnotify.Create == fsnotify.Create { + // 鍒ゆ柇鏂囦欢绫诲瀷鏄惁涓�.mp3鎴�.wav + if filepath.Ext(event.Name) == ".mp3" || filepath.Ext(event.Name) == ".wav" { + // 鏂囦欢鍚� + fileName := filepath.Base(event.Name) + // 鏂囦欢澶у皬 + bs, _ := os.ReadFile(event.Name) + size := len(bs) + //鏍¢獙鏂囦欢鍛藉悕 + arr := strings.Split(fileName, "_") + if len(arr) != 6 { + logx.Errorf(fmt.Sprintf("%s:%s", fileName, "鏂囦欢鍚嶇О閿欒")) + 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鏂囦欢澶逛笅 + src := conf.LocalConf.StorePath + "/" + fileName + err = os.Rename(event.Name, src) + if err != nil { + logx.Errorf(fmt.Sprintf("%s:%s", fileName, "绉诲姩鏂囦欢澶辫触")) + continue + } + + audio := &models.Audio{ + Name: fileName, + Size: int64(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 + } + + }() + } + } + case err, ok := <-watcher.Errors: + if !ok { + logx.Errorf(err.Error()) + } + } + } +} -- Gitblit v1.8.0