Added /addQue to add the last received song to the songQue. Changed /rand and /rand/low to not add song to the que. Changed /next to not call /rand when the end of the que is reached, instead the last song in the que will be resent.
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
var currentSong = -1
|
||||
var games []GameData
|
||||
var songQue []SongData
|
||||
var lastFetched SongData
|
||||
|
||||
func getSoundCheckSong() string {
|
||||
reset()
|
||||
@@ -33,6 +34,12 @@ func reset() {
|
||||
games = findAllGames()
|
||||
}
|
||||
|
||||
func addLatestToQue() {
|
||||
if lastFetched.path != "" {
|
||||
songQue = append(songQue, lastFetched)
|
||||
}
|
||||
}
|
||||
|
||||
func getRandomSong() string {
|
||||
if games == nil || len(games) == 0 {
|
||||
games = findAllGames()
|
||||
@@ -45,7 +52,7 @@ func getRandomSong() string {
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
|
||||
currentSong = len(songQue)
|
||||
songQue = append(songQue, song)
|
||||
lastFetched = song
|
||||
return song.path
|
||||
}
|
||||
|
||||
@@ -74,12 +81,15 @@ func getRandomSongLowChance() string {
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
|
||||
currentSong = len(songQue)
|
||||
songQue = append(songQue, song)
|
||||
lastFetched = song
|
||||
return song.path
|
||||
|
||||
}
|
||||
|
||||
func getSongInfo() SongInfo {
|
||||
if songQue == nil {
|
||||
return SongInfo{}
|
||||
}
|
||||
var currentSongData = songQue[currentSong]
|
||||
|
||||
currentGameData := getCurrentGame(currentSongData)
|
||||
@@ -144,8 +154,12 @@ func setPlayed(songNumber int) {
|
||||
}
|
||||
|
||||
func getNextSong() string {
|
||||
if songQue == nil {
|
||||
return ""
|
||||
}
|
||||
if currentSong == len(songQue)-1 || currentSong == -1 {
|
||||
return getRandomSong()
|
||||
var songData = songQue[currentSong]
|
||||
return songData.path
|
||||
} else {
|
||||
currentSong = currentSong + 1
|
||||
var songData = songQue[currentSong]
|
||||
@@ -154,6 +168,9 @@ func getNextSong() string {
|
||||
}
|
||||
|
||||
func getPreviousSong() string {
|
||||
if songQue == nil {
|
||||
return ""
|
||||
}
|
||||
if currentSong == -1 || currentSong == 0 {
|
||||
var songData = songQue[0]
|
||||
return songData.path
|
||||
@@ -177,12 +194,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
s := getSong(song)
|
||||
sendFile(w, r, s)
|
||||
//http.ServeFile(w, r, s)
|
||||
}
|
||||
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
||||
song := getSoundCheckSong()
|
||||
sendFile(w, r, song)
|
||||
//http.ServeFile(w, r, getSoundCheckSong())
|
||||
|
||||
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
||||
reset()
|
||||
@@ -190,16 +205,10 @@ 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)
|
||||
|
||||
/*file, _ := os.Open(song)
|
||||
seeker := io.ReadSeeker(file)
|
||||
http.ServeContent(w, r, song, time.Time{}, seeker)*/
|
||||
|
||||
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
|
||||
chance := getRandomSongLowChance()
|
||||
//http.ServeFile(w, r, chance)
|
||||
sendFile(w, r, chance)
|
||||
|
||||
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
||||
@@ -212,12 +221,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
} else if r.URL.Path == "/music/next" {
|
||||
song := getNextSong()
|
||||
//http.ServeFile(w, r, song)
|
||||
sendFile(w, r, song)
|
||||
|
||||
} else if r.URL.Path == "/music/previous" {
|
||||
song := getPreviousSong()
|
||||
//http.ServeFile(w, r, song)
|
||||
sendFile(w, r, song)
|
||||
|
||||
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
||||
@@ -233,6 +240,9 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
setPlayed(p.song)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else if r.URL.Path == "/music/addQue" && r.Method == http.MethodGet {
|
||||
addLatestToQue()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,13 +250,13 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string
|
||||
fmt.Println("Client requests: " + Filename)
|
||||
|
||||
//Check if file exists and open
|
||||
Openfile, err := os.Open(Filename)
|
||||
defer Openfile.Close() //Close after function return
|
||||
openFile, err := os.Open(Filename)
|
||||
if err != nil {
|
||||
//File not found, send 404
|
||||
http.Error(writer, "File not found.", 404)
|
||||
return
|
||||
}
|
||||
defer openFile.Close() //Close after function return
|
||||
|
||||
//File is found, create and send the correct headers
|
||||
|
||||
@@ -254,12 +264,12 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string
|
||||
//Create a buffer to store the header of the file in
|
||||
FileHeader := make([]byte, 512)
|
||||
//Copy the headers into the FileHeader buffer
|
||||
_, _ = Openfile.Read(FileHeader)
|
||||
_, _ = openFile.Read(FileHeader)
|
||||
//Get content type of file
|
||||
//FileContentType := http.DetectContentType(FileHeader)
|
||||
|
||||
//Get the file size
|
||||
FileStat, _ := Openfile.Stat() //Get info from file
|
||||
FileStat, _ := openFile.Stat() //Get info from file
|
||||
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
|
||||
|
||||
//Send the headers
|
||||
@@ -269,8 +279,8 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string
|
||||
|
||||
//Send the file
|
||||
//We read 512 bytes from the file already, so we reset the offset back to 0
|
||||
_, _ = Openfile.Seek(0, 0)
|
||||
_, _ = io.Copy(writer, Openfile) //'Copy' the file to the client
|
||||
_, _ = openFile.Seek(0, 0)
|
||||
_, _ = io.Copy(writer, openFile) //'Copy' the file to the client
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user