yinbangzhong
2024-06-20 b4d6928d7387d2bbadbff4ef754605ac7c4916be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package service
 
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 结构体用于存储响应体的内容
type Response struct {
    Code   int     `json:"code"`
    Msg    string  `json:"msg"`
    Result string  `json:"result"`
    Score  float64 `json:"score"`
}
 
func AnalysisAudio(filename string, targetURL string) (resp Response, err error) {
    file, err := os.Open(filename)
    if err != nil {
        return
    }
    defer file.Close()
 
    // 创建一个缓冲区来存储表单数据
    var requestBody bytes.Buffer
    writer := multipart.NewWriter(&requestBody)
 
    // 创建一个表单字段,用于存储文件
    fileWriter, err := writer.CreateFormFile("audio", filename)
    if err != nil {
        return
    }
 
    // 将文件内容复制到表单字段中
    _, err = io.Copy(fileWriter, file)
    if err != nil {
        return
    }
 
    // 关闭表单写入器,以便写入末尾的边界
    writer.Close()
 
    // 创建POST请求,指定URL和请求体
    request, err := http.NewRequest("POST", targetURL, &requestBody)
    if err != nil {
        return
    }
 
    // 设置请求头,指定Content-Type为multipart/form-data
    request.Header.Set("Content-Type", writer.FormDataContentType())
 
    // 发送请求
    client := &http.Client{}
    response, err := client.Do(request)
    if err != nil {
        return
    }
    defer response.Body.Close()
 
    // 读取响应
    body := &bytes.Buffer{}
    _, err = io.Copy(body, response.Body)
    if err != nil {
        return
    }
 
    err = json.NewDecoder(body).Decode(&resp)
    if err != nil {
        return
    }
 
    return
}
 
func Process(audioId uint) (err error) {
    audio, err := models.NewAudioSearch().SetID(audioId).First()
 
    if err != nil {
        return errors.New("查找音频失败")
    }
 
    if audio.AudioStatus != constvar.AudioStatusUploadOk && audio.AudioStatus != constvar.AudioStatusFailed {
        return errors.New("状态不正确")
    }
 
    err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusProcessing})
    if err != nil {
        return errors.New("DB错误")
    }
 
    go func() {
        var resp Response
        sz := audio.Size / 1024 / 1024
        if sz > 2 {
            resp, err = AnalysisAudio(audio.FilePath, conf.AanlysisConf.LongUrl)
        } else {
            resp, err = AnalysisAudio(audio.FilePath, conf.AanlysisConf.Url)
        }
        if err != nil {
            logx.Errorf("err when AnalysisAudio:%v", err)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
        if resp.Code != 0 {
            logx.Errorf("AnalysisAudio error return:%v", resp)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
        logx.Infof("AnalysisAudio result: %v", resp)
        words := GetWordFromText(resp.Result, audio)
 
        err = models.WithTransaction(func(db *gorm.DB) error {
            err = models.NewAudioSearch().SetOrm(db).SetID(audioId).UpdateByMap(map[string]interface{}{
                "audio_status": constvar.AudioStatusFinish,
                "score":        resp.Score,
                "tags":         strings.Join(words, ","),
            })
            if err != nil {
                return err
            }
            err = models.NewAudioTextSearch().SetOrm(db).Save(&models.AudioText{
                AudioID:   audio.ID,
                AudioText: resp.Result,
            })
            return err
        })
        if err != nil {
            logx.Infof("AnalysisAudio success but update record failed: %v", err)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
    }()
 
    return nil
}
 
func GetWordFromText(text string, audio *models.Audio) (words []string) {
    if audio == nil {
        return nil
    }
    wordRecords, err := models.NewWordSearch().SetLocomotiveNumber(audio.LocomotiveNumber).FindNotTotal()
    if err != nil || len(wordRecords) == 0 {
        return nil
    }
    for _, v := range wordRecords {
        if strings.Contains(text, v.Content) {
            words = append(words, v.Content)
        }
    }
    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)
    }
    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 {
        log.Fatal(err)
    }
    defer watcher.Close()
    err = watcher.Add(conf.LocalConf.PreLoadPath)
    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文件内容到bts
        bts, err := os.ReadFile(filePath)
        if err != nil {
            logx.Errorf(fmt.Sprintf("%s:%s", filePath, "读取txt文件失败"))
            return false
        }
        //解析 交路号: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 {
        case <-cxt.Done():
            fmt.Println("preload stop")
            break FOR // 退出循环
        case event, ok := <-watcher.Events:
            if !ok {
                continue
            }
            if event.Op&fsnotify.Create == fsnotify.Create {
                // 文件名
                fileName := filepath.Base(event.Name)
                //获取不带扩展名的文件名
                name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
                //判断文件在pair中
                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)
                    }
                    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
                    }
                    if len(audio.Name) > 0 {
                        if err = models.NewAudioSearch().Create(audio); err != nil {
                            logx.Errorf(fmt.Sprintf("%s:%s", fileName, "数据库create失败"))
                            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:
            if !ok {
                logx.Errorf(err.Error())
            }
        }
    }
}