|
@@ -175,10 +175,12 @@ func PreLoad(cxt context.Context) {
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
|
}
|
|
}
|
|
|
|
|
+FOR:
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case <-cxt.Done():
|
|
case <-cxt.Done():
|
|
|
fmt.Println("preload stop")
|
|
fmt.Println("preload stop")
|
|
|
|
|
+ break FOR // 退出循环
|
|
|
case event, ok := <-watcher.Events:
|
|
case event, ok := <-watcher.Events:
|
|
|
if !ok {
|
|
if !ok {
|
|
|
continue
|
|
continue
|
|
@@ -186,11 +188,14 @@ func PreLoad(cxt context.Context) {
|
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
|
// 判断文件类型是否为.mp3或.wav
|
|
// 判断文件类型是否为.mp3或.wav
|
|
|
if filepath.Ext(event.Name) == ".mp3" || filepath.Ext(event.Name) == ".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)
|
|
fileName := filepath.Base(event.Name)
|
|
|
- // 文件大小
|
|
|
|
|
- bs, _ := os.ReadFile(event.Name)
|
|
|
|
|
- size := len(bs)
|
|
|
|
|
//校验文件命名
|
|
//校验文件命名
|
|
|
arr := strings.Split(fileName, "_")
|
|
arr := strings.Split(fileName, "_")
|
|
|
if len(arr) != 6 {
|
|
if len(arr) != 6 {
|
|
@@ -211,16 +216,30 @@ func PreLoad(cxt context.Context) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//将文件移动到uploads文件夹下
|
|
//将文件移动到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)
|
|
err = os.Rename(event.Name, src)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
logx.Errorf(fmt.Sprintf("%s:%s", fileName, "移动文件失败"))
|
|
logx.Errorf(fmt.Sprintf("%s:%s", fileName, "移动文件失败"))
|
|
|
continue
|
|
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{
|
|
audio := &models.Audio{
|
|
|
Name: fileName,
|
|
Name: fileName,
|
|
|
- Size: int64(size),
|
|
|
|
|
|
|
+ Size: size,
|
|
|
FilePath: src,
|
|
FilePath: src,
|
|
|
AudioStatus: constvar.AudioStatusUploadOk,
|
|
AudioStatus: constvar.AudioStatusUploadOk,
|
|
|
LocomotiveNumber: arr[0],
|
|
LocomotiveNumber: arr[0],
|