Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.
This commit is contained in:
@@ -24,7 +24,7 @@ func (m *Music) GetSong(ctx *gin.Context) {
|
||||
helpers.SendSong(ctx, s)
|
||||
}
|
||||
|
||||
func (m *Music) GetMusicFirst(ctx *gin.Context) {
|
||||
func (m *Music) GetSoundCheckSong(ctx *gin.Context) {
|
||||
song := server.GetSoundCheckSong()
|
||||
helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func SetupRestServer(swagger embed.FS) {
|
||||
musicGroup := router.Group("/music")
|
||||
{
|
||||
musicGroup.GET("", music.GetSong)
|
||||
musicGroup.GET("first", music.GetMusicFirst)
|
||||
musicGroup.GET("soundTest", music.GetSoundCheckSong)
|
||||
musicGroup.GET("reset", music.ResetMusic)
|
||||
musicGroup.GET("rand", music.GetRandomSong)
|
||||
musicGroup.GET("rand/low", music.GetRandomSongLowChance)
|
||||
@@ -86,7 +86,7 @@ func SetupRestServer(swagger embed.FS) {
|
||||
router.GET("/version", index.GetVersion)
|
||||
router.GET("/test", index.GetDBTest)
|
||||
router.StaticFS("/swagger", helpers.EmbedFolder(swagger, "swagger", false))
|
||||
router.Use(static.Serve("/", static.LocalFile("frontend/dist", true)))
|
||||
router.Use(static.Serve("/", static.LocalFile("/frontend", true)))
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"io/fs"
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/models"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -60,6 +62,20 @@ func EmbedFolder(fsEmbed embed.FS, targetPath string, index bool) static.ServeFi
|
||||
}
|
||||
}
|
||||
|
||||
func CheckIfSongExists(song models.SongData) bool {
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil {
|
||||
//File not found
|
||||
db.RemoveBrokenSong(song)
|
||||
return false
|
||||
}
|
||||
defer func(openFile *os.File) {
|
||||
_ = openFile.Close()
|
||||
}(openFile) //Close after function return
|
||||
return true
|
||||
}
|
||||
|
||||
func SendSong(ctx *gin.Context, Filename string) {
|
||||
fmt.Println("Client requests: " + Filename)
|
||||
|
||||
|
||||
@@ -10,9 +10,13 @@ func TestDB() {
|
||||
}
|
||||
|
||||
func GetVersionHistory() models.VersionData {
|
||||
data := models.VersionData{Version: "3.0",
|
||||
Changelog: "Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application.",
|
||||
data := models.VersionData{Version: "3.1",
|
||||
Changelog: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.",
|
||||
History: []models.VersionData{
|
||||
{
|
||||
Version: "3.0",
|
||||
Changelog: "Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application.",
|
||||
},
|
||||
{
|
||||
Version: "2.3.0",
|
||||
Changelog: "Images should not be included in the database, removes songs where the path doesn't work.",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/helpers"
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -17,8 +18,6 @@ var songQue []models.SongData
|
||||
var lastFetched models.SongData
|
||||
|
||||
func GetSoundCheckSong() string {
|
||||
Reset()
|
||||
|
||||
files, err := ioutil.ReadDir("songs")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -51,9 +50,13 @@ func GetRandomSong() string {
|
||||
if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
songExists := false
|
||||
var song models.SongData
|
||||
|
||||
song := getSongFromList(games)
|
||||
|
||||
for !songExists {
|
||||
song = getSongFromList(games)
|
||||
songExists = helpers.CheckIfSongExists(song)
|
||||
}
|
||||
lastFetched = song
|
||||
return song.Path
|
||||
}
|
||||
@@ -75,8 +78,12 @@ func GetRandomSongLowChance() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
song := getSongFromList(listOfGames)
|
||||
songExists := false
|
||||
var song models.SongData
|
||||
for !songExists {
|
||||
song = getSongFromList(listOfGames)
|
||||
songExists = helpers.CheckIfSongExists(song)
|
||||
}
|
||||
|
||||
lastFetched = song
|
||||
return song.Path
|
||||
|
||||
@@ -109,28 +109,20 @@ func addNewGame(name string, path string) {
|
||||
}
|
||||
|
||||
func checkSongs(gameDir string, gameId int) {
|
||||
songs := make([]models.SongData, 0)
|
||||
findSongsFromGame := db.FindSongsFromGame(gameId)
|
||||
|
||||
files, err := ioutil.ReadDir(gameDir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
db.ClearSongs(gameId)
|
||||
for _, entry := range files {
|
||||
path := gameDir + entry.Name()
|
||||
fileName := entry.Name()
|
||||
if isSong(entry) {
|
||||
songs = append(songs, models.SongData{GameId: gameId, SongName: fileName, Path: path})
|
||||
db.AddSong(models.SongData{GameId: gameId, SongName: fileName, Path: path})
|
||||
} else if isCoverImage(entry) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
}
|
||||
if len(songs) != len(findSongsFromGame) {
|
||||
db.ClearSongs(gameId)
|
||||
for _, song := range songs {
|
||||
db.AddSong(song)
|
||||
}
|
||||
}
|
||||
//TODO: Add number of songs here
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user