Hopefully fixed the caching problem with random
This commit is contained in:
@@ -3,10 +3,12 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -163,7 +165,7 @@ func getPreviousSong() string {
|
||||
}
|
||||
|
||||
func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
(w).Header().Set("Access-Control-Allow-Origin", "*")
|
||||
setCorsAndNoCacheHeaders(&w, r)
|
||||
if r.URL.Path == "/music" && r.Method == http.MethodGet {
|
||||
song := r.URL.Query().Get("song")
|
||||
if song == "" {
|
||||
@@ -173,20 +175,32 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
http.ServeFile(w, r, getSong(song))
|
||||
s := getSong(song)
|
||||
sendFile(w, r, s)
|
||||
//http.ServeFile(w, r, s)
|
||||
}
|
||||
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
||||
http.ServeFile(w, r, getSoundCheckSong())
|
||||
song := getSoundCheckSong()
|
||||
sendFile(w, r, song)
|
||||
//http.ServeFile(w, r, getSoundCheckSong())
|
||||
|
||||
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
||||
reset()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
|
||||
http.ServeFile(w, r, getRandomSong())
|
||||
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 {
|
||||
http.ServeFile(w, r, getRandomSongLowChance())
|
||||
chance := getRandomSongLowChance()
|
||||
//http.ServeFile(w, r, chance)
|
||||
sendFile(w, r, chance)
|
||||
|
||||
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
@@ -197,10 +211,14 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_ = json.NewEncoder(w).Encode(getPlayedSongs())
|
||||
|
||||
} else if r.URL.Path == "/music/next" {
|
||||
http.ServeFile(w, r, getNextSong())
|
||||
song := getNextSong()
|
||||
//http.ServeFile(w, r, song)
|
||||
sendFile(w, r, song)
|
||||
|
||||
} else if r.URL.Path == "/music/previous" {
|
||||
http.ServeFile(w, r, getPreviousSong())
|
||||
song := getPreviousSong()
|
||||
//http.ServeFile(w, r, song)
|
||||
sendFile(w, r, song)
|
||||
|
||||
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
@@ -218,6 +236,44 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if err != nil {
|
||||
//File not found, send 404
|
||||
http.Error(writer, "File not found.", 404)
|
||||
return
|
||||
}
|
||||
|
||||
//File is found, create and send the correct headers
|
||||
|
||||
//Get the Content-Type of the file
|
||||
//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)
|
||||
//Get content type of file
|
||||
//FileContentType := http.DetectContentType(FileHeader)
|
||||
|
||||
//Get the file size
|
||||
FileStat, _ := Openfile.Stat() //Get info from file
|
||||
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
|
||||
|
||||
//Send the headers
|
||||
writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
|
||||
writer.Header().Set("Content-Type", "audio/mpeg")
|
||||
writer.Header().Set("Content-Length", FileSize)
|
||||
|
||||
//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
|
||||
return
|
||||
}
|
||||
|
||||
type Played struct {
|
||||
song int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user