/
route.go
541 lines (478 loc) · 11.3 KB
/
route.go
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/satori/go.uuid"
"gorm.io/gorm"
)
func GetCharts(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read charts
var charts []Chart
if err := db.Preload("Song").Find(&charts).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read charts: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &GetChartsReply{
Success: true,
Charts: charts,
}
c.JSON(http.StatusOK, rep)
}
}
func GetSongs(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read songs
var songs []Song
if err := db.Find(&songs).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read songs: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &GetSongsReply{
Success: true,
Songs: songs,
}
c.JSON(http.StatusOK, rep)
}
}
func GetBPM(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// get id
id := c.Param("id")
// read song
var song Song
if err := db.First(&song, id).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &GetBPMReply{
Success: true,
BPM: song.BPM,
Offset: song.Offset,
}
c.JSON(http.StatusOK, rep)
}
}
func GetSongFile(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// get id
id := c.Param("id")
// get song from db
var song Song
if err := db.First(&song, id).Error; err != nil {
c.String(
http.StatusInternalServerError,
fmt.Sprintf("failed to find song from db: %v", err),
)
}
// read file
file, err := os.ReadFile(song.Location)
if err != nil {
c.String(
http.StatusInternalServerError,
fmt.Sprintf("failed to read file: %v", err),
)
return
}
// reply
if _, err := c.Writer.Write(file); err != nil {
c.String(
http.StatusInternalServerError,
fmt.Sprintf("failed to write file: %v", err),
)
return
}
c.Writer.Flush()
}
}
func CalBPM(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// get id
id := c.Param("id")
// read song
var song Song
if err := db.First(&song, id).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// try read from server
reply, err := getBPM(&song)
if err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to calculate BPM: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
song.BPM = math.Round(reply.BPM)
song.Offset = reply.Offset
if err := db.Save(&song).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to save updated song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &GetBPMReply{
Success: true,
BPM: song.BPM,
Offset: song.Offset,
}
c.JSON(http.StatusOK, rep)
}
}
func SetBPM(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// Get the song ID from the request parameters
id := c.Param("id")
// Read the song from the database
var song Song
if err := db.First(&song, id).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Parse the BPM and offset values from the request body
var requestBody struct {
BPM float64 `json:"bpm"`
Offset float64 `json:"offset"`
}
if err := c.ShouldBindJSON(&requestBody); err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to parse request body: %v", err),
}
c.JSON(http.StatusBadRequest, rep)
return
}
// Update the song's BPM and offset
song.BPM = requestBody.BPM
song.Offset = requestBody.Offset
// Save the updated song to the database
if err := db.Save(&song).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to save updated song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Reply with a success message
rep := &GeneralReply{
Success: true,
Msg: "BPM and offset successfully updated.",
}
c.JSON(http.StatusOK, rep)
}
}
func SyncBPM(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// Get the song ID from the request parameters
id := c.Param("id")
// Read the song from the database
var song Song
if err := db.Preload("Charts").First(&song, id).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Check if song has charts
if len(song.Charts) == 0 {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("song has no charts"),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Read BPM from song's chart
chart := song.Charts[0]
var level Level
if err := json.Unmarshal([]byte(chart.Content), &level); err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to get chart of BPM: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Update the song's BPM and offset
song.BPM = level.BPM
song.Offset = float64(level.Offset) / 44.1
// Save the updated song to the database
if err := db.Save(&song).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to save updated song: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// Reply with a success message
rep := &GetBPMReply{
Success: true,
BPM: song.BPM,
Offset: song.Offset,
}
c.JSON(http.StatusOK, rep)
}
}
func PostCharts(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read chart
var req PostChartsRequest
if err := c.BindJSON(&req); err != nil {
return
}
// save chart
chart := &Chart{
Content: req.Content,
SongID: req.SongID,
}
if err := db.Create(chart).Error; err != nil {
rep := &PostChartsReply{
Success: false,
Msg: fmt.Sprintf("failed to save chart to db: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &PostChartsReply{
Success: true,
ChartID: chart.ID,
}
c.JSON(http.StatusOK, rep)
}
}
func PostChart(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read chart
var req PostChartRequest
if err := c.BindJSON(&req); err != nil {
return
}
// get param
id := c.Param("id")
// load chart from db
var chart Chart
if err := db.First(&chart, id).Error; err != nil {
rep := &PostChartReply{
Success: false,
Msg: fmt.Sprintf("failed to read chart from db: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// save chart
chart.Content = req.Content
if err := db.Save(chart).Error; err != nil {
rep := &PostChartReply{
Success: false,
Msg: fmt.Sprintf("failed to save chart to db: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &PostChartReply{
Success: true,
}
c.JSON(http.StatusOK, rep)
}
}
func PostSongs(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read song
file, err := c.FormFile("song")
if err != nil {
rep := &PostSongsReply{
Success: false,
Msg: fmt.Sprintf("failed to read song: %v", err),
}
c.JSON(http.StatusBadRequest, rep)
return
}
// save song file to disk
base := filepath.Base(file.Filename)
ext := filepath.Ext(file.Filename)
dir := fmt.Sprintf("/songs/%s%s", uuid.NewV4().String(), ext)
if err := c.SaveUploadedFile(file, dir); err != nil {
rep := &PostSongsReply{
Success: false,
Msg: fmt.Sprintf("failed to save song to %s: %v", dir, err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// save song info to db
song := &Song{
Name: base,
BPM: 0,
Offset: 0,
Location: dir,
}
if err := db.Create(song).Error; err != nil {
rep := &PostSongsReply{
Success: false,
Msg: fmt.Sprintf("failed to save song to database: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// reply
rep := &PostSongsReply{
Success: true,
SongID: song.ID,
}
c.JSON(http.StatusOK, rep)
}
}
func DeleteChart(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// get id
id := c.Param("id")
// delete chart from db
if err := db.Delete(id).Error; err != nil {
rep := &DeleteChartReply{
Success: false,
Msg: fmt.Sprintf("failed to delete chart from db: %v", err),
}
c.JSON(http.StatusBadRequest, rep)
return
}
// reply
rep := &DeleteChartReply{
Success: true,
}
c.JSON(http.StatusOK, rep)
}
}
func DeleteSong(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// get id
id := c.Param("id")
// delete related charts
if err := db.Where("song_id = ?", id).Delete(&Chart{}).Error; err != nil {
rep := &DeleteSongReply{
Success: false,
Msg: fmt.Sprintf("failed to delete related charts from db: %v", err),
}
c.JSON(http.StatusBadRequest, rep)
return
}
// delete song from db
if err := db.Delete(&Song{}, id).Error; err != nil {
rep := &DeleteSongReply{
Success: false,
Msg: fmt.Sprintf("failed to delete song from db: %v", err),
}
c.JSON(http.StatusBadRequest, rep)
return
}
// reply
rep := &DeleteSongReply{
Success: true,
}
c.JSON(http.StatusOK, rep)
}
}
func TestBPM(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
// read song
var songs []Song
if err := db.Preload("Charts").Find(&songs).Error; err != nil {
rep := &GeneralReply{
Success: false,
Msg: fmt.Sprintf("failed to read songs: %v", err),
}
c.JSON(http.StatusInternalServerError, rep)
return
}
// start testing each songs
bpmCorrect := 0
testTotal := 0
offsetCorrect := 0
for _, song := range songs {
if len(song.Charts) == 0 {
continue
}
chart := song.Charts[0]
var level Level
if err := json.Unmarshal([]byte(chart.Content), &level); err != nil {
continue
}
bpm := level.BPM
spb := 60.0 / bpm
offset := float64(level.Offset) / float64(level.SampleRate)
if spb > 0 {
for offset > spb {
offset -= spb
}
}
offset *= 1000
reply, err := getBPM(&song)
if err != nil {
continue
}
bpmErr := eqErr(reply.BPM, bpm)
if bpmErr < 0.5 {
bpmCorrect++
}
testTotal++
offsetErr := relErr(reply.Offset, offset, spb*1000)
if offsetErr < 30 {
offsetCorrect++
}
fmt.Printf(
"%d %.2f %.1f | BPM %.2f - %.2f | Offset %.1f - %.1f\n",
song.ID,
bpmErr,
offsetErr,
bpm,
reply.BPM,
offset,
reply.Offset,
)
}
// reply
rep := &GeneralReply{
Success: true,
Msg: fmt.Sprintf("BPM: %d / %d correct; Offset: %d / %d correct", bpmCorrect, testTotal, offsetCorrect, testTotal),
}
c.JSON(http.StatusOK, rep)
}
}