Added a check to see if song exists before returning it, if not a new song will be picked up
This commit is contained in:
@@ -47,14 +47,37 @@ func getRandomSong() string {
|
||||
games = findAllGames()
|
||||
}
|
||||
|
||||
game := getRandomGame(games)
|
||||
songs := findSongsFromGame(game.id)
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
song := getSongFromList(games)
|
||||
|
||||
lastFetched = song
|
||||
return song.path
|
||||
}
|
||||
|
||||
func getSongFromList(games []GameData) SongData {
|
||||
songFound := false
|
||||
var song SongData
|
||||
for !songFound {
|
||||
game := getRandomGame(games)
|
||||
songs := findSongsFromGame(game.id)
|
||||
song = songs[rand.Intn(len(songs))]
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.path)
|
||||
if err != nil {
|
||||
//File not found
|
||||
log.Fatal("Song not found, maybe delete song and/or game" + song.songName + " songPath: " + song.path)
|
||||
} else {
|
||||
songFound = true
|
||||
}
|
||||
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
return song
|
||||
}
|
||||
|
||||
func getRandomSongLowChance() string {
|
||||
gameList := findAllGames()
|
||||
|
||||
@@ -73,9 +96,7 @@ func getRandomSongLowChance() string {
|
||||
}
|
||||
}
|
||||
|
||||
game := getRandomGame(listOfGames)
|
||||
songs := findSongsFromGame(game.id)
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
song := getSongFromList(listOfGames)
|
||||
|
||||
lastFetched = song
|
||||
return song.path
|
||||
@@ -189,11 +210,11 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
} else {
|
||||
s := getSong(song)
|
||||
sendFile(w, r, s)
|
||||
sendFile(w, s)
|
||||
}
|
||||
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
||||
song := getSoundCheckSong()
|
||||
sendFile(w, r, song)
|
||||
sendFile(w, song)
|
||||
|
||||
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
||||
reset()
|
||||
@@ -201,11 +222,11 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
|
||||
song := getRandomSong()
|
||||
sendFile(w, r, song)
|
||||
sendFile(w, song)
|
||||
|
||||
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
|
||||
chance := getRandomSongLowChance()
|
||||
sendFile(w, r, chance)
|
||||
sendFile(w, chance)
|
||||
|
||||
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
@@ -217,11 +238,11 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
} else if r.URL.Path == "/music/next" {
|
||||
song := getNextSong()
|
||||
sendFile(w, r, song)
|
||||
sendFile(w, song)
|
||||
|
||||
} else if r.URL.Path == "/music/previous" {
|
||||
song := getPreviousSong()
|
||||
sendFile(w, r, song)
|
||||
sendFile(w, song)
|
||||
|
||||
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
@@ -242,7 +263,7 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func sendFile(writer http.ResponseWriter, request *http.Request, Filename string) {
|
||||
func sendFile(writer http.ResponseWriter, Filename string) {
|
||||
fmt.Println("Client requests: " + Filename)
|
||||
|
||||
//Check if file exists and open
|
||||
@@ -252,7 +273,9 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string
|
||||
http.Error(writer, "File not found.", 404)
|
||||
return
|
||||
}
|
||||
defer openFile.Close() //Close after function return
|
||||
defer func(openFile *os.File) {
|
||||
_ = openFile.Close()
|
||||
}(openFile) //Close after function return
|
||||
|
||||
//File is found, create and send the correct headers
|
||||
|
||||
|
||||
Reference in New Issue
Block a user