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()
|
games = findAllGames()
|
||||||
}
|
}
|
||||||
|
|
||||||
game := getRandomGame(games)
|
song := getSongFromList(games)
|
||||||
songs := findSongsFromGame(game.id)
|
|
||||||
song := songs[rand.Intn(len(songs))]
|
|
||||||
|
|
||||||
lastFetched = song
|
lastFetched = song
|
||||||
return song.path
|
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 {
|
func getRandomSongLowChance() string {
|
||||||
gameList := findAllGames()
|
gameList := findAllGames()
|
||||||
|
|
||||||
@@ -73,9 +96,7 @@ func getRandomSongLowChance() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
game := getRandomGame(listOfGames)
|
song := getSongFromList(listOfGames)
|
||||||
songs := findSongsFromGame(game.id)
|
|
||||||
song := songs[rand.Intn(len(songs))]
|
|
||||||
|
|
||||||
lastFetched = song
|
lastFetched = song
|
||||||
return song.path
|
return song.path
|
||||||
@@ -189,11 +210,11 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
s := getSong(song)
|
s := getSong(song)
|
||||||
sendFile(w, r, s)
|
sendFile(w, s)
|
||||||
}
|
}
|
||||||
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
||||||
song := getSoundCheckSong()
|
song := getSoundCheckSong()
|
||||||
sendFile(w, r, song)
|
sendFile(w, song)
|
||||||
|
|
||||||
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
||||||
reset()
|
reset()
|
||||||
@@ -201,11 +222,11 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
|
||||||
song := getRandomSong()
|
song := getRandomSong()
|
||||||
sendFile(w, r, song)
|
sendFile(w, song)
|
||||||
|
|
||||||
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
|
||||||
chance := getRandomSongLowChance()
|
chance := getRandomSongLowChance()
|
||||||
sendFile(w, r, chance)
|
sendFile(w, chance)
|
||||||
|
|
||||||
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
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" {
|
} else if r.URL.Path == "/music/next" {
|
||||||
song := getNextSong()
|
song := getNextSong()
|
||||||
sendFile(w, r, song)
|
sendFile(w, song)
|
||||||
|
|
||||||
} else if r.URL.Path == "/music/previous" {
|
} else if r.URL.Path == "/music/previous" {
|
||||||
song := getPreviousSong()
|
song := getPreviousSong()
|
||||||
sendFile(w, r, song)
|
sendFile(w, song)
|
||||||
|
|
||||||
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
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)
|
fmt.Println("Client requests: " + Filename)
|
||||||
|
|
||||||
//Check if file exists and open
|
//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)
|
http.Error(writer, "File not found.", 404)
|
||||||
return
|
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
|
//File is found, create and send the correct headers
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user