| | |
| | | |
| | | 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 结构体用于存储响应体的内容 |
| | |
| | | } |
| | | 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, "数据库create失败")) |
| | | 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()) |
| | | } |
| | | } |
| | | } |
| | | } |