blob: f9dca200ed5c87bc098180c1e5c2477cac12dc06 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package endpoints
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
settings "physick.ru/culture_exam/Settings"
)
type getErr struct {
Error string
}
func (base getErr) String() string {
var outp, jsonErr = json.Marshal(base)
if jsonErr != nil {
return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
}
return string(outp)
}
func getSong(w http.ResponseWriter, r *http.Request) {
var songName = r.URL.Query().Get("name")
if len(songName) == 0 {
fmt.Fprint(w, getErr { Error: "No song name provided" })
w.WriteHeader(http.StatusBadRequest)
return
}
var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, songName)
var file, fopenErr = os.ReadFile(fullPath)
if fopenErr != nil {
fmt.Fprint(w, getErr { Error: fopenErr.Error()})
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Add("Content-Type", "audio/mpeg")
var audioChunks = bytes.NewBuffer(file)
if _, err := audioChunks.WriteTo(w); err != nil {
log.Println(err)
}
}
|