yinbangzhong
2024-06-17 7342185782fec53480caeba8e047d01b35927bec
service/process.go
@@ -175,10 +175,12 @@
   if err != nil {
      log.Fatal(err)
   }
FOR:
   for {
      select {
      case <-cxt.Done():
         fmt.Println("preload stop")
         break FOR // 退出循环
      case event, ok := <-watcher.Events:
         if !ok {
            continue
@@ -186,11 +188,14 @@
         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)
               // 文件大小
               bs, _ := os.ReadFile(event.Name)
               size := len(bs)
               //校验文件命名
               arr := strings.Split(fileName, "_")
               if len(arr) != 6 {
@@ -211,16 +216,30 @@
               }
               //将文件移动到uploads文件夹下
               src := conf.LocalConf.StorePath + "/" + fileName
               //判断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:             int64(size),
                  Size:             size,
                  FilePath:         src,
                  AudioStatus:      constvar.AudioStatusUploadOk,
                  LocomotiveNumber: arr[0],